Python 前馈神经网络总是输出随机但相似的值

Python 前馈神经网络总是输出随机但相似的值,python,neural-network,feed-forward,Python,Neural Network,Feed Forward,我最近根据在线书籍和塞巴斯蒂安·拉格(Sebastian Lague)在youtube上关于神经网络的简短系列文章编写了一个神经网络。我尽可能忠实地将其编码为原始代码,但最终无法工作。我试图用它解决一个简单的XOR问题,但它似乎总是给我随机但相似的值。我甚至试着复制和粘贴作者的代码,没有做任何更改,但仍然不起作用 class NeuralNetwork: def __init__(self, layer_sizes, rate): weight_shapes = [(a

我最近根据在线书籍和塞巴斯蒂安·拉格(Sebastian Lague)在youtube上关于神经网络的简短系列文章编写了一个神经网络。我尽可能忠实地将其编码为原始代码,但最终无法工作。我试图用它解决一个简单的XOR问题,但它似乎总是给我随机但相似的值。我甚至试着复制和粘贴作者的代码,没有做任何更改,但仍然不起作用

class NeuralNetwork:

    def __init__(self, layer_sizes, rate):
        weight_shapes = [(a,b) for a,b in zip(layer_sizes[1:], layer_sizes[:-1])]
        self.weights = [np.random.standard_normal(s)/s[1]**0.5 for s in weight_shapes]
        self.biases = [np.zeros((s,1)) for s in layer_sizes[1:]]
        self.rate = rate

    def predict(self, a):
        for w,b in zip(self.weights, self.biases):
            z = np.matmul(w,a) + b
            a = self.activation(z)
        return a

    def backprop(self, a, o):

        o = np.array(o)

        self.zCollection = []

        # Forward Propogation
        for w,b in zip(self.weights, self.biases):
            z = np.matmul(w,a) + b

            self.zCollection.append(z)

            a = self.activation(z)
        
        # Output error
        error =  (a - o) * self.activationPrime(self.zCollection[-1])

        self.weights[-1] += np.matmul(error, self.activation(self.zCollection[-2]).T) * self.rate
        self.biases[-1] += error * self.rate
        
        # Looping through layers
        for i in range(2, len(self.weights)):

            error = np.multiply(self.weights[-i+1].T * error,self.activationPrime(self.zCollection[-i]))

            self.weights[-i] = np.add(self.weights[-i], np.matmul(error, self.activation(self.zCollection[-i-1]).T) * self.rate)
            self.biases[-i] = np.add(self.biases[-i], error * self.rate)

    @staticmethod
    def activation(x):
        return 1/(1+np.exp(-x))
    @staticmethod
    def activationPrime(x):
        activation = lambda x : 1/(1+np.exp(-x))
        return activation(x) * (1 - activation(x))



if __name__ == "__main__":

    inp = [[0,0],[1,0],[0,1],[1,1]]
    out = [[0],[1],[1],[0]]

    # Reformating arrays
    inp = np.array([np.array(i) for i in inp])
    inp = np.array([i.reshape((len(i), 1)) for i in inp])
    out = np.array([np.array(i) for i in out])
    out = np.array([i.reshape((len(i), 1)) for i in out])


    layer_sizes = (2,2,1)
    nn = NeuralNetwork(layer_sizes, 0.001)

    print("start")
    for j in range(100):
        for i,o in zip(inp, out):
            nn.backprop(i, o)
    print("done")

    for i in inp:
        print(f"{[list(j) for j in i]} >> {nn.predict(i)[0,0]}")
我自己做了一些调查,发现权重的更新值在每次迭代中总是很小且恒定的。我不知道为什么,但看起来重量没有变化。我相信这可能是原因,因为当我在脚本开始时设置种子时,输出值与4dp非常相似,但我不确定。我测试了前向传播,所以这不是问题所在。我还尝试将输入随机化,改变学习率、不同的层大小和数量。我还尝试了一个感知机可以解决的不同问题集。这个问题是预测两个数的和是否大于其他数。那也没用。当我将各个时代的输出错误绘制成图表时,它看起来像。从粗线可以看出,该值在振荡,似乎在减小。然而,当我测试它时,它给出了完全错误的结果

以下是我使用不同参数得到的一些输出:

学习率:100
层大小:(2,2,1)
时代:10000

[[0], [0]] >> 1.70366026492168e-23
[[1], [0]] >> 4.876567289343432e-20
[[0], [1]] >> 2.4579325136292694e-24
[[1], [1]] >> 9.206132845755066e-21
[[0], [0]] >> 0.9719657241512317
[[1], [0]] >> 0.9724187979341556
[[0], [1]] >> 0.9736236543859843
[[1], [1]] >> 0.9739884707274225
[[0], [0]] >> 0.0024879823892047168
[[1], [0]] >> 0.9970151468472171
[[0], [1]] >> 0.996966687356631
[[1], [1]] >> 0.003029227917616288
学习率:1
层大小:(2,5,5,1)
时代:10000

[[0], [0]] >> 1.70366026492168e-23
[[1], [0]] >> 4.876567289343432e-20
[[0], [1]] >> 2.4579325136292694e-24
[[1], [1]] >> 9.206132845755066e-21
[[0], [0]] >> 0.9719657241512317
[[1], [0]] >> 0.9724187979341556
[[0], [1]] >> 0.9736236543859843
[[1], [1]] >> 0.9739884707274225
[[0], [0]] >> 0.0024879823892047168
[[1], [0]] >> 0.9970151468472171
[[0], [1]] >> 0.996966687356631
[[1], [1]] >> 0.003029227917616288
学习率:1
层大小:(2,2,1)
时代:100

[[0], [0]] >> 0.3912836914268991
[[1], [0]] >> 0.49042088270977163
[[0], [1]] >> 0.4499482050352108
[[1], [1]] >> 0.5324205501065111

我好像已经修好了。我做了三个主要的改变:

  • 我在输出层错误计算中切换了a和o,结果如下:
    error=(o-a)*self.activationPrime(self.zCollection[-1])

  • 更新权重和偏差时,我将其替换

    self.weights[-1] += np.matmul(error, self.activation(self.zCollection[-2]).T) * self.rate
    self.biases[-1] += error * self.rate
    

    我在for循环中也做了同样的事情。要查看该代码,请参考帖子中的代码

  • 这些变化对少数几个时代不起作用,所以我把它们增加到100000个,这是有效的。然而,当降低学习率时,我不得不再次增加历次次数

  • 通过这些新参数和更改,我得到了以下示例:

    学习率:1
    层大小:(2,2,1)
    时代:100000

    [[0], [0]] >> 1.70366026492168e-23
    [[1], [0]] >> 4.876567289343432e-20
    [[0], [1]] >> 2.4579325136292694e-24
    [[1], [1]] >> 9.206132845755066e-21
    
    [[0], [0]] >> 0.9719657241512317
    [[1], [0]] >> 0.9724187979341556
    [[0], [1]] >> 0.9736236543859843
    [[1], [1]] >> 0.9739884707274225
    
    [[0], [0]] >> 0.0024879823892047168
    [[1], [0]] >> 0.9970151468472171
    [[0], [1]] >> 0.996966687356631
    [[1], [1]] >> 0.003029227917616288
    
    我很确定这些问题(如果你可以这样称呼的话)与我的代码无关,只是前馈神经网络的一个特点


    我花了一段时间,但a发现了算法中的第四个问题。在backprop方法内的第二个for循环中,
    错误
    计算不正确。行应该实际读取
    error=np.multiply(np.matmul(self.weights[-i+1].T,error),self.activationPrime(self.zCollection[-i])

    尝试在error函数中使用MSE:
    ((a-o)**2)