Advertisement
naraku9333

Untitled

Nov 25th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.11 KB | None | 0 0
  1. public void backPropagate(double[] desiredOutput) {
  2.         if(desiredOutput.length != network.get(network.size()-1).size())
  3.             throw new IllegalArgumentException("Invalid number of outputs");
  4.        
  5.         double outputError = 0;
  6.        
  7.         //start at output layer
  8.         for(int i = network.size()-1; i > 0; --i) {
  9.             for(int j = 0; j < network.get(i).size(); ++j) {
  10.                 Neuron n = network.get(i).get(j);
  11.                
  12.                 //calculate errors
  13.                 if(i == network.size()-1) {//output neuron
  14.                     n.setError(activator.fprime(n.getValue()) * (desiredOutput[j] - n.getValue()));
  15.                     outputError += n.getError();
  16.                 }
  17.                 else {
  18.                     double w = 0;
  19.                     for(Link l : n.forward) { w += l.weight; }
  20.                     n.setError(activator.fprime(n.getValue()) * w * outputError);
  21.                 }
  22.                
  23.                 //adjust weights
  24.                 for(Link l : n.backward) { l.weight += n.getError() * l.prev.getValue(); }
  25.             }
  26.         }
  27.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement