在YouTube上的This video應該是一個很好的資源!如果你只是對數學公式感興趣,可以跳到第二部分的末尾。如果你想知道如何更新權重,我建議觀看這兩個視頻。使用這個視頻,我開發了以下算法:
public void applyBackpropagation(double expectedOutput[]){
for (int i = 0; i < expectedOutput.length; i++) {
double d = expectedOutput[i];
if (d < 0 || d > 1) {
if (d < 0)
expectedOutput[i] = 0 + epsilon;
else
expectedOutput[i] = 1 - epsilon;
}
}
int i = 0;
for(Neuron n : outputLayer){
ArrayList<Connection> connections = n.getAllInConnections();
double Ok = n.getOutput();
double tk = expectedOutput[i];
double dk = Ok*(1-Ok)*(Ok - tk);
for(Connection c : connections){
double dw = -learningRate * dk * c.leftNeuron.getOutput();
c.setWeight(c.getWeight() + dw);
}
i++;
}
for(Neuron n : hiddenLayer){
ArrayList<Connection> connections = n.getAllInConnections();
double Oj = n.getOutput();
double sum = 0;
i = 0;
for(Neuron out_n : outputLayer){
double out_ok = out_n.getOutput();
double out_tk = expectedOutput[i];
double out_dk = out_ok*(1-out_ok)*(out_ok - out_tk);
double wjk = getWeightByNeurons(n, out_n);
sum += out_dk * wjk;
i++;
}
double dj = Oj*(1-Oj)*sum;
for(Connection c : connections){
double dw = -learningRate * dj * c.getFromNeuron().getOutput();
c.setWeight(c.getWeight() + dw);
}
}
}
使用的激活函數是sigmoid。