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

本文介绍了一个使用AutoHotkey编写的OR逻辑门感知器程序。该程序通过调整权重和偏置来模拟人工神经网络的基本单元——感知器,并实现了对OR逻辑门的功能模拟。通过多次迭代训练,感知器能够正确地预测输入数据对应的输出结果。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

;~ ================
 
;~ ---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
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值