【发布时间】: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
好在它工作正常,这让我很奇怪......
我问过我认识的每个人这个问题,他们都很困惑。
这是创建神经网络库的视频表示...与上面的代码相同。
【问题讨论】:
-
当
m_gradient是渐变的负数时,您发布的代码中不需要-。 -
只需添加
-即可查看会发生什么。当你得到错误的结果时,你知道不应该有-。错误标志的错误总是发生,你注意到它们并改变标志,我认为它没有什么魔力 -
你怎么知道那是梯度的负值?对不起,我是初学者……你能详细说明一下吗?
-
哦,所以这只是试错法?
-
我不知道。你说你得到了正确的结果,
m_gradient是梯度的负值是一种可能的解释
标签: c++ machine-learning neural-network gradient-descent