调试反向传播算法

3
我正在尝试在Python中使用numpy实现反向传播算法。我一直在使用这个网站来实现反向传播的矩阵形式。但是在XOR上测试这段代码时,即使多次运行数千次迭代,我的神经网络仍然无法收敛。我认为存在某种逻辑错误。如果有人愿意帮忙检查一下,我将非常感激。完整可运行的代码可以在Github上找到。
import numpy as np

def backpropagate(network, tests, iterations=50):

    #convert tests into numpy matrices
    tests = [(np.matrix(inputs, dtype=np.float64).reshape(len(inputs), 1),
            np.matrix(expected, dtype=np.float64).reshape(len(expected), 1))
            for inputs, expected in tests]

    for _ in range(iterations):

        #accumulate the weight and bias deltas
        weight_delta = [np.zeros(matrix.shape) for matrix in network.weights]
        bias_delta = [np.zeros(matrix.shape) for matrix in network.bias]

        #iterate over the tests
        for potentials, expected in tests:

            #input the potentials into the network
            #calling the network with trace == True returns a list of matrices,
            #representing the potentials of each layer 
            trace = network(potentials, trace=True)
            errors = [expected - trace[-1]]

            #iterate over the layers backwards
            for weight_matrix, layer in reversed(list(zip(network.weights, trace))):
                #compute the error vector for a layer
                errors.append(np.multiply(weight_matrix.transpose()*errors[-1],
                                          network.sigmoid.derivative(layer)))

            #remove the input layer
            errors.pop()
            errors.reverse()

            #compute the deltas for bias and weight
            for index, error in enumerate(errors):
                bias_delta[index] += error
                weight_delta[index] += error * trace[index].transpose()

        #apply the deltas
        for index, delta in enumerate(weight_delta):
            network.weights[index] += delta
        for index, delta in enumerate(bias_delta):
            network.bias[index] += delta

此外,这是计算输出以及sigmoid函数的代码。很少可能出现错误;我已经用模拟退火训练了一个网络来模拟XOR运算。
# the call function of the neural network
def __call__(self, potentials, trace=True):

    #ensure the input is properly formated
    potentials = np.matrix(potentials, dtype=np.float64).reshape(len(potentials), 1)

    #accumulate the trace
    trace = [potentials]

    #iterate over the weights
    for index, weight_matrix in enumerate(self.weights):
        potentials = weight_matrix * potentials + self.bias[index]
        potentials = self.sigmoid(potentials)
        trace.append(potentials)

    return trace

#The sigmoid function that is stored in the network
def sigmoid(x):
    return np.tanh(x)
sigmoid.derivative = lambda x : (1-np.square(x))
1个回答

3
问题在于缺失步长参数。梯度应该被额外缩放,以使权重空间中的整个步骤不会一次性完成。因此,应该使用以下方式而不是:network.weights[index] += deltanetwork.bias[index] += delta
def backpropagate(network, tests, stepSize = 0.01, iterations=50):

    #...

    network.weights[index] += stepSize * delta

    #...

    network.bias[index] += stepSize * delta

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接