/************************************************************ ; Backpropagation with momentum * ; by Andres Perez-Uribe * ; HEIG-VD March/2008 * ; * ; Original version: * ; C code by A. Perez-Uribe * ; Universidad del Valle, Cali, Colombia * ; sep/93 * ; * ; Email : andres DOT perez-uribe AT heig-vd.ch * ; REDS Institute @ HEIG-VD * ; University of Applied Sciences of Western * ; Switzerland (HES-SO) * ;************************************************************ References : - G. Hinton, "How neural networks learn from experience", Scientific American, sep 1992. - P. Werbos, "The Roots of Backpropagation: From ordered derivatives to Neural Neworks and Political Forecasting", John Wiley and Sons, New York, 1994 Compile : javac Bkprop.java Run : java Bkprop There is no guarantee that the code will do what you expect or that it is error free. It is simply meant to provide a useful way to experiment with the Backpropagation learning algorithm. Update Oct 7/99...thanks to Stephane Pouyet Last update March/2007 ... change of variable names for the sake of clarity The original names came from Hinton's paper. First Java adaptation by Christophe Scalfo, HEIG-VD, September/2007 Second Java version by Andres Perez-Uribe, HEIG-VD, March/2008 */ import java.io.*; public class Bkprop { final String trainfile = "digits.txt"; final String testfile = "digits.txt"; final int INPUTS = 35; /* number if inputs */ final int HIDDEN = 5; /* number of hidden units */ final int OUTPUTS = 10; /* number of outputs */ final double EPSILON = 0.1; /* maximum Mean Square Error to stop training */ final int NUMTRAIN = 18; /* number of training patterns */ final int NUMTEST = 18; /* number of test patterns */ /* synaptic weights */ private double[][] inhiddw = new double[INPUTS][HIDDEN]; private double[][] hidoutw = new double[HIDDEN][OUTPUTS]; /* delta_weights memorization for Backprop with momentum */ private double[][] deltaihw = new double[INPUTS][HIDDEN]; private double[][] deltahow = new double[HIDDEN][OUTPUTS]; /* input, hidden and output vector values */ private double[] x= new double[INPUTS]; private double[] y= new double[HIDDEN]; private double[] z= new double[OUTPUTS]; //Sigmoid activation function public double sigm(double x){ return 1/(1 + Math.exp(-x)); } //Derivative of the sigmoid activation function public double dxsigm(double y){ return (y)*(1-y); } /* training patterns */ private double[][] patterns = new double[NUMTRAIN][INPUTS]; /* desired outputs */ private double[][] desout= new double[NUMTRAIN][OUTPUTS]; /* delta error of the hidden neurons */ private double[] ehid = new double[HIDDEN]; /* delta error of the output neurons */ private double[] eout = new double[OUTPUTS]; /* learned patterns */ private boolean[] lrnpatr = new boolean[NUMTRAIN]; /* squared mean error */ private double[] sme = new double[NUMTRAIN]; private double delta = 0.5; /* learning rate */ private double alfa = 0.1; /* momentum */ private double[][] matrizin = new double[NUMTEST][INPUTS]; private int output; //Loading a training database public void read_data(){ BufferedReader lecteurAvecBuffer = null; String ligne; String[] trame; try { lecteurAvecBuffer = new BufferedReader(new FileReader(trainfile)); } catch(FileNotFoundException exc) { System.out.println("Error loading the training database"); } try { for(int i =0; i