在阅读了很多其他人的神经网络代码后,我确信我的代码有问题。它有效,我可以训练一个网络,只是为了训练隐藏层中的下一个感知器,我必须训练最后一个,我不应该能够并行训练隐藏层中的所有单元吗?
下面是它计算隐藏层误差的代码:
for(int i=n->numOfPerceptronLayers-2;i>=1;i--) { // for all hidden layers
float sum = 0.0; // <- This here is the problem
for(int j=0;j<n->perceptronLayers[i].numOfPerceptrons;j++) { // For all the units in the current hidden layer
for(int k=0;k<n->perceptronLayers[i].perceptrons[j].numOfConnections;k++) { // Loop through the current units connections to the previous layer (output layer)
sum += n->perceptronLayers[i+1].perceptrons[k].error * n->perceptronLayers[i+1].perceptrons[k].weights[j];
}
n->perceptronLayers[i].perceptrons[j].error = n->perceptronLayers[i].perceptrons[j].output * (1.0 - n->perceptronLayers[i].perceptrons[j].output) * sum;
}
}
它应该是这样的(但这不起作用):
for(int i=n->numOfPerceptronLayers-2;i>=1;i--) { // for all hidden layers
for(int j=0;j<n->perceptronLayers[i].numOfPerceptrons;j++) { // For all the units in the current hidden layer
float sum = 0.0;
for(int k=0;k<n->perceptronLayers[i].perceptrons[j].numOfConnections;k++) { // Loop through the current units connections to the previous layer (output layer)
sum += n->perceptronLayers[i+1].perceptrons[k].error * n->perceptronLayers[i+1].perceptrons[k].weights[j];
}
n->perceptronLayers[i].perceptrons[j].error = n->perceptronLayers[i].perceptrons[j].output * (1.0 - n->perceptronLayers[i].perceptrons[j].output) * sum;
}
}
为什么必须为整个层而不是单个感知器声明 sum 变量?