📄 joonetools.java
字号:
/* * JooneTools.java * * Created on January 16, 2006, 4:19 PM * * Copyright @2005 by Paolo Marrone and the Joone team * Licensed under the Lesser General Public License (LGPL); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at http://www.gnu.org/ * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.joone.helpers.factory;import java.io.File;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.ObjectInputStream;import java.io.ObjectOutput;import java.io.ObjectOutputStream;import java.io.OutputStream;import java.io.PrintStream;import java.util.TreeSet;import java.util.Vector;import org.joone.engine.DelayLayer;import org.joone.engine.DirectSynapse;import org.joone.engine.FullSynapse;import org.joone.engine.GaussianLayer;import org.joone.engine.KohonenSynapse;import org.joone.engine.Layer;import org.joone.engine.LinearLayer;import org.joone.engine.Monitor;import org.joone.engine.NeuralNetEvent;import org.joone.engine.NeuralNetListener;import org.joone.engine.Pattern;import org.joone.engine.SigmoidLayer;import org.joone.engine.SoftmaxLayer;import org.joone.engine.Synapse;import org.joone.engine.WTALayer;import org.joone.engine.learning.ComparingSynapse;import org.joone.engine.learning.TeachingSynapse;import org.joone.engine.listeners.ErrorBasedTerminator;import org.joone.io.MemoryInputSynapse;import org.joone.io.MemoryOutputSynapse;import org.joone.io.StreamInputSynapse;import org.joone.net.NeuralNet;import org.joone.net.NeuralNetAttributes;/** * Utility class to build/train/interrogate neural networks. * By using this class, it's possible to easily build/train and interrogate * a neural network with only 3 rows of code, as in this example: * * // Create an MLP network with 3 layers [2,2,1 nodes] with a logistic output layer * NeuralNet nnet = JooneTools.create_standard(new int[]{2,2,1}, JooneTools.LOGISTIC); * // Train the network for 5000 epochs, or until the rmse < 0.01 * double rmse = JooneTools.train(nnet, inputArray, desiredArray, 5000, 0.01, 0, null); * // Interrogate the network * double[] output = JooneTools.interrogate(nnet, testArray); * * @author paolo */public class JooneTools { // Kinds of output layer /** * Linear output layer */ public static final int LINEAR = 1; /** * Logistic (sigmoid) output layer */ public static final int LOGISTIC = 2; /** * Softmax output layer */ public static final int SOFTMAX = 3; /** * WTA output layer (unsupervised Kohonen) */ public static final int WTA = 4; /** * Gaussian output layer (unsupervised Kohonen) */ public static final int GAUSSIAN = 5; // Training algorithms /** * Backprop on-line (incremental) learning algorithm */ public static final int BPROP_ONLINE = 0; /** * Backprop batch learning algorithm */ public static final int BPROP_BATCH = 1; /** * Resilient Backprop learning algorithm */ public static final int RPROP = 2; /** * Creates a feed forward neural network without I/O components. * @param nodes array of integers containing the nodes of each layer * @param outputType the type of output layer. One of 'LINEAR', 'SOFTMAX', 'LOGISTIC' * @return The neural network created * @throws java.lang.IllegalArgumentException . */ public static NeuralNet create_standard(int nodes[], int outputType) throws IllegalArgumentException { NeuralNet nnet = new NeuralNet(); if ((nodes == null) || (nodes.length < 2)) { throw new IllegalArgumentException("create_standard: Nodes is empty"); } Layer[] layers = new Layer[nodes.length]; // Input layer layers[0] = new LinearLayer(); layers[0].setRows(nodes[0]); layers[0].setLayerName("input"); nnet.addLayer(layers[0], NeuralNet.INPUT_LAYER); // Hidden layers if (nodes.length > 2) { for (int i=1; i < nodes.length - 1; ++i) { layers[i] = new SigmoidLayer(); layers[i].setRows(nodes[i]); layers[i].setLayerName("hidden"+i); nnet.addLayer(layers[i], NeuralNet.HIDDEN_LAYER); } } // Output layer int outp = nodes.length - 1; switch (outputType) { case LINEAR: layers[outp] = new LinearLayer(); break; case LOGISTIC: layers[outp] = new SigmoidLayer(); break; case SOFTMAX: layers[outp] = new SoftmaxLayer(); break; default: throw new IllegalArgumentException("create_standard: output type not supported"); } layers[outp].setRows(nodes[outp]); layers[outp].setLayerName("output"); nnet.addLayer(layers[outp], NeuralNet.OUTPUT_LAYER); // Internal connections for (int i=0; i < layers.length - 1; ++i) { connect(layers[i], new FullSynapse(), layers[i+1]); } // Prepares the learning parameters Monitor mon = nnet.getMonitor(); mon.addLearner(BPROP_ONLINE, "org.joone.engine.BasicLearner"); // Default mon.addLearner(BPROP_BATCH, "org.joone.engine.BatchLearner"); mon.addLearner(RPROP, "org.joone.engine.RpropLearner"); mon.setLearningRate(0.7); mon.setMomentum(0.7); return nnet; } /** * Creates a feed forward neural network without I/O components. * @param nodes array of integers containing the nodes of each layer * @param outputType the type of output layer. One of 'LINEAR', 'SOFTMAX', 'LOGISTIC' * @return The neural network created * @throws java.lang.IllegalArgumentException . */ public static NeuralNet create_timeDelay(int nodes[], int taps, int outputType) throws IllegalArgumentException { NeuralNet nnet = new NeuralNet(); if ((nodes == null) || (nodes.length < 2)) { throw new IllegalArgumentException("create_standard: nodes: not enough elements"); } Layer[] layers = new Layer[nodes.length]; // Input layer layers[0] = new DelayLayer(); layers[0].setRows(nodes[0]); ((DelayLayer)layers[0]).setTaps(taps); layers[0].setLayerName("input"); nnet.addLayer(layers[0], NeuralNet.INPUT_LAYER); // Hidden layers if (nodes.length > 2) { for (int i=1; i < nodes.length - 1; ++i) { layers[i] = new SigmoidLayer(); layers[i].setRows(nodes[i]); layers[i].setLayerName("hidden"+i); nnet.addLayer(layers[i], NeuralNet.HIDDEN_LAYER); } } // Output layer int outp = nodes.length - 1; switch (outputType) { case LINEAR: layers[outp] = new LinearLayer(); break; case LOGISTIC: layers[outp] = new SigmoidLayer(); break; case SOFTMAX: layers[outp] = new SoftmaxLayer(); break; default: throw new IllegalArgumentException("create_standard: output type not supported"); } layers[outp].setRows(nodes[outp]); layers[outp].setLayerName("output"); nnet.addLayer(layers[outp], NeuralNet.OUTPUT_LAYER); // Internal connections for (int i=0; i < layers.length - 1; ++i) { connect(layers[i], new FullSynapse(), layers[i+1]); } // Prepares the learning parameters Monitor mon = nnet.getMonitor(); mon.addLearner(BPROP_ONLINE, "org.joone.engine.BasicLearner"); // Default mon.addLearner(BPROP_BATCH, "org.joone.engine.BatchLearner"); mon.addLearner(RPROP, "org.joone.engine.RpropLearner"); mon.setLearningRate(0.7); mon.setMomentum(0.7); return nnet; } /** * Creates an unsupervised neural network without I/O components. * This method is able to build the following kind of networks, depending on the 'outputType' parameter: * WTA - Kohonen network with a WinnerTakeAll output layer * GAUSSIAN - Kohonen network with a gaussian output layer * The nodes array must contain 3 elements, with the following meaning: * nodes[0] = Rows of the input layer * nodes[1] = Width of the output map * nodes[2] = Height of the output map * @param nodes array of integers containing the nodes of each layer (see note above) * @param outputType the type of output layer. One of 'WTA', 'GAUSSIAN' * @return The neural network created * @throws java.lang.IllegalArgumentException */ public static NeuralNet create_unsupervised(int nodes[], int outputType) throws IllegalArgumentException { NeuralNet nnet = new NeuralNet(); if ((nodes == null) || (nodes.length < 3)) { throw new IllegalArgumentException("create_unsupervised: nodes: not enough elements"); } // A Kohonen network contains 2 layers Layer[] layers = new Layer[2]; // Input layer layers[0] = new LinearLayer(); layers[0].setRows(nodes[0]); layers[0].setLayerName("input");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -