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

16 篇文章 3 订阅
13 篇文章 2 订阅

参考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   Output
0	0	0
0	1	0
1	0	0
1	1	1
*/
 
;========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 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 . " 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
} 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值