【问题标题】:Backpropagation with Momentum动量反向传播
【发布时间】:2018-04-23 00:22:26
【问题描述】:

我正在关注 this tutorial 以实现反向传播算法。但是,我一直在为这个算法实现动量。

没有动量,这是权重更新方法的代码:

def update_weights(network, row, l_rate):
    for i in range(len(network)):
        inputs = row[:-1]
        if i != 0:
            inputs = [neuron['output'] for neuron in network[i - 1]]
        for neuron in network[i]:
            for j in range(len(inputs)):
                neuron['weights'][j] += l_rate * neuron['delta'] * inputs[j]
            neuron['weights'][-1] += l_rate * neuron['delta']

下面是我的实现:

def updateWeights(network, row, l_rate, momentum=0.5):
    for i in range(len(network)):
        inputs = row[:-1]
        if i != 0:
            inputs = [neuron['output'] for neuron in network[i-1]]
        for neuron in network[i]:
            for j in range(len(inputs)):
                previous_weight = neuron['weights'][j]
                neuron['weights'][j] += l_rate * neuron['delta'] * inputs[j] + momentum * previous_weight
            previous_weight = neuron['weights'][-1]
            neuron['weights'][-1] += l_rate * neuron['delta'] + momentum * previous_weight

这给了我一个 Mathoverflow 错误,因为权重在多个时期内以指数方式变得太大。我相信我的 previous_weight 逻辑对于更新是错误的。

【问题讨论】:

    标签: python algorithm neural-network backpropagation gradient-descent


    【解决方案1】:

    我会给你一个提示。您在实现中将momentum 乘以previous_weight,这是同一步骤中网络的另一个参数。这显然很快就爆发了。

    你应该做的是记住整个更新向量, l_rate * neuron['delta'] * inputs[j],在上一个反向传播步骤上并将其加起来。它可能看起来像这样:

    velocity[j] = l_rate * neuron['delta'] * inputs[j] + momentum * velocity[j]
    neuron['weights'][j] += velocity[j]
    

    ... 其中velocity 是一个与network 长度相同的数组,定义的范围比updateWeights 更大,并用零初始化。详情请见this post

    【讨论】:

    • 如果我错了,请纠正我,但你想记住前一个向量,所以你的velocity更新不应该如下所示:velocity[j] = l_rate * neuron['delta'] * inputs[j] + momentum * velocity[max(0, j-1)]...至少我是这样的已实施。
    猜你喜欢
    • 2011-01-09
    • 1970-01-01
    • 2019-03-18
    • 1970-01-01
    • 1970-01-01
    • 2020-11-23
    • 1970-01-01
    • 2018-05-05
    • 2011-08-25
    相关资源
    最近更新 更多