sandeshMC

ErrorBackPropagation

Apr 15th, 2016
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.43 KB | None | 0 0
  1. package faceplus;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.File;
  5. import java.io.FileNotFoundException;
  6. import java.io.FileReader;
  7. import java.util.Arrays;
  8. import java.util.Random;
  9. import java.util.Scanner;
  10.  
  11. /**
  12.  *
  13.  * @author sandesh
  14.  */
  15. class Sigmoid {
  16.  
  17.     public static double output(double x) {
  18.         return 1.0 / (1.0 + Math.exp(-x));
  19.     }
  20.  
  21.     public static double derivative(double x) {
  22.         return x * (1 - x);
  23.     }
  24. }
  25.  
  26. class Neuron {
  27.  
  28.     static double lr = 0.3;
  29.     static int len;
  30.  
  31.     Neuron(int x) {
  32.         len = x;
  33.     }
  34.  
  35.     Neuron() {
  36.  
  37.     }
  38.  
  39.     public double[] inputs = new double[len];
  40.     public double[] weights = new double[len];
  41.     public double error;
  42.  
  43.     private double biasWeight;
  44.  
  45.     //private Random r = new Random();
  46.     public double output() {
  47.  
  48.         double outSignal = 0;
  49.         for (int i = 0; i < weights.length; i++) {
  50.             outSignal += weights[i] * inputs[i];
  51.         }
  52.         return Sigmoid.output(outSignal + biasWeight);
  53.     }
  54.  
  55.     public void randomizeWeights() {
  56.         for (int i = 0; i < weights.length; i++) {
  57.             weights[i] = Math.random();
  58.         }
  59.         biasWeight = Math.random();
  60.     }
  61.  
  62.     public void adjustWeights() {
  63.         for (int i = 0; i < weights.length; i++) {
  64.             double ed = 0;
  65.             for (int j = 0; j < inputs.length; j++) {
  66.                 ed += error * inputs[i];
  67.             }
  68.             weights[i] += ed * lr;
  69.         }
  70.         // weights[0] += (error * inputs[0]);
  71.         // weights[1] += (error * inputs[1]);
  72.         biasWeight += error;
  73.     }
  74. }
  75.  
  76. public class ErrorBackProp {
  77.    // public static int inputLength = 0;
  78.  
  79.     static Neuron hiddenNeuron[];
  80.  
  81.     static void createNeuron(int n, int x) {
  82.         new Neuron(x);
  83.         hiddenNeuron = new Neuron[n];
  84.         for (int i = 0; i < n; i++) {
  85.             hiddenNeuron[i] = new Neuron();
  86.         }
  87.     }
  88.  
  89.     static void randomWeightsForNeurons() {
  90.  
  91.         for (int i = 0; i < hiddenNeuron.length; i++) {
  92.             hiddenNeuron[i].randomizeWeights();
  93.         }
  94.     }
  95.  
  96.     static void train() throws Exception {
  97.          double inputs[][] = {{0, 0}, {0, 1}, {1, 0}, {1, 1}};
  98.         double[] results = {0, 1, 1, 0};
  99.         /*double inputs[][] = new double[14][166];
  100.         try (BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\sandesh\\Documents\\NetBeansProjects\\FacePlus\\src\\faceplus\\dataset.txt"))) {
  101.         String line;
  102.         int i=0;
  103.         while ((line = br.readLine()) != null) {
  104.         String numbers[] = line.split(",");
  105.         for(int x = 0 ; x < numbers.length ; x ++) {
  106.             inputs[i][x] = Double.parseDouble(numbers[x]);
  107.         }
  108.         i++;
  109.     }
  110. }
  111.        
  112.         for(int p = 0 ; p < inputs.length ; p ++) {
  113.        
  114.         System.out.println(Arrays.toString(inputs[p]));
  115.         }*/
  116.  
  117.        
  118.         // the input values
  119.       //  double[] results = {0.1, 0.99, 0.1, 0.99, 0.1, 0.99, 0.1, 0.99, 0.1, 0.99, 0.1, 0.99, 0.1, 0.99};
  120.         int inputLength = inputs[0].length;
  121.         createNeuron(2, inputLength);
  122.         randomWeightsForNeurons();
  123.         // desired results
  124.         // creating the neurons
  125.         // Neuron hiddenNeuron1 = new Neuron();
  126.         //  Neuron hiddenNeuron2 = new Neuron();
  127.         Neuron outputNeuron = new Neuron();
  128.  
  129.         // random weights
  130.         //hiddenNeuron1.randomizeWeights();
  131.         // hiddenNeuron2.randomizeWeights();
  132.         outputNeuron.randomizeWeights();
  133.  
  134.         int iter = 0;
  135.  
  136.         while (iter != 100000) {
  137.             double error1 = 0 ;
  138.             for (int i = 0; i < 4; i++) {
  139.                
  140.                 for (int j = 0; j < hiddenNeuron.length; j++) {
  141.                     hiddenNeuron[j].inputs = inputs[i];
  142.                 }
  143.                 //   hiddenNeuron1.inputs = inputs[i];
  144.                 //   hiddenNeuron2.inputs = inputs[i];
  145.                 for (int j = 0; j < outputNeuron.inputs.length; j++) {
  146.                     outputNeuron.inputs[j] = hiddenNeuron[j].output();
  147.                 }
  148.                 //outputNeuron.inputs[0] = hiddenNeuron1.output();
  149.                 // outputNeuron.inputs[1] = hiddenNeuron2.output();
  150.                 System.out.println("Epoch No. "+iter);
  151.                 System.out.println("Output for "+i+" is " + outputNeuron.output());
  152.                 outputNeuron.error = Sigmoid.derivative(outputNeuron.output()) * (results[i] - outputNeuron.output());
  153.                 error1 += (results[i] - outputNeuron.output())*(results[i] - outputNeuron.output());
  154.                 outputNeuron.adjustWeights();
  155.                 for (int j = 0; j < hiddenNeuron.length; j++) {
  156.                     hiddenNeuron[j].error = Sigmoid.derivative(hiddenNeuron[j].output()) * outputNeuron.error * outputNeuron.weights[j];
  157.                 }
  158.                 //hiddenNeuron1.error = Sigmoid.derivative(hiddenNeuron1.output()) * outputNeuron.error * outputNeuron.weights[0];
  159.                 //hiddenNeuron2.error = Sigmoid.derivative(hiddenNeuron2.output()) * outputNeuron.error * outputNeuron.weights[1];
  160.  
  161.                 for (int j = 0; j < hiddenNeuron.length; j++) {
  162.                     hiddenNeuron[j].adjustWeights();
  163.                 }
  164.  
  165.             }
  166.             double MSE = (1.00/56.00)*error1;
  167.             System.out.println("MSE: "+MSE);
  168.             iter++;
  169.         }
  170.     }
  171.  
  172.     public static void main(String args[]) throws Exception {
  173.         train();
  174.     }
  175.  
  176. }
Add Comment
Please, Sign In to add comment