对 C++ 代码中的神经网络梯度下降部分感到困惑

问题描述

我打算用 C++ 制作我自己的神经网络库,我正在通过其他人的代码来确保我在正确的轨道上......下面是一个示例代码,我正在尝试从中学习......

代码中的所有内容都有意义,除了梯度下降部分,他通过增加正学习率来逐字地更新权重......我们不应该需要对梯度取负值来达到最优值吗??

行号:137 - 157。

double Neuron::eta = 0.10;
void Neuron::updateInputWeights(Layer &prevLayer)
{
    // The weights to be updated are in the Connection container
    // in the nuerons in the preceding layer

    for(unsigned n = 0; n < prevLayer.size(); ++n)
    {
        Neuron &neuron = prevLayer[n];
        double oldDeltaWeight = neuron.m_outputWeights[m_myIndex].deltaWeight;

        double newDeltaWeight = 
                // Individual input,magnified by the gradient and train rate:
                eta
                * neuron.getoutputVal()
                * m_gradient
                // Also add momentum = a fraction of the prevIoUs delta weight
                + alpha
                * oldDeltaWeight;
// updating Weights
        neuron.m_outputWeights[m_myIndex].deltaWeight = newDeltaWeight;
        neuron.m_outputWeights[m_myIndex].weight += newDeltaWeight;
    }
}

里面的东西都是为了权重更新而加的东西,里面没有负号。

https://github.com/huangzehao/SimpleNeuralNetwork/blob/master/src/neural-net.cpp

好在它工作正常,这让我很奇怪......

我向我认识的每个人都问了这个问题,他们都感到困惑。

这是创建神经网络库的视频表示...与上面的代码相同。

https://vimeo.com/19569529

解决方法

是的,这确实令人困惑,但我认为这是这一行的症结所在。 (我可能错了,但如果你说训练有效,那么唯一可能改变迹象的线路应该是这个。)

eta * neuron.getOutputVal() * m_gradient

其中 neuron.getOutputVal() 提供更新的方向。