hiddenlayer.java
来自「JaNet: Java Neural Network Toolkit res」· Java 代码 · 共 363 行
JAVA
363 行
//////////////////////////////////////////////////////////////////////////////////// // // Copyright (C) 1996 L. Patocchi & W.Gander//// This program is free software; you can redistribute it and/or modify it// under the terms of the GNU General Public License as published by the Free// Software Foundation; either version 2 of the License, or (at your option)// any later version.//// This program is distributed in the hope that it will be useful, but WITHOUT// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for// more details.//// You should have received a copy of the GNU General Public License along with// this program; if not, write to the Free Software Foundation, Inc., 675 Mass// Ave, Cambridge, MA 02139, USA.//// Contacts:// // Project Supervisor// W.Hett hew@info.isbiel.ch// // Authors// W.Gander gandw@info.isbiel.ch// L.Patocchi patol@info.isbiel.ch//// Documentation can be found at://// http://www.isbiel.ch/Projects/janet/index.html////////////////////////////////////////////////////////////////////////////////////////// File : hiddenLayer.java//////////////////////////////////////////////////////////////////////// hiddenLayer extending class//////////////////////////////////////////////////////////////////////// Author: Patocchi L.// Date: 03.09.1996// Project: jaNet//// hiddenLayer is the extended BPNLayer class for a BackPropagation // hidden layer.// ////////////////////////////////////////////////////////////////////// date who what// 03.09.1996 Patocchi L. creationpackage jaNet.backprop;import java.io.*;public class hiddenLayer extends BPNLayer{ // those 3 layer-length arrays keep internal values for each unit. protected double sum[]; // sum of x(i)*w(i,j) for unit j in this lajer protected double delta[]; // delta or error for each unit in this layer protected double bias[]; // bias for each unit in this layer // upper and lower layer and weight packs. protected BPNLayer upperLayer; protected BPNLayer lowerLayer; protected BPNWeightPack upperWeights; protected BPNWeightPack lowerWeights; // the activation function of this layer named by fnClassName is referenced by aFn. protected static activationFn aFn; protected String fnClassName; // neural nets functional variables; protected double learningRate; protected double momentum; // debugging private static final boolean trace = false;////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////// public hiddenLayer(){ this(1); } public hiddenLayer(int n){ if(trace)System.out.println("hiddenLayer: Trace in constructor with "+n+" units."); setSize(n); }////////////////////////////////////////////////////////////////////// initialisers//////////////////////////////////////////////////////////////////// public void reset(){ reset(0.0); } public void reset(double x){ if(trace)System.out.println("hiddenLayer: Trace in reset "+sum+" "+delta); for(int i=0; i<vector.length; i++){ sum[i] = x; delta[i] = x; } }////////////////////////////////////////////////////////////////////// parameter-providers//////////////////////////////////////////////////////////////////// public double[] getDelta(){ double newDelta[] = new double[vector.length]; for(int i=0; i<vector.length; i++) newDelta[i] = delta[i]; return newDelta; } public double[] getBias(){ double newBias[] = new double[vector.length]; for(int i=0; i<vector.length; i++)newBias[i] = bias[i]; return newBias; } public double getDelta(int x) throws BPNException{ if(x <0 || x>=vector.length) throw new BPNException("hiddenLayer: Error in getDelta(x), <x> under/overflow layer size."); return delta[x]; } public double getBias(int x) throws BPNException{ if(x <0 || x>=vector.length) throw new BPNException("hiddenLayer: Error in getBias(x), <x> under/overflow layer size."); return bias[x]; } public double[] getDelta(int From, int To) throws BPNException{ if(From > To) throw new BPNException("hiddenLayer: Error in getDelta(From, To), <From> greather than <To>."); if(From <0 || To>=vector.length) throw new BPNException("hiddenLayer: Error in getDelta(From, To), <From> or <To> under/overflow layer size."); double newDelta[] = new double[To - From+1]; for(int i=From; i<=To; i++)newDelta[i-From] = delta[i]; return newDelta; } public double[] getBias(int From, int To) throws BPNException{ if(From > To) throw new BPNException("hiddenLayer: Error in getBias(From, To), <From> greather than <To>."); if(From <0 || To>=vector.length) throw new BPNException("hiddenLayer: Error in getBias(From, To), <From> or <To> under/overflow layer size."); double newBias[] = new double[To - From+1]; for(int i=From; i<=To; i++)newBias[i-From] = bias[i]; return newBias; } public double getLearningRate(){ return learningRate; } public double getMomentum(){ return momentum; }////////////////////////////////////////////////////////////////////// parameter-modifiers//////////////////////////////////////////////////////////////////// public void setSize(int size){ if(trace)System.out.println("hiddenLayer: Trace in setSize("+size+")."); super.setSize(size); delta = new double[size]; sum = new double[size]; bias = new double[size]; //reset(); } public void setBias(double[] b) throws BPNException{ if(b == null) throw new BPNException("hiddenLayer: Error in setBias(vector), <vector> is null."); if(b.length!=vector.length) throw new BPNException("hiddenLayer: Error in setBias(vector), <vector> length don't match layer length."); for(int i=0; i<b.length; i++) bias[i] = b[i]; } public void setBias(double b){ for(int i=0; i<bias.length; i++) bias[i] = b; } public void setBias(double v, int at) throws BPNException{ double x[] = new double[1]; x[0] = v; setBias(x, at); x = null; } public void setBias(double[] v, int at) throws BPNException{ if(v == null) throw new BPNException("hiddenLayer: Error in setBias(vector, at), <vector> is null."); if(at<0) throw new BPNException("hiddenLayer: Error in setBias(vector,at), <at> is negative."); if(v.length+at>vector.length) throw new BPNException("hiddenLayer: Error in setBias(vector,at), <at> + <vector> length overflow layer length."); // insert the array 'v' at given position 'at' for(int i=at; i<v.length+at; i++) bias[i] = v[i-at]; } public void setLearningRate(double val){ learningRate = val; } public void setMomentum(double val){ momentum = val; }////////////////////////////////////////////////////////////////////// Plug-in's (activating function, layers, weights pack)//////////////////////////////////////////////////////////////////// public void setActivationFnClass(String fnclassname) throws BPNException{ activationFn aFnTemp; try{ // try to load and create a new instance of a class which the name is specified in fnclassname string. aFnTemp = (activationFn) Class.forName(fnclassname).newInstance(); }catch (InstantiationException instex){ throw new BPNException("hiddenLayer: Error in instantiation activating fn class ("+instex+")."); }catch (IllegalAccessException illaccex){ throw new BPNException("hiddenLayer: Error in accessing activating fn class ("+illaccex+")."); }catch (ClassNotFoundException classnotfound){ throw new BPNException("hiddenLayer: Error, activating fn class '"+fnclassname+"' not found ("+classnotfound+")."); } fnClassName = fnclassname; aFn = aFnTemp; } public String getActivationFnClassName(){ return new String(fnClassName); } public void setUpperLayer(BPNLayer layer){ upperLayer = layer; } public BPNLayer getUpperLayer(){ return upperLayer; } public void setLowerLayer(BPNLayer layer){ lowerLayer = layer; } public BPNLayer getLowerLayer(){ return lowerLayer; } public void setUpperWeightPack(BPNWeightPack uwp){ upperWeights = uwp; } public BPNWeightPack getUpperWeightPack(){ return upperWeights; } public void setLowerWeightPack(BPNWeightPack uwp){ lowerWeights = uwp; } public BPNWeightPack getLowerWeightPack(){ return lowerWeights; }////////////////////////////////////////////////////////////////////// I/O ports//////////////////////////////////////////////////////////////////// public void writeToFile(RandomAccessFile raf) throws BPNException{ try{ raf.writeUTF(getClass().getName()+version); int size = getSize(); raf.writeInt(size); for(int i=0; i<size; i++) raf.writeDouble(bias[i]); raf.writeUTF(fnClassName); }catch(IOException ioe){ throw new BPNException("hiddenLayer: Error in writeToFile,("+ioe+")."); } } public static hiddenLayer readFromFile(RandomAccessFile raf) throws BPNException{ int upper, lower; hiddenLayer temp = new hiddenLayer(); try{ if(raf.readUTF().compareTo(temp.getClass().getName()+version) != 0){ throw new BPNException("hiddenLayer: Error in readFromFile, unknown version."); } // setup size of tables in temporary variables int size = raf.readInt(); temp = new hiddenLayer(size); double newBias[] = new double[size]; // read content and stores values for(int i=0; i<size; i++) newBias[i] = raf.readDouble(); temp.setBias(newBias); temp.setActivationFnClass(raf.readUTF()); }catch(IOException ioe){ throw new BPNException("hiddenLayer: Error in readFromFile,("+ioe+")."); } return temp; }////////////////////////////////////////////////////////////////////// neural network hidden layer functionalities//////////////////////////////////////////////////////////////////// public void propagate() throws BPNException{ if(aFn == null) throw new BPNException("hiddenLayer: Error in ("+this+"), activation Fn is not defined."); // lower neuron connected weights array double weightsToUpper[] = null; // get upper layer values double upperVector[] = upperLayer.getVector(); // may be possible to parallelize this in future ? for(int i=0; i<vector.length; i++){ // get weigths that connect neuron i to upper neurons try{ weightsToUpper = upperWeights.getLowerNeuronConnectedWeights(i); }catch(BPNException bpne){ throw new BPNException("GENERAL PROGRAM ERROR in hiddenLayer.propagate: Please contact authors, Thank-You.\n("+bpne+")"); } sum[i] = 0.0; // perform the sum of all upper units times weigth that connect this unit for(int j=0; j<upperVector.length; j++){ sum[i] += upperVector[j] * weightsToUpper[j]; //System.out.print(" u("+upperVector[j]+") w("+weightsToUpper[j]+")"); } //System.out.println(" unit"+i); // update this neuron output value vector[i] = aFn.activation(sum[i]) + bias[i]; } // propagate to next layer lowerLayer.propagate(); } public void learn() throws BPNException{ double upperVector[] = null; double weightsToUpper[] = null; double deltaWeightsToUpper[] = null; double weightsToLower[] = null; double lowerDelta[] = null; // get upperLayer vector; upperVector = upperLayer.getVector(); lowerDelta = lowerLayer.getDelta(); // modify outputLayer upperWeights // this 'for' statement is hightly parallelisable for(int i=0; i<vector.length; i++){ // get weigths that connect neuron i to other Layers try{ weightsToUpper = upperWeights.getLowerNeuronConnectedWeights(i); deltaWeightsToUpper = upperWeights.getLowerNeuronConnectedDeltas(i); weightsToLower = lowerWeights.getUpperNeuronConnectedWeights(i); }catch(BPNException bpne){ throw new BPNException("GENERAL PROGRAM ERROR in hiddenLayer.learn, getting weightsToUpper/Lower:"+ " Please contact the authors, Thank-You.\n("+bpne+")"); } // Update delta values for this layer delta[i] = 0.0; for(int n=0; n<lowerDelta.length; n++) delta[i] += lowerDelta[n] * weightsToLower[n]; delta[i] *= aFn.activation1stD(sum[i]); // update weights values for(int j=0; j<upperVector.length; j++){ double deltaW = deltaWeightsToUpper[j] * momentum + learningRate * upperVector[j] * delta[i]; weightsToUpper[j] += deltaW; //System.out.print(" to("+j+") D("+deltaW+")"); } //System.out.println(" from unit "+i); // restoring weights values in UpperWeights upperWeights.setLowerNeuronConnectedWeights(i, weightsToUpper); } // updated all now feedback to upperLayer upperLayer.learn(); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?