daily pastebin goal
57%
SHARE
TWEET

Untitled

a guest Jul 19th, 2018 73 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package feedForward1;
  2.  
  3. import java.util.LinkedList;
  4.  
  5. public class Neuron {
  6.    
  7.     private double input, output, error, delta;
  8.    
  9.     private LinkedList<Double> inputs;
  10.    
  11.     private LinkedList<Double> weights;
  12.    
  13.     public Neuron() {
  14.         weights = new LinkedList<Double>();
  15.         inputs = new LinkedList<Double>();
  16.     }
  17.    
  18.     public double getInput() {
  19.         return input;
  20.     }
  21.    
  22.     public double getOutput() {
  23.         return output;
  24.     }
  25.    
  26.     public double getError() {
  27.         return error;
  28.     }
  29.    
  30.     public double activate(boolean derivative, double i) {
  31.         if(derivative)
  32.             return i*(1.0-i);
  33.         else
  34.             return activate(i);
  35.        
  36.     }
  37.     public double activate(double i) {
  38.         output =  1.0 / (1.0 + Math.exp(-1.0 * i));
  39.         return output;
  40.     }
  41.    
  42.     public double activate() {
  43.         return activate(input);
  44.     }
  45.    
  46.     public double getWeightFor(int i) {
  47.         return weights.get(i);
  48.     }
  49.    
  50.     public void setWeightFor(int i, double d) {
  51.         weights.set(i, d);
  52.     }
  53.    
  54.     public void createWeight(double d) {
  55.         weights.add(d);
  56.     }
  57.    
  58.     public void incrementWeight(int i, double d) {
  59.         double temp = getWeightFor(i);
  60.         temp -= d;
  61.         setWeightFor(i,temp);
  62.     }
  63.    
  64.     public void incrementInput(double d) {
  65.         inputs.add(d);
  66.         input += d;
  67.     }
  68.    
  69.     public void setInput(double d) {
  70.         input = d;
  71.     }
  72.    
  73.     public int getWeightSize() {
  74.         return weights.size();
  75.     }
  76.    
  77.     public double getDelta() {
  78.         return delta;
  79.     }
  80.  
  81.     public void updateWeight(double rate, Layer nextLayer) {
  82.         if(nextLayer==null) return;
  83.         for(int i = 0; i < weights.size(); i++) {
  84.             incrementWeight(i, nextLayer.getNeuronAt(i).getDelta()*output*rate);
  85.         }
  86.     }
  87.    
  88.    
  89.     //For output layer
  90.     public void calcDelta(Layer layer, double ideal) {
  91.         activate();
  92.         error = Math.abs(ideal-output);
  93.         error = Math.pow(error, 2);
  94.         delta = BoundNumber.bound(error*activate(true,input));
  95.     }
  96.    
  97.     //For hidden layer
  98.     public void calcDelta(Layer layer, Layer nextLayer) {
  99.             //For each weight
  100.             double sumDelta = 0.0;
  101.             for(int i = 0; i < weights.size(); i++)
  102.                 sumDelta += nextLayer.getNeuronAt(i).getDelta()*getWeightFor(i);
  103.             delta = BoundNumber.bound(activate(true,input)*sumDelta);
  104.     }
  105.    
  106.    
  107.     public void clearInput() {
  108.         input = 0;
  109.         inputs.clear();
  110.     }
  111.  
  112. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top