Python Numpy - 单神经元感知器重量更新问题

时间:2016-02-11 00:09:36

标签: python numpy

我被要求为一个编程任务编程一个感知器,并且我正在接受重量更新。特别是它会混淆调整第0个重量和其余答案的抛出。谢谢您的帮助。

到目前为止我的代码是。

import copy
def train(weights,inputs,targets,eta,nIterations):
    biasInputs = np.c_[np.ones((4,1)),inputs]
    print weights
    for n in range(nIterations): 
        activations = forward(inputs,weights)
        (rows,cols) = np.shape(biasInputs)
        # Here is where you update the weights using the learning parameter eta
        weights = np.add(weights, np.multiply(np.dot((np.subtract(targets,activations)), biasInputs) ,eta))
        # After you are done print the weights for verification purposes
        print weights

    return weights

我正在使用的更新方程是wi = wi + xi(f(y) - f(y!))g

  • w是权重
  • x是输入
  • f(y)是目标(预测)值
  • f(y!)是激活(实际)值
  • 和g是学习因素(这里是eta)

我运行时的输出是

Your output has 27 lines:
---
Line 1: Training for AND inputs
Line 2: [ 0.5 -0.25 0.6 ]
Line 3: [ 0.2 -0.35 0.5 ]
Line 4: [ -5.55111512e-17 -3.50000000e-01 4.00000000e-01]
Line 5: [-0.1 -0.35 0.3 ]
Line 6: [-0.1 -0.25 0.3 ]
Line 7: [-0.1 -0.15 0.3 ]
Line 8: [-0.2 -0.15 0.2 ]
Line 9: [-0.1 -0.05 0.3 ]
Line 10: [-0.2 -0.05 0.2 ]
Line 11: [-0.1 0.05 0.3 ]
Line 12: [-0.2 0.05 0.2 ]
Line 13: [-0.2 0.05 0.2 ]
Line 14: Training for XOR inputs
Line 15: [ 0.5 -0.25 0.6 ]
Line 16: [ 0.3 -0.35 0.5 ]
Line 17: [ 0.2 -0.35 0.4 ]
Line 18: [ 0.1 -0.35 0.3 ]
Line 19: [ -2.77555756e-17 -3.50000000e-01 2.00000000e-01]
Line 20: [ 0.1 -0.25 0.2 ]
Line 21: [ -2.77555756e-17 -2.50000000e-01 1.00000000e-01]
Line 22: [ 0.1 -0.15 0.1 ]
Line 23: [ -2.77555756e-17 -1.50000000e-01 2.77555756e-17]
Line 24: [ 0.2 -0.05 0.1 ]
Line 25: [ -2.77555756e-17 -1.50000000e-01 2.77555756e-17]
Line 26: [ -2.77555756e-17 -1.50000000e-01 2.77555756e-17]
Line 27: 


however the expected output is different. 

    Instructor output has 27 lines:
---
Line 1: This line is correct <
Line 2: This line is correct
Line 3: This line is correct
Line 4: [ 2.77555756e-17 -3.50000000e-01 4.00000000e-01]
Line 5: [-0.2 -0.35 0.3 ]
Line 6: [-0.2 -0.25 0.3 ]
Line 7: [-0.2 -0.15 0.3 ]
Line 8: [-0.2 -0.05 0.3 ]
Line 9: [-0.3 -0.05 0.2 ]
Line 10: [-0.2 0.05 0.3 ]
Line 11: [-0.3 0.05 0.2 ]
Line 12: [-0.2 0.15 0.3 ]
Line 13: [-0.2 0.15 0.3 ]
Line 14: This line is correct
Line 15: This line is correct
Line 16: This line is correct
Line 17: This line is correct
Line 18: This line is correct
Line 19: [ 2.77555756e-17 -3.50000000e-01 2.00000000e-01]
Line 20: [ 2.77555756e-17 -2.50000000e-01 2.00000000e-01]
Line 21: [ 2.77555756e-17 -1.50000000e-01 2.00000000e-01]
Line 22: [-0.1 -0.15 0.1 ]
Line 23: [ 2.77555756e-17 -5.00000000e-02 1.00000000e-01]
Line 24: [ -1.00000000e-01 -5.00000000e-02 2.77555756e-17]
Line 25: [ 0.1 0.05 0.1 ]
Line 26: [ 0.1 0.05 0.1 ]
Line 27: This line is correct

0 个答案:

没有答案