XOR Java神经网络 [英] XOR Java Neural Network

查看:78
本文介绍了XOR Java神经网络的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试在Java中进行XOR神经网络,但该网络始终会预测其在其中进行训练的最终输出.

Attempting a XOR neural network in java, but the network always predicts the final output it trains in.

这是我的代码

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;  
        }

其中InputOneInputTwoResult是XOR的真值表条目,权重是随机分配的,而WeightChange是动量.

Where InputOne, InputTwo and Result are the truth table entries for XOR, the weights are randomly assigned and WeightChange is the momentum.

然后我再次输入真值表,每个输出与它训练的最后一个输入大致相同.

I then feed in the truth table again and every output is more or less the same as the last input it trained on.

有人有什么主意吗?

推荐答案

您应该训练case1(一次),case2(一次),case3(一次),case4(一次)->再重复一次,直到学习全部四个.没有一个案例.根据情况进行一次迭代.您需要使其学习塑料.对于您的教学方案,当您教授案例2时,它会忘记案例1.您需要在一个公共的while循环中重复喂所有情况,直到总错误减少到一个下限为止.

You should train for case1(once),case2(once),case3(once),case4(once) --> repeat again until it learns all four. Not a single case. Make single iteration per case. You need to make it learn plastic. For your teaching scheme, when you teach case2, it forgets case1. You need to feed all cases repeatedly in a common while loop until total error diminishes to a lower limit.

当您仅学习单个案例时,它会学得很好,但会忘记其他案例.因此,您一个接一个地喂食,然后将一组案例的总误差(也许是平方误差的总和)小于公差.

When you make it learn only single case, it learns it good but forgets others. So you feed one after another and take a group of cases' total error (maybe sum of squared errors) to be smaller than a tolerance.

这篇关于XOR Java神经网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆