Preparation

References

Examples

Try the Perceptron example first with Neuroph Studio. /!\ For Windows 7 and up, run Neuroph Studio as an administrator.

This is the Perceptron example from http://neuroph.sourceforge.net/tutorials/Perceptron.html.

If you have difficulties with the terminology, read https://en.wikibooks.org/wiki/Artificial_Neural_Networks/Neural_Network_Basics.

Java code

   1 import java.util.Arrays;
   2 import org.neuroph.core.NeuralNetwork;
   3 import org.neuroph.nnet.Perceptron;
   4 import org.neuroph.core.data.DataSet;
   5 import org.neuroph.core.data.DataSetRow;
   6 
   7 /**
   8  * This sample shows how to create, train, save and load simple Perceptron
   9  * neural network
  10  */
  11 public class PerceptronSample {
  12 
  13         public static void main(String args[]) {
  14 
  15                 // create training set (logical AND function)
  16                 DataSet trainingSet = new DataSet(2, 1);
  17                 trainingSet.addRow(new DataSetRow(new double[] { 0, 0 }, new double[] { 0 }));
  18                 trainingSet.addRow(new DataSetRow(new double[] { 0, 1 }, new double[] { 0 }));
  19                 trainingSet.addRow(new DataSetRow(new double[] { 1, 0 }, new double[] { 0 }));
  20                 trainingSet.addRow(new DataSetRow(new double[] { 1, 1 }, new double[] { 1 }));
  21 
  22                 // create perceptron neural network
  23                 NeuralNetwork myPerceptron = new Perceptron(2, 1);
  24 
  25                 // learn the training set
  26                 myPerceptron.learn(trainingSet);
  27 
  28                 // test perceptron
  29                 System.out.println("Testing trained perceptron");
  30                 testNeuralNetwork(myPerceptron, trainingSet);
  31 
  32                 // save trained perceptron
  33                 myPerceptron.save("mySamplePerceptron.nnet");
  34 
  35                 // load saved neural network
  36                 NeuralNetwork loadedPerceptron = NeuralNetwork.load("mySamplePerceptron.nnet");
  37 
  38                 // test loaded neural network
  39                 System.out.println("Testing loaded perceptron");
  40                 testNeuralNetwork(loadedPerceptron, trainingSet);
  41         }
  42 
  43         public static void testNeuralNetwork(NeuralNetwork nnet, DataSet tset) {
  44 
  45                 for (DataSetRow dataRow : tset.getRows()) {
  46 
  47                         nnet.setInput(dataRow.getInput());
  48                         nnet.calculate();
  49                         double[] networkOutput = nnet.getOutput();
  50                         System.out.print("Input: " + Arrays.toString(dataRow.getInput()));
  51                         System.out.println(" Output: " + Arrays.toString(networkOutput));
  52                 }
  53         }
  54 }

Processing code

   1 import org.neuroph.core.*;
   2 import org.neuroph.core.data.*;
   3 import org.neuroph.core.data.norm.*;
   4 import org.neuroph.core.data.sample.*;
   5 import org.neuroph.core.events.*;
   6 import org.neuroph.core.exceptions.*;
   7 import org.neuroph.core.input.*;
   8 import org.neuroph.core.learning.error.*;
   9 import org.neuroph.core.learning.*;
  10 import org.neuroph.core.learning.stop.*;
  11 import org.neuroph.core.transfer.*;
  12 import org.neuroph.nnet.*;
  13 import org.neuroph.nnet.comp.*;
  14 import org.neuroph.nnet.comp.layer.*;
  15 import org.neuroph.nnet.comp.neuron.*;
  16 import org.neuroph.nnet.learning.*;
  17 import org.neuroph.util.benchmark.*;
  18 import org.neuroph.util.*;
  19 import org.neuroph.util.io.*;
  20 import org.neuroph.util.plugins.*;
  21 import org.neuroph.util.random.*;
  22 
  23 import java.util.*;
  24 
  25 /**
  26  * This sample shows how to create, train, save and load simple Perceptron neural network 
  27  */
  28 void setup() {
  29 
  30   // create training set (logical AND function)
  31   DataSet trainingSet = new DataSet(2, 1);
  32   trainingSet.addRow(new DataSetRow(new double[] { 0, 0 }, new double[] { 0 }));
  33   trainingSet.addRow(new DataSetRow(new double[] { 0, 1 }, new double[] { 0 }));
  34   trainingSet.addRow(new DataSetRow(new double[] { 1, 0 }, new double[] { 0 }));
  35   trainingSet.addRow(new DataSetRow(new double[] { 1, 1 }, new double[] { 1 }));
  36 
  37   // create perceptron neural network
  38   NeuralNetwork myPerceptron = new Perceptron(2, 1);
  39 
  40   // learn the training set
  41   myPerceptron.learn(trainingSet);
  42 
  43   // test perceptron
  44   println("Testing trained perceptron");
  45   testNeuralNetwork(myPerceptron, trainingSet);
  46   
  47   println(dataPath(""));
  48 
  49   // save trained perceptron
  50   myPerceptron.save(sketchPath("mySamplePerceptron.nnet"));
  51 
  52   // load saved neural network
  53   NeuralNetwork loadedPerceptron = NeuralNetwork.load(sketchPath("mySamplePerceptron.nnet"));
  54 
  55   // test loaded neural network
  56   println("Testing loaded perceptron");
  57   testNeuralNetwork(loadedPerceptron, trainingSet);
  58 }
  59 
  60 void testNeuralNetwork(NeuralNetwork nnet, DataSet tset) {
  61 
  62   for (DataSetRow dataRow : tset.getRows()) {
  63 
  64     nnet.setInput(dataRow.getInput());
  65     nnet.calculate();
  66     double[ ] networkOutput = nnet.getOutput();
  67     print("Input: " + Arrays.toString(dataRow.getInput()) );
  68     println(" Output: " + Arrays.toString(networkOutput) );
  69   }
  70 }

diii: NeurophNeuralNetwork (last edited 2016-12-12 22:46:15 by JunHu)