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

;~ ================
 
;~ ---Artificial Neural Networks---
 
;~ --OR 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.
 
/*
OR Truth Table:
 
Input   Output
0   0           0
0   1           1
1   0           1
1   1           1
 
*/
 
;========OR 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 data
perceptron := new perceptron()
bool := true
trainingLoops := 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 . " OR " . 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 = 1) or ( i2 =1))
        return 1
    else
        return 0
}

おすすめ

転載: blog.csdn.net/liuyukuan/article/details/72876969