簡體   English   中英

神經網絡中權重的更新

[英]update of weights in a neural network

我正在嘗試為AND示例的情況編寫感知器學習規則。 以圖形方式,我們將擁有:

在此處輸入圖片說明

其中x0 = 1的值,用於更新權重的算法為:

在此處輸入圖片說明

並且我用Python編寫了以下程序:

import math

def main():
    theta=[-0.8,0.5,0.5]
    learnrate=0.1
    target=[0,0,0,1]
    output=[0,0,0,0]
    x=[[1,0,0],[1,0,1],[1,1,0],[1,1,1]]
    for i in range(0,len(x)):
       output[i]=evaluate(theta,x[i])

    for j in range(0,100):
        update(theta,x,learnrate,target,output)

def evaluate(theta,x): 
    r=theta[0]*x[0]+theta[1]*x[1]+theta[2]*x[2]
    r=1/(1+math.exp(-r))
    return r

def update(theta,x,n,target,output):
    for i in range(0,len(x)):
        for j in range(0,len(x[i])):
            delta=n*(target[i]-output[i])*x[i][j]
            theta[j]=theta[j]+delta
        print theta
        r=evaluate(theta,x[i])
        print r
    print "\n"

if __name__=="__main__":
    main()

對於第一組theta值,運行程序時會發生問題:

theta=[-0.8,0.5,0.5]

我得到的價值觀:

[-7.869649929246505, 0.7436243430418894, 0.7436243430418894]
0.000382022127989
[-7.912205677565339, 0.7436243430418894, 0.7010685947230553]
0.000737772440166
[-7.954761425884173, 0.7010685947230553, 0.7010685947230553]
0.000707056388635
[-7.90974482561542, 0.7460851949918075, 0.7460851949918075]
0.00162995036457

括號項是更新的theta值,其他值是評估結果。 在這種情況下,我的結果對於最后一種情況應該非常接近1,對於其他情況應該接近0,但是這沒有發生。

當我使用以下值時:

theta=[-30,20,20]

它們整齊地接近最后一個數據集,而另一個則為0:

[-30.00044943890137, 20.0, 20.0]
9.35341823401e-14
[-30.000453978688242, 20.0, 19.99999546021313]
4.53770586567e-05
[-30.000458518475114, 19.99999546021313, 19.99999546021313]
4.53768526644e-05
[-30.000453978688242, 20.0, 20.0]
0.999954581518

甚至當我嘗試另一套時:

theta=[-5,20,20]

我的結果不如以前的結果:

[-24.86692245237865, 10.100003028432075, 10.100003028432075]
1.5864734081e-11
[-24.966922421788425, 10.100003028432075, 10.000003059022298]
3.16190904073e-07
[-25.0669223911982, 10.000003059022298, 10.000003059022298]
2.86101378609e-07
[-25.0669223911982, 10.000003059022298, 10.000003059022298]
0.00626235903

我缺少部分內容或此實現中存在問題嗎? 我知道還有另一種使用導數的算法,但是我想實現這種幼稚的情況。

謝謝

問題是權重更改后您沒有重新計算輸出,因此誤差信號保持恆定,並且每次迭代時權重將以相同的方式改變。

更改代碼,如下所示:

def update(theta,x,n,target,output):
    for i in range(0,len(x)):
        output[i] = evaluate(theta,x[i])  # This line is added
        for j in range(0,len(x[i])):
            delta=n*(target[i]-output[i])*x[i][j]
            theta[j]=theta[j]+delta
        print theta
        r=evaluate(theta,x[i])
        print r
    print "\n"

並且您應該發現它收斂得更好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM