📄 learningset.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 : learningSet.java//package jaNet.backprop.gui;import java.io.*;import jaNet.backprop.*;public class learningSet{ public static final int block = 1000; public BPNpattern inputs[]; public BPNpattern targets[]; private int size; private double minVal, maxVal; public learningSet(String path, String file, int inSize, int outSize, double minDomain, double maxDomain) throws BPNException{ FileInputStream in; double bigBuffer[] = new double[block]; double tempBuf[]; // keep these values for future rescales... minVal = minDomain; maxVal = maxDomain; // set extreme values double max = -1.79769e+308; double min = +1.79769e+308; int count = 0; try{ in = new FileInputStream(new File(path, file)); }catch(FileNotFoundException fnfe){ throw new BPNException("BPNlearningSet:"+fnfe); }catch(SecurityException se){ throw new BPNException("BPNlearningSet:"+se); } try{ StreamTokenizer strTok = new StreamTokenizer(in); strTok.slashSlashComments(true); strTok.slashStarComments(true); strTok.parseNumbers(); strTok.eolIsSignificant(false); int ttype = strTok.nextToken(); while(ttype != StreamTokenizer.TT_EOF){ if(ttype != StreamTokenizer.TT_NUMBER){ in.close(); throw new BPNException("BPNlearningSet: Invalid double format type at line "+strTok.lineno()); } // test if bigBuf has enough space left if(bigBuffer.length == count){ double temp[] = bigBuffer; bigBuffer = new double[count + block]; for(int i=0; i<count-1; i++) bigBuffer[i] = temp[i]; } // add a value to big buffer bigBuffer[count] = strTok.nval; if(strTok.nval<min) min = strTok.nval; if(strTok.nval>max) max = strTok.nval; count++; ttype = strTok.nextToken(); } }catch(IOException ioe){ try{in.close();}catch(IOException ignore){} throw new BPNException("BPNpattern: "+ioe); } try{in.close();}catch(IOException ignore){} if(count%(inSize+outSize) != 0){ // something wrong throw new BPNException("There are "+count%(inSize+outSize)+" unplaceable elements."); } size = count/(inSize+outSize); inputs = new BPNpattern[size]; targets = new BPNpattern[size]; count = 0; //System.out.println("normalise from "+min+".."+max+" to "+minDomain+".."+maxDomain); double inputVector[] = new double[inSize]; for(int i=0; i<size; i++){ for(int j=0; j<inSize; j++) inputVector[j] = scale(bigBuffer[count++],min,max, minDomain,maxDomain); inputs[i] = new BPNpattern(inputVector); } double targetVector[] = new double[outSize]; for(int i=0; i<size; i++){ for(int j=0; j<outSize; j++) targetVector[j] = scale(bigBuffer[count++],min,max, minDomain,maxDomain); targets[i] = new BPNpattern(targetVector); } } public static double scale(double x, double xMin, double xMax, double toMin, double toMax){ return (x-xMin)/(xMax-xMin)*(toMax-toMin)+toMin; } public void scaleDataTo(double min, double max){ for(int p=0; p<size; p++){ for(int i=0; i<inputs[p].vector.length; i++) inputs[p].vector[i] = scale(inputs[p].vector[i],minVal,maxVal,min, max); for(int i=0; i<targets[p].vector.length; i++) targets[p].vector[i] = scale(targets[p].vector[i],minVal,maxVal,min, max); } minVal = min; maxVal = max; } public int getSize(){ return size; } // public static void main(String args[]) {// try{// learningSet my = new learningSet("./","xor.pat",3,1);// System.out.println("OK, found "+my.size+" patterns.");// }catch(BPNException bpne){// System.out.println(""+bpne);// }// }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -