📄 bpn.java
字号:
outputL.setLearningRate(learningRate); outputL.setMomentum(momentum); }else{ /////////////////////////////////////// // define hidden layer /////////////////////////////////////// hiddenL = hiddenLayer.readFromFile(file); allLayers[i] = (BPNLayer) hiddenL; // attach upper BPNWeightPack hiddenL.setUpperWeightPack(weightP); // create lower BPNWeightPack weightP = new BPNWeightPack(file); allWeights[i] = weightP; // attach lower BPNWeightPack hiddenL.setLowerWeightPack(weightP); // attach upper layer hiddenL.setUpperLayer(allLayers[i-1]); // attach upper layer to this layer allLayers[i-1].setLowerLayer(allLayers[i]); // define which kind of layer is this. hiddenL.setLayerKind(BPNLayer.HIDDEN); // setup neural net parameters hiddenL.setLearningRate(learningRate); hiddenL.setMomentum(momentum); } } }catch(IOException ioe){ throw new BPNException("BPN: Error in buildStructure,("+ioe+")."); } }////////////////////////////////////////////////////////////////////// set-up the activating functions for layers//////////////////////////////////////////////////////////////////// private void setActivationFn(String layerActivatingFn[]) throws BPNException{ if(allLayers == null) throw new BPNException("BPN: Error in setActivationFn, no network structure."); for(int i=1; i<allLayers.length; i++) setActivationFn(layerActivatingFn[i-1], i); } public void setActivationFn(String layerActivatingFn, int pos) throws BPNException{ if(allLayers == null) throw new BPNException("BPN: Error in setActivationFn, no network structure."); if(pos<1 || pos >= allLayers.length) throw new BPNException("BPN: Error in setActivationFn, invalid position."); allLayers[pos].setActivationFnClass(layerActivatingFn); }////////////////////////////////////////////////////////////////////// initialisers//////////////////////////////////////////////////////////////////// public void randomize() throws BPNException{ randomize(initMin, initMax); } public void randomize(double value) throws BPNException{ if(value<0.0) value *= (-1.0); randomize(value*(-1.0),value); } public void randomize(double min, double max) throws BPNException{ if(allWeights == null) throw new BPNException("BPN: Error in randomize, no network structure."); for(int i=0; i<allWeights.length; i++) allWeights[i].randomize(min, max); }////////////////////////////////////////////////////////////////////// Backpropagation neural network functionalities//////////////////////////////////////////////////////////////////// public void propagate() throws BPNException{ if(inputL == null) throw new BPNException("BPN: Error in propagate, no network structure."); inputL.propagate(); } public void propagate(double vector[]) throws BPNException{ if(inputL == null) throw new BPNException("BPN: Error in propagate, no network structure."); inputL.propagate(vector); } public double[] getOutputVector() throws BPNException{ if(outputL == null) throw new BPNException("BPN: Error in getOutputVector, no network structure."); return outputL.getVector(); } public void learn(double input[], double target[]) throws BPNException{ outputL.learn(input, target); }////////////////////////////////////////////////////////////////////// SET's//////////////////////////////////////////////////////////////////// public void setLearningRate(double val){ learningRate = val; for(int i=1; i<allLayers.length; i++) allLayers[i].setLearningRate(val); } public void setMomentum(double val){ momentum = val; for(int i=1; i<allLayers.length; i++) allLayers[i].setMomentum(val); } public void setInitMin(double val){ initMin = val; } public void setInitMax(double val){ initMax = val; } public void setBias(int layer, double value) throws BPNException{ if(layer<0 || layer>allLayers.length-1) throw new BPNException("BPN: Error in setBias, layer index is out of bounds"); allLayers[layer].setBias(value); } public void setActivationFnClass(int layer, String ActFn) throws BPNException{ if(layer<0 || layer>allLayers.length-1) throw new BPNException("BPN: Error in setActivationFnClass, layer index is out of bounds"); allLayers[layer].setActivationFnClass(ActFn); } public void setWeightCopy(){ for(int i=0; i<allWeights.length; i++){ allWeights[i].setCopy(); } } public void restoreWeightCopy(){ for(int i=0; i<allWeights.length; i++) allWeights[i].restoreCopy(); }////////////////////////////////////////////////////////////////////// GET's//////////////////////////////////////////////////////////////////// public double getLearningRate(){ return learningRate; } public double getMomentum(){ return momentum; } public double getError(){ return outputL.getError(); } public double getError(double vector[], double target[]) throws BPNException{ return outputL.getError(vector, target); } public double getInitMin(){ return initMin; } public double getInitMax(){ return initMax; } public BPNdescriptor getBPNdescriptor(){ return new BPNdescriptor(allLayers); } //public BPNWeightPack[] getAllWeights(){ // return allWeights; //}////////////////////////////////////////////////////////////////////// I/O functionalities//////////////////////////////////////////////////////////////////// public void saveNeuralNetwork(String path, String name) throws BPNException{ if(name == null || path == null) throw new BPNException("BPN: Error in saveNeuralNetwork, path or name are null."); try{ File rawfile = new File(path, name); try{ RandomAccessFile file = new RandomAccessFile(rawfile,"rw"); saveNeuralNetwork(file); file.close(); }catch(IllegalArgumentException iae){ throw new BPNException("BPN: Error in saveNeuralNetwork,"+iae); } } catch(IOException ioe){ throw new BPNException("BPN: Error in saveNeuralNetwork,"+ioe); } catch(SecurityException se){ throw new BPNException("BPN: Error in saveNeuralNetwork,"+se); } } public void saveNeuralNetwork(RandomAccessFile file) throws IOException,BPNException{ file.writeUTF(""+getClass().getName()+version); // write network basic data file.writeDouble(learningRate); file.writeDouble(momentum); file.writeDouble(initMin); file.writeDouble(initMax); // write the number of layers we own file.writeInt(allLayers.length); // for each layer write .... for(int i=0;i<allLayers.length;i++){ // write himself on file allLayers[i].writeToFile(file); // the lower weight pack if(i != allLayers.length-1) allLayers[i].getLowerWeightPack().writeToFile(file); } } public void loadNeuralNetwork(String path, String name) throws BPNException{ if(name == null || path == null) throw new BPNException("BPN: Error in loadNeuralNetwork, path or name are null."); try{ File rawfile = new File(path, name); try{ RandomAccessFile file = new RandomAccessFile(rawfile,"r"); loadNeuralNetwork(file); file.close(); }catch(IllegalArgumentException iae){ throw new BPNException("BPN: Error in loadNeuralNetwork,"+iae); } } catch(IOException ioe){ throw new BPNException("BPN: Error in loadNeuralNetwork,"+ioe); } catch(SecurityException se){ throw new BPNException("BPN: Error in loadNeuralNetwork,"+se); } } public void loadNeuralNetwork(RandomAccessFile file) throws BPNException{ buildStructure(file); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -