Java神经网络XOR 1隐藏层

时间:2018-06-07 15:10:13

标签: java neural-network matrix-multiplication xor backpropagation

我正在尝试将神经网络编码为具有3个隐藏神经元的XOR门。 Java中的Netbeans IDE。我认为这是一个物流错误,代码当前如何处理矩阵乘法,特别是输入,但我似乎无法弄明白。任何帮助将不胜感激,谢谢。

public static void main(String[] args) {

    //input array
    int[][] inputs = {  {0,0},
                        {0,1},
                        {1,0},
                        {1,1}};

    //output array
    int[] outputs = {0, 1, 1, 0};

    //input weights randomly initialized
    double[] inputWeights = new double[6];
    for (int i = 0; i < inputWeights.length; i++) {
        inputWeights[i]=Math.random();
     System.out.println("Input Weight "+i+" is "+df.format(inputWeights[i]));
    }

    //Learning rate initialized
    final double RATE = 3.0;
    final double BIAS = 0.5;
    double OutputResult=0;
    double error =1;
    double[] hiddenResults = new double[3];
    double OutputSum=0;


    //hidden weights randomly initialized
    double[] hiddenWeights = new double[3];
    for (int i = 0; i < hiddenWeights.length; i++) {
        hiddenWeights[i]=Math.random();

    }
    //generation/iteration loop
    for (int g = 0; g < 10000; g++)
    {
        //input counter



    double[] hiddenSums = new double[3];
    //forward propagation
    for(int p=0; p < 4; p++){
    for (int i = 0; i < 3; i++) {
     hiddenSums[i] = (inputs[p][0]*inputWeights[i])+(inputs[p][1]*inputWeights[i+3]);

    }
    }


    for (int i = 0; i < 3; i++) {
            hiddenResults[i]=Application(hiddenSums[i]);

    }

    //calculate output sum using hidden weights and results

    for (int i = 0; i < 3; i++) {
        OutputSum+=hiddenResults[i]*hiddenWeights[i];
    }


    //find output error
    //will be replaced with loop value
    for(int p=0; p < 4; p++){
    double target = outputs[p];
     error = OutputResult-target;}

    //calculate Delta Output Sum
    double DOutputSum = (Application(OutputResult)*(1-Application(OutputResult))) * error;

    //populate new hidden weights
    double[] newHiddenWeights = new double[3];
    for (int i = 0; i < 3; i++) {
        newHiddenWeights[i]+=hiddenWeights[i]+(DOutputSum/hiddenResults[i]);

    }

    //calculate delta hidden sums
    double[] DHiddenSums =new double[3];
    for (int i = 0; i < 3; i++) {
      DHiddenSums[i]= ( DOutputSum/hiddenWeights[i])*(Application(hiddenSums[i]*(1-Application(hiddenSums[i]))));

    }

    //calculate change in input weights
    double[] DWeights = new double[6];
    for(int p=0; p < 4; p++){
    for (int i = 0; i < 6; i++) {
        //replace with BIG LOOP DO THIS LATER 
        DWeights[i]=DHiddenSums[i%3]*((inputs[p][i%2])*(RATE)-BIAS);}



         }

    //replace old weights with new weights
    for (int i = 0; i < 6; i++) {
        inputWeights[i]+=DWeights[i];


    }



    }
    //print final data
    double[] finalOutputs = new double[4];
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 3; j++) {
            finalOutputs[i]+=(hiddenResults[j]*hiddenWeights[j]);
                }
        finalOutputs[i]=Application(finalOutputs[i]);
        }

    for (int i = 0; i < 4; i++) {
        System.out.println(inputs[i][0]+" "+inputs[i][1]+" "+
        " : "+outputs[i]+" "+finalOutputs[i]);


    }



}

public static double Application(double s)
{
     double sigmoid = 1/(1+Math.exp(-s));
     double result = 0;
    if (s>1) 
        result=1;
    return sigmoid;

}

}

0 个答案:

没有答案