Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 神经网络中权值的更新_Python_Algorithm_Neural Network_Perceptron - Fatal编程技术网

Python 神经网络中权值的更新

Python 神经网络中权值的更新,python,algorithm,neural-network,perceptron,Python,Algorithm,Neural Network,Perceptron,我试图为一个例子编写感知器学习规则。从图形上看,我们将有: 其中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)):

我试图为一个例子编写感知器学习规则。从图形上看,我们将有:

其中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=[-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
括号中的术语是更新后的θ值,而其他值是评估结果。在这种情况下,对于最后一种情况,我的结果应该非常接近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"
你会发现它收敛得更好