1

在阅读了很多其他人的神经网络代码后,我确信我的代码有问题。它有效,我可以训练一个网络,只是为了训练隐藏层中的下一个感知器,我必须训练最后一个,我不应该能够并行训练隐藏层中的所有单元吗?

下面是它计算隐藏层误差的代码:

    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 变量?

4

2 回答 2

0

除非我遗漏了什么,否则我相信第一个代码段是错误的,而后一段是正确的。

在第一个代码段中,您对整个层使用单个“sum”变量会导致错误随着每个后续感知器处理而累积。因此,感知器 j 总是比感知器 j-1 有更多的错误。

后一个代码解决了这个问题,但你说它是不工作的。唯一合理的结论是,真正的问题出在代码的其他地方,因为第一个代码段不应该工作。

另外:您确实应该能够并行训练所有层的感知器,因为每个感知器仅依赖于它的前向连接来分担错误(在标准的前馈反向传播中)。

于 2011-08-17T00:34:09.860 回答
0

我似乎找到了问题所在,基本上我训练单个感知器的 TrainPerceptron(Perceptron* p, float error, float moment) 函数通过参数给出了感知器的错误,即使感知器结构具有错误属性。我正在将错误属性传递给函数,但我猜有些事情搞混了,因为在我删除了该参数并使用存储在感知器结构中的错误之后,它就起作用了。

于 2011-08-17T01:59:25.173 回答