在java中尝试XOR神经网络,但网络总是预测它训练的最终输出。
这是我的代码
for( int i = 0; i < 4; i++ ) { //Forward pass
diff = 1;
while( diff > 0.01 ) {
SumError = 0;
Y1 = ( InputOne[i] * Weight[0] ) + ( InputTwo[i] * Weight[1] ) + Weight[6];
Y1 = 1 / ( 1 + Math.exp( -Y1 ) );
Node1[i] = Y1;
Y2 = ( InputOne[i] * Weight[2] ) + ( InputTwo[i] * Weight[3] ) + Weight[7];
Y2 = 1 / ( 1 + Math.exp( -Y2 ) );
Node2[i] = Y2;
Y3 = ( Y1 * Weight[4] ) + ( Y2 * Weight[5] ) + Weight[8];
Y3 = 1 / ( 1 + Math.exp( -Y3 ) );
Node3[i] = Y3;
diff = Math.abs( Result[i] - Y3 );
System.out.println( i + " " + Result[i] + " " + Y3 + " " + diff );
//Error Signals
Delta3[i] = Y3 * ( 1 - Y3 ) * ( Result[i] - Y3 );
Delta2[i] = Node2[i] * ( 1 - Node2[i] ) * ( Delta3[i] * Weight[5] );
Delta1[i] = Node1[i] * ( 1 - Node1[i] ) * ( Delta3[i] * Weight[4] );
//Update Weights
Weight[0] = Weight[0] + ( ( WeightChange[0] * alpha ) + ( eta * Delta2[i] * InputOne[i] ) );
Weight[2] = Weight[2] + ( ( WeightChange[2] * alpha ) + ( eta * Delta2[i] * InputTwo[i] ) );
Weight[1] = Weight[1] + ( ( WeightChange[1] * alpha ) + ( eta * Delta1[i] * InputOne[i] ) );
Weight[3] = Weight[3] + ( ( WeightChange[3] * alpha ) + ( eta * Delta1[i] * InputTwo[i] ) );
Weight[4] = Weight[4] + ( ( WeightChange[4] * alpha ) + ( eta * Delta3[i] * Y1 ) );
Weight[5] = Weight[5] + ( ( WeightChange[5] * alpha ) + ( eta * Delta3[i] * Y2 ) );
Weight[6] = Weight[6] + ( ( WeightChange[6] * alpha ) + ( eta * Delta1[i] ) );
Weight[7] = Weight[7] + ( ( WeightChange[7] * alpha ) + ( eta * Delta2[i] ) );
Weight[8] = Weight[8] + ( ( WeightChange[8] * alpha ) + ( eta * Delta3[i] ) );
for( int k = 0; k < 9; k++ ) {
WeightChange[k] = OldWeight[k] - Weight[k];
OldWeight[k] = Weight[k];
}
//Global Error
for( int j = 0; j < 4; j++ ) {
Y1 = ( InputOne[j] * Weight[0] ) + ( InputTwo[j] * Weight[1] ) + Weight[6];
Y1 = 1 / ( 1 + Math.exp( -Y1 ) );
Y2 = ( InputOne[j] * Weight[2] ) + ( InputTwo[j] * Weight[3] ) + Weight[7];
Y2 = 1 / ( 1 + Math.exp( -Y2 ) );
Y3 = ( Y1 * Weight[4] ) + ( Y2 * Weight[5] ) + Weight[8];
Y3 = 1 / ( 1 + Math.exp( -Y3 ) );
//System.out.println( Y3 + " " + Math.abs( Result[j] - Y3 ) );
SumError = SumError + Math.pow( ( Result[j] - Y3 ) , 2 );
}
SumError = SumError * 0.5;
}
Count = Count + 1;
}
InputOne
,InputTwo
和Result
是XOR的真值表条目,权重是随机分配的,WeightChange
是动量。
然后我再次输入真值表,每个输出或多或少与它训练的最后一个输入相同。
有人有什么想法吗?
答案 0 :(得分:2)
你应该训练case1(一次),case2(一次),case3(一次),case4(一次) - &gt;再次重复,直到它了解所有四个。不是一个案例。每个案例进行一次迭代。你需要让它学习塑料。对于你的教学方案,当你教case2时,它会忘记case1。您需要在一个普通的while循环中重复提供所有案例,直到总误差减少到一个下限。
当你只学习单个案例时,它会很好地学习但会忘记其他案例。所以你一个接一个地喂食,并把一组病例的总误差(可能是误差平方和)小于公差。