dialga5555

WIP Two Layer Neural Netowork Java Code

Mar 25th, 2018
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.61 KB | None | 0 0
  1. public class twoLayerNeuralNetwork
  2. {
  3.    public static void main(String[] args)
  4.    {
  5.    double inputs[][] =
  6.    {
  7.    {0,0,1},
  8.    {0,1,1},
  9.    {1,0,1},
  10.    {0,1,0},
  11.    {1,0,0},
  12.    {1,1,1},
  13.    {0,0,0}
  14.    };
  15.    //patter: XOR for first two inputs- third input is irrelevant
  16.    
  17.    double neurons[] = {0.1,0.9,0.1,0.9};
  18.    
  19.    double outputs[] = {0,1,1,1,1,0,0};
  20.    
  21.    double weights1[][] =
  22.    {
  23.    {0.3,4.5,-6},
  24.    {-8.75,0.2,-8.75},
  25.    {6.2,4.4,0},
  26.    {-0.5,0.08,-0.4}
  27.    };
  28.    
  29.    double weights2[] =
  30.    {0,0,0,0};
  31.    
  32.    for(int set=0; set<inputs.length; set++)
  33.    {
  34.    for(int neuron=0; neuron<weights1.length; neuron++)
  35.    {System.out.println("set: "+set+" neuron: "+neuron+" "+sum(weights1[neuron],inputs[set]));}
  36.    System.out.print("\n");}
  37.    }
  38.    
  39.    
  40.    
  41.    public static double[] train(double inputs[][], double weights1[][], double neurons[], double weights2[], double outputs[])
  42.    {
  43.    
  44.    }
  45.    
  46.    
  47.    public static double sum(double w[], double i[])
  48.    {
  49.    double sum = 0;
  50.    for(int p=0; p<w.length; p++)
  51.    {sum+=w[p]*i[p];}
  52.    return sum;
  53.    }
  54.      
  55.    public static double sigmoid(double s)
  56.    {
  57.    return (1/(1+Math.exp(-1*s)));
  58.    }
  59.    
  60.    public static double[] adjust(double inputs[], double outputs[], double weights[])
  61.    {
  62.    double testOutputs[] = new double[outputs.length];
  63.    for(int n=0; n<outputs.length; n++)
  64.    {testOutputs[n]=sigmoid(sum(weights,inputs));}
  65.    
  66.    for(int n=0; n<weights.length; n++)
  67.    {
  68.    double error = outputs[n]-testOutputs[n];
  69.    weights[n]+=(error*inputs[n]*testOutputs[n]*(1-testOutputs[n]));
  70.    }
  71.    return weights;
  72.    }
  73. }
Add Comment
Please, Sign In to add comment