我試圖教2個輸入,4個隱藏節點(全部在同一層)和1個輸出節點的神經網絡。二進制表示可以正常工作,但是我對雙極性有問題。我無法弄清楚爲什麼,但總誤差有時會彙集到2.xx左右的相同數字。我的sigmoid是2 /(1 + exp(-x)) - 1.也許我在sigmoiding在錯誤的地方。例如,爲了計算輸出誤差,我應該比較sigmoided輸出與期望值還是sigmoided期望值?神經網絡教學:雙極XOR
我在這裏關注這個網站:http://galaxy.agh.edu.pl/~vlsi/AI/backp_t_en/backprop.html,但他們使用不同的功能,然後我被指示使用。即使當我嘗試實現他們的功能時,我仍遇到同樣的問題。無論哪種方式,我停留在相同數量的一半時間(不同的實現不同的數字)。請告訴我,如果我的代碼在某個地方犯了錯誤,或者這是正常的(我不明白這是怎麼回事)。動量被設置爲0.這是一個常見的0動量問題嗎?我們應該使用錯誤的功能是:
如果UI是一個輸出單元
Error(i) = (Ci - ui) * f'(Si)
如果用戶界面是一個隱藏的單元
Error(i) = Error(Output) * weight(i to output) * f'(Si)
public double sigmoid(double x) {
double fBipolar, fBinary, temp;
temp = (1 + Math.exp(-x));
fBipolar = (2/temp) - 1;
fBinary = 1/temp;
if(bipolar){
return fBipolar;
}else{
return fBinary;
}
}
// Initialize the weights to random values.
private void initializeWeights(double neg, double pos) {
for(int i = 0; i < numInputs + 1; i++){
for(int j = 0; j < numHiddenNeurons; j++){
inputWeights[i][j] = Math.random() - pos;
if(inputWeights[i][j] < neg || inputWeights[i][j] > pos){
print("ERROR ");
print(inputWeights[i][j]);
}
}
}
for(int i = 0; i < numHiddenNeurons + 1; i++){
hiddenWeights[i] = Math.random() - pos;
if(hiddenWeights[i] < neg || hiddenWeights[i] > pos){
print("ERROR ");
print(hiddenWeights[i]);
}
}
}
// Computes output of the NN without training. I.e. a forward pass
public double outputFor (double[] argInputVector) {
for(int i = 0; i < numInputs; i++){
inputs[i] = argInputVector[i];
}
double weightedSum = 0;
for(int i = 0; i < numHiddenNeurons; i++){
weightedSum = 0;
for(int j = 0; j < numInputs + 1; j++){
weightedSum += inputWeights[j][i] * inputs[j];
}
hiddenActivation[i] = sigmoid(weightedSum);
}
weightedSum = 0;
for(int j = 0; j < numHiddenNeurons + 1; j++){
weightedSum += (hiddenActivation[j] * hiddenWeights[j]);
}
return sigmoid(weightedSum);
}
//Computes the derivative of f
public static double fPrime(double u){
double fBipolar, fBinary;
fBipolar = 0.5 * (1 - Math.pow(u,2));
fBinary = u * (1 - u);
if(bipolar){
return fBipolar;
}else{
return fBinary;
}
}
// This method is used to update the weights of the neural net.
public double train (double [] argInputVector, double argTargetOutput){
double output = outputFor(argInputVector);
double lastDelta;
double outputError = (argTargetOutput - output) * fPrime(output);
if(outputError != 0){
for(int i = 0; i < numHiddenNeurons + 1; i++){
hiddenError[i] = hiddenWeights[i] * outputError * fPrime(hiddenActivation[i]);
deltaHiddenWeights[i] = learningRate * outputError * hiddenActivation[i] + (momentum * lastDelta);
hiddenWeights[i] += deltaHiddenWeights[i];
}
for(int in = 0; in < numInputs + 1; in++){
for(int hid = 0; hid < numHiddenNeurons; hid++){
lastDelta = deltaInputWeights[in][hid];
deltaInputWeights[in][hid] = learningRate * hiddenError[hid] * inputs[in] + (momentum * lastDelta);
inputWeights[in][hid] += deltaInputWeights[in][hid];
}
}
}
return 0.5 * (argTargetOutput - output) * (argTargetOutput - output);
}