目前,我在反向传播算法上遇到了问题。我正在尝试实现它,并使用它来识别人脸的方向(左,右,下,直)。基本上,我有N个图像,读取像素并将其值(0到255)更改为0.0到1.0之间的值。所有图像均为32*30。我有一个由960个神经元组成的输入层,一个由3个神经元组成的隐藏层和一个由4个神经元组成的输出层。例如,输出的<0.1,0.9,0.1,0.1>表示person向右看。我遵循了伪代码。然而,它不能正常工作-它不计算正确的权重,因此它不能处理训练和测试用例。以下是代码的一部分:
// main function - it runs the algorithm
private void runBackpropagationAlgorithm() {
for (int i = 0; i < 900; ++i) {
for (ImageUnit iu : images) {
double [] error = calcOutputError(iu.getRatioMatrix(), iu.getClassification());
changeHiddenUnitsOutWeights(error);
error = calcHiddenError(error);
changeHiddenUnitsInWeights(error,iu.getRatioMatrix());
}
}
}
// it creates the neural network
private void createNeuroneNetwork() {
Random generator = new Random();
for (int i = 0; i < inHiddenUnitsWeights.length; ++i) {
for (int j = 0; j < hiddenUnits; ++j) {
inHiddenUnitsWeights[i][j] = generator.nextDouble();
}
}
for (int i = 0; i < hiddenUnits; ++i) {
for (int j = 0; j < 4; ++j) {
outHddenUnitsWeights[i][j] = generator.nextDouble();
}
}
}
// Calculates the error in the network. It runs through the whole network.
private double [] calcOutputError(double[][] input, double [] expectedOutput) {
int currentEdge = 0;
Arrays.fill(hiddenUnitNodeValue, 0.0);
for (int i = 0; i < input.length; ++i) {
for (int j = 0; j < input[0].length; ++j) {
for (int k = 0; k < hiddenUnits; ++k) {
hiddenUnitNodeValue[k] += input[i][j] * inHiddenUnitsWeights[currentEdge][k];
}
++currentEdge;
}
}
double[] out = new double[4];
for (int j = 0; j < 4; ++j) {
for (int i = 0; i < hiddenUnits; ++i) {
out[j] += outHddenUnitsWeights[i][j] * hiddenUnitNodeValue[i];
}
}
double [] error = new double [4];
Arrays.fill(error, 4);
for (int i = 0; i < 4; ++i) {
error[i] = ((expectedOutput[i] - out[i])*(1.0-out[i])*out[i]);
//System.out.println((expectedOutput[i] - out[i]) + " " + expectedOutput[i] + " " + out[i]);
}
return error;
}
// Changes the weights of the outgoing edges of the hidden neurons
private void changeHiddenUnitsOutWeights(double [] error) {
for (int i = 0; i < hiddenUnits; ++i) {
for (int j = 0; j < 4; ++j) {
outHddenUnitsWeights[i][j] += learningRate*error[j]*hiddenUnitNodeValue[i];
}
}
}
// goes back to the hidden units to calculate their error.
private double [] calcHiddenError(double [] outputError) {
double [] error = new double[hiddenUnits];
for (int i = 0; i < hiddenUnits; ++i) {
double currentHiddenUnitErrorSum = 0.0;
for (int j = 0; j < 4; ++j) {
currentHiddenUnitErrorSum += outputError[j]*outHddenUnitsWeights[i][j];
}
error[i] = hiddenUnitNodeValue[i] * (1.0 - hiddenUnitNodeValue[i]) * currentHiddenUnitErrorSum;
}
return error;
}
// changes the weights of the incomming edges to the hidden neurons. input is the matrix of ratios
private void changeHiddenUnitsInWeights(double [] error, double[][] input) {
int currentEdge = 0;
for (int i = 0; i < input.length; ++i) {
for (int j = 0; j < input[0].length; ++j) {
for (int k = 0; k < hiddenUnits; ++k) {
inHiddenUnitsWeights[currentEdge][k] += learningRate*error[k]*input[i][j];
}
++currentEdge;
}
}
}随着算法的运行,它会计算越来越大的权重,最终接近无穷大(NaN值)。我检查了代码。可惜,我没能解决我的问题。我将坚定地感谢任何试图帮助我的人。
发布于 2012-08-24 08:01:24
您的代码缺少传递函数。听起来您想要具有softmax输出的逻辑函数。您需要在calcOutputError中包含以下内容
// Logistic transfer function for hidden layer.
for (int k = 0; k < hiddenUnits; ++k) {
hiddenUnitNodeValue[k] = logistic(hiddenUnitNodeValue[k]);
}和
// Softmax transfer function for output layer.
sum = 0;
for (int j = 0; j < 4; ++j) {
out[j] = logistic(out[j]);
sum += out[j];
}
for (int j = 0; j < 4; ++j) {
out[j] = out[j] / sum;
}其中逻辑函数是
public double logistic(double x){
return (1/(1+(Math.exp(-x)));
}请注意,softmax传输函数给出的输出总和为1,因此可以将其解释为概率。
此外,您对输出层的误差梯度的计算也不正确。它应该是简单的
for (int i = 0; i < 4; ++i) {
error[i] = (expectedOutput[i] - out[i]);
} 发布于 2012-08-17 17:47:48
我没有检查你所有的代码。我只想给你一些一般性的建议。我不知道你的目标是(1)学习人脸的方向,还是(2)实现你自己的神经网络。
在情况(1)中,您应该考虑those库之一。它们可以正常工作,并为您提供更灵活的配置选项。例如,标准反向传播是神经网络中最差的优化算法之一。收敛依赖于学习率。我看不出您在实现中选择了哪个值,但它可能太高了。还有其他优化算法不需要学习率,也不需要在训练过程中调整学习率。此外,隐藏层中的3个神经元很可能是不够的。大多数用于图像的神经网络都有数百个甚至数千个隐藏单元。我建议你首先尝试用一个完全开发的库来解决你的问题。如果它确实有效,请尝试实现您自己的ANN或感到高兴。:)
在情况(2)中,你应该首先尝试解决一个更简单的问题。取一个非常简单的人工数据集,然后取一个standard benchmark,然后对您的数据进行尝试。验证您的反向传播实现是否有效的一个好方法是与numerical differentation method进行比较。
发布于 2012-08-17 22:52:26
我还没有测试过你的代码,但我几乎可以肯定你一开始就有很大的权重。大多数关于主题的介绍都把它留在了“初始化具有随机值的权重”上,而忽略了算法实际上对一些初始值的发散(转到Inf)。
尝试使用较小的起始值,例如,在-1/5和1/5之间,并将其缩小。
另外还有一个矩阵乘法的方法,你(只)用了4次,很容易看出来有没有问题。
https://stackoverflow.com/questions/11991651
复制相似问题