gpt4 book ai didi

python - 尽管一切似乎都正常,神经网络却给出了错误的预测

转载 作者:行者123 更新时间:2023-11-30 08:54:09 25 4
gpt4 key购买 nike

我正在尝试编写我的第一个神经网络,但我已经完全陷入这个问题一个多星期了。我正在学习 Andrew NG 的机器学习类(class),并在 python 中实现了以下函数。

    forwardPropogate() #does forward propagation
backwardPropogate() #computes the gradients using backpropogation
costFunction() #takes as input, all the parameters of the neural network in a rolled up single array and computes its cost
gradientDescend() #tries to minimise the cost using gradient descend

当我尝试训练网络时,我发现它给了我非常糟糕的结果,当我无法弄清楚代码出了什么问题时,我下载了代码的 MATLAB 版本并尝试将其与我自己的代码进行比较。

为了确保我的实现正确,我运行了 MATLAB 代码,从中获取参数并通过我的 backwardPropogate()costFunction() 运行它。

运行 backwardPropogate() 这是 MATLAB 和我自己的代码给出的梯度图。 enter image description here Python正如您所看到的,它们非常相似。此外,我还对两个输出进行了手动初始化,这足以让我确信 by backwardPropogate() 已正确实现。我还进行了数值梯度检查,这也匹配得很好。

MATLAB 代码找到的参数成本为 J = 0.14942,Python 给出 J = 0.149420032652。我确信 costFunction()backwardPropogate() 已正确实现(我不应该这样做吗?)。

当我运行我的gradientDescend()时,我得到了成本值与迭代次数的关系图。 J values 。这看起来又不错。

我不明白为什么代码仍然给我错误的值。即使在训练集上,成功率也接近 10%。

这是我的梯度下降及其调用。

   def gradientDescend(self,gradientFunction,thetaValues):

JValues = np.zeros(MAX_ITER)

for i in range(0,MAX_ITER):
thetaValues = thetaValues - ALPHA * gradientFunction(thetaValues)

J = self.costFunction(thetaValues)
JValues[i] = J

print i/MAX_ITER * 100 #show percentage completed

return thetaValues,JValues

def train(self):

thetaValues = (np.random.rand(NoTheta1+NoTheta2,1) * (2 * INIT_EPSILON)) - INIT_EPSILON

trainedThetas,JVals = self.gradientDescend(self.getGradients,thetaValues)
self.theta1,self.theta2 = self.unrollParameters(thetaValues)

xaxis = np.arange(0,len(JVals))
plt.plot(xaxis,JVals)
plt.show()

return self.theta1,self.theta2

经过进一步检查,我发现我们做的参数的初始随机值和我训练的参数一样糟糕!在所有的事情中,这是我最不理解的。成本函数似乎从循环开始到结束一直在减少。所以即使最终的参数不好,至少也应该比最初的参数做得更好。我不知道从这里该去哪里。欢迎大家提出意见。

最佳答案

train() 中,实际上并未使用 GradientDescend() 函数的输出 trainedThetas。在GradientDescend() 之后的行中,self.unrollParameters(thetaValues) 采用thetaValues 的原始随机向量。这就是为什么你看不到成本函数有任何学习或改进。

unrollParameters() 中的 thetaValues 替换为 trainedValues 即可。

关于python - 尽管一切似乎都正常,神经网络却给出了错误的预测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43861424/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com