【问题标题】:Confused on Neural Net gradient descend part in c++ code对 C++ 代码中的神经网络梯度下降部分感到困惑
【发布时间】:2021-09-23 05:29:35
【问题描述】:

我打算用 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

【问题讨论】:

  • m_gradient 是渐变的负数时,您发布的代码中不需要-
  • 只需添加- 即可查看会发生什么。当你得到错误的结果时,你知道不应该有-。错误标志的错误总是发生,你注意到它们并改变标志,我认为它没有什么魔力
  • 你怎么知道那是梯度的负值?对不起,我是初学者……你能详细说明一下吗?
  • 哦,所以这只是试错法?
  • 我不知道。你说你得到了正确的结果,m_gradient 是梯度的负值是一种可能的解释

标签: c++ machine-learning neural-network gradient-descent


【解决方案1】:

是的,这确实令人困惑,但我认为关键在于这一行。 (我可能错了,但如果你说训练有效,那么唯一可能改变标志的线应该是这个。)

eta * neuron.getOutputVal() * m_gradient

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

【讨论】:

  • 啊,因为 tanh 的范围是负数和正数?
  • 那是真的吗?
  • 你能告诉我这条线是如何告诉更新方向的吗?
猜你喜欢
  • 1970-01-01
  • 2014-10-10
  • 1970-01-01
  • 2015-07-11
  • 2011-08-24
  • 1970-01-01
  • 2023-03-08
  • 2022-09-28
  • 2015-07-12
相关资源
最近更新 更多