// Back-Propagation Neural Network // // Written in Python. See http://www.python.org/ // Placed in the public domain. // Neil Schemenauer // Translated to Java by: // Jose Antonio Martin H. import java.util.Random; import java.io.*; //import string //random.seed(0) public class bpnn implements Serializable { static Random generator = new Random(); // Vars int ni; int nh; int no; double N,M; double[] ai; double[] ah; double[] ao; public double[][] wi; public double[][] wo; double[][] ci; double[][] co; public static class pattern { double[] input; double[] target; pattern(double[] in,double[] out) { input=in; target=out; } } // calculate a random number where: a <= rand < b public static double rand(double a, double b) { double retval; retval=(b-a)*generator.nextDouble() + a; return retval; } // Make a matrix public static double[][] makeMatrix(int I, int J) { double [][] m = new double[I][J]; return m; } public static double tanh(double x) { return (Math.exp(x)- Math.exp(-x)) / (Math.exp(x)+Math.exp(-x)); } // our sigmoid function, tanh is a little nicer than the standard 1/(1+e^-x) public static double sigmoid(double x) { return tanh(x); } // derivative of our sigmoid function public static double dsigmoid(double y) { return 1.0-y*y; } bpnn(int ini,int inh, int ino) { int i,j; //# number of input, hidden, and output nodes ni = ini + 1; // +1 for bias node nh = inh; no = ino; ai= new double[ni]; ah= new double[nh]; ao= new double[no]; N=0.5;// N: learning rate 0.5 good; M=0.1;// M: momentum factor 0.1 good //# init activations for nodes for(i=0;i