/** * I declare this source public domain knowledge. Use as you wish. */ /** * A simple multilayered perceptron. * * This is a feedforward neural network without much possibility * of customization. It can have any number of hidden layers with the * basic tanh activation function, and it always has a linear output * layer. * * Implementation note: Creating new objects during training is avoided by * pre-allocating working memory as object members during instantiation. * These work arrays are updated and used in all the key methods. * * @author nieminen@jyu.fi * */ public class SimpleMLP { /** Numbers of neurons on each layer; this describes the * neural architecture. Can be given on instantiation only. */ private int[] nneur; /** Weights reside in layerwise matrices. */ private double[][][] W; /** Space required for computing the gradient. */ private double[][][] G; /** Space required for computing one output vector */ private double[][] tmpout; /** Counter of optimization steps since random initialization of weights. */ private int iterSinceInit = 0; /** * Creates an MLP with the given architecture. * * @param numbersOfNeurons The number of neurons on each layer; * Must include input and output!! */ public SimpleMLP(int[] numbersOfNeurons){ nneur = numbersOfNeurons.clone(); /* Create the required matrices for weights, gradient, and computation */ W = new double[nneur.length-1][][]; G = new double[nneur.length-1][][]; tmpout = new double[nneur.length-1][]; for(int L=0;L= 0; L--){ double[] outPrevious = L>0 ? out[L-1]:input; if (L < nlayers-1) NeuralMath.tanhDerivTimesSubmatTVec(W[L+1], out[L+1], out[L]); NeuralMath.matPlusVecVecTwbias(G[L], out[L], outPrevious); } return costTerm; } /** Applies the network for one input vector. */ private double[] feedVector(double[] input){ double[][] out = tmpout; int nlayers = W.length; int L; /* Forward loop for layer outputs*/ double[] in; for (L=0, in = input; Lmaxt) maxt=targets[i];} /* Build binary vectors for internal use. */ double[][] targetvecs = new double[inputs.length][maxt]; for(int i=0;ivmax){vmax = outs[i][j]; imax=j;} } outclasses[i] = imax+1; // Begin class indices from 1. } return outclasses; } }