📄 bpnweightpack.java
字号:
//////////////////////////////////////////////////////////////////////////////////// // // 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 : BPNWeightPack.java//////////////////////////////////////////////////////////////////////// BPNWeightPack base class//////////////////////////////////////////////////////////////////////// Author: Patocchi L.// Date: 03.09.1996// Project: jaNet//// BPNWeightPack class group together all weights between two// BPNLayers. In methods there is the concept of upper and lower// parts, where upper is in direction to the input of the net and // lower is in direction of the output. Since we stored the// weights in a squared table (or double array) we defined the// X axis as the upper layer reference and the Y axis as the// lower layer reference.//// upper// +-------------------->X// | w . . . w// | 1,1 m,1// l | . . .// o | . . .// w | . . .// e | // r |// | w . . . w// v 1,n m,n// Y// ////////////////////////////////////////////////////////////////////// date who what// 03.09.1996 Patocchi L. creationpackage jaNet.backprop;import java.io.*;public class BPNWeightPack { public static final String version = "1.0beta1"; protected double table[][]; protected double copy[][] = null;; protected double deltaOne[][]; public int upperSize; // Table Width public int lowerSize; // Table Height; ////////////////////////////////////////////////////////////////////// Constructors//////////////////////////////////////////////////////////////////// public BPNWeightPack(){ this(1,1); } public BPNWeightPack(int upper,int lower){ setSize(upper,lower); } public BPNWeightPack(RandomAccessFile raf) throws BPNException{ readFromFile(raf); }////////////////////////////////////////////////////////////////////// initialisers//////////////////////////////////////////////////////////////////// public void randomize(double value){ if(value<0.0) value *= (-1.0); randomize(value*(-1.0),value); } public void randomize(double min, double max){ for(int x=0; x<upperSize; x++) for(int y=0; y<lowerSize; y++) table[x][y] = Math.random()*(max-min) - min; }////////////////////////////////////////////////////////////////////// parameter-providers//////////////////////////////////////////////////////////////////// public double getW(int upper, int lower){ return table[upper][lower]; } public double[] getLowerNeuronConnectedDeltas(int pos) throws BPNException{ if(pos<0 || pos >= lowerSize) throw new BPNException("BPNWeightPack: Error in getLowerNeuronConnectedWeigths, <pos>("+pos+") is out of bounds."); double orizVector[] = new double[upperSize]; for(int i=0; i< upperSize; i++) orizVector[i] = deltaOne[i][pos]; return orizVector; } public double[] getLowerNeuronConnectedWeights(int pos) throws BPNException{ if(pos<0 || pos >= lowerSize) throw new BPNException("BPNWeightPack: Error in getLowerNeuronConnectedWeigths, <pos>("+pos+") is out of bounds."); double orizVector[] = new double[upperSize]; for(int i=0; i< upperSize; i++) orizVector[i] = table[i][pos]; return orizVector; } public double[] getUpperNeuronConnectedWeights(int pos) throws BPNException{ if(pos<0 || pos >= upperSize) throw new BPNException("BPNWeightPack: Error in getUpperNeuronConnectedWeigths, <pos>("+pos+") is out of bounds."); double vertVector[] = new double[lowerSize]; for(int i=0; i< lowerSize; i++) vertVector[i] = table[pos][i]; return vertVector; }////////////////////////////////////////////////////////////////////// parameter-modifiers//////////////////////////////////////////////////////////////////// public void setSize(int upper,int lower){ upperSize = upper; lowerSize = lower; table = new double[upper][lower]; deltaOne = new double[upper][lower]; copy = null; randomize(0.1); } public void setW(int upper, int lower, double value){ table[upper][lower] = value; } public void setLowerNeuronConnectedWeights(int pos, double newWeights[]) throws BPNException{ if(newWeights == null) throw new BPNException("BPNWeightPack: Error in setLowerNeuronConnectedWeigths, newWeights is null."); if(newWeights.length != upperSize) throw new BPNException("BPNWeightPack: Error in setLowerNeuronConnectedWeigths, newWeights length doesn't match upperSize."); if(pos<0 || pos >= lowerSize) throw new BPNException("BPNWeightPack: Error in setLowerNeuronConnectedWeigths, <pos>("+pos+") is out of bounds."); for(int i=0; i< upperSize; i++){ deltaOne[i][pos] = newWeights[i] - table[i][pos]; table[i][pos] = newWeights[i]; } } public void setCopy(){ if(copy == null) copy = new double[upperSize][lowerSize]; //System.out.println("start of copy"); for(int i=0; i<upperSize; i++) System.arraycopy(table[i],0,copy[i],0,lowerSize); //System.out.println("end of copy"); } public void restoreCopy(){ if(copy == null)return; for(int i=0; i<upperSize; i++){ System.arraycopy(copy[i],0,table[i],0,lowerSize); } } ////////////////////////////////////////////////////////////////////// I/O ports//////////////////////////////////////////////////////////////////// public void writeToFile(RandomAccessFile raf) throws BPNException{ try{ raf.writeUTF(getClass().getName()+version); raf.writeInt(upperSize); raf.writeInt(lowerSize); for(int x=0; x<upperSize; x++) for(int y=0; y<lowerSize; y++) raf.writeDouble(table[x][y]); }catch(IOException ioe){ throw new BPNException("BPNWeightPack: Error in writeToFile,("+ioe+")."); } } public void readFromFile(RandomAccessFile raf) throws BPNException{ int upper, lower; try{ if(raf.readUTF().compareTo(getClass().getName()+version) != 0){ throw new BPNException("BPNWeightPack: Error in readFromFile, unknown version."); } // setup size of tables in temporary variables upper = raf.readInt(); lower = raf.readInt(); double newTable[][] = new double[upper][lower]; // read content and stores values for(int x=0; x<upper; x++) for(int y=0; y<lower; y++) newTable[x][y] = raf.readDouble(); // OK all readings were correct now update BPNWeightPack values. upperSize = upper; lowerSize = lower; table = newTable; deltaOne = new double[upper][lower]; }catch(IOException ioe){ throw new BPNException("BPNWeightPack: Error in readFromFile,("+ioe+")."); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -