[AHK]AutoHotkey也玩神经网络实现AND逻辑感知器

来源:互联网 发布:淘宝签到领金币 编辑:程序博客网 时间:2024/06/09 18:19

参考OR逻辑感知器,写了AND逻辑感知器

;~ ================ ;~ ---Artificial Neural Networks--- ;~ --AND Logic Gate Perceptron ;~ =======Code=========#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.#Warn  ; Enable warnings to assist with detecting common errors.SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory. /*AND Truth Table:Input   Output000010100111*/ ;========AND Logic Gate Perceptron======= class perceptron{    c := 0.01 ; learning constant    bias :=  - 0.5    weights := [] ; initialize array          __New()    {        Loop, 2 ; Get random weights        {            Random, rand, -1.0, 1.0 ; Generate a random number between -1 and 1            this.weights.Insert(rand)        }    }       getOutput(i1,i2)    {          sum := 0               sum := this.bias + i1*this.weights[1] + i2*this.weights[2]        return % stepF(sum)    }       train(i1,i2)    {        actual := this.getOutput(i1,i2)               error := desired(i1,i2) - actual ; calculate error               this.weights[1] := this.weights[1] + (this.c * error * i1)        this.weights[2] := this.weights[2] + (this.c * error * i2)    }   } training := [[0,0],[0,1],[1,0],[1,1]] ; training dataperceptron := new perceptron()bool := truetrainingLoops := 500 ; training the perceptron Loop, % trainingLoops    Loop, 4        perceptron.train(training[A_Index,1],training[A_Index,2]) MsgBox, Training finished!   while(bool){    InputBox, input1, Input 1, Please enter 1 or 0.    InputBox, input2, Input 2, Please enter 1 or 0.       MsgBox, 4,,% input1 . " AND " . input2 . " = " . perceptron.getOutput(input1,input2) . "`nDo you want to try other inputs?"       IfMsgBox, No        bool := false} ;-----Functions----- stepF(float) ; Step Function(Activation Function){    if float > 0        return 1    else        return 0}desired(i1,i2) ; get the desired result{    if(( i1 = 0) or ( i2 =0))        return 0    else        return 1}