Differences between revisions 33 and 34
Revision 33 as of 2016-12-11 23:02:42
Size: 6587
Editor: JunHu
Comment:
Revision 34 as of 2016-12-12 20:50:49
Size: 6643
Editor: JunHu
Comment:
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from AdmoveoNeuralNetwork
## page was renamed from AdmovelNeuralNetwork
Line 6: Line 5:
 * Follow the instructions from the assignment [[http://wiki.id.tue.nl/creapro|Creative Programming]] to prepare the software environment, including Processing and Arduino. If you are not planning to use the GUI components from the Control``P5 library, you can skip the Control``P5 part.  * Follow the instructions from the assignment [[http://wiki.id.tue.nl/creapro|Creative Programming]] to install Processing and if needed, Arduino software environment.
Line 8: Line 7:
  * Download proper neurophstudio installer for your platform. I will use Windows platform as examples here   * Download neurophstudio '''2.8''' installer for your platform (for Windows: [[http://sourceforge.net/projects/neuroph/files/neuroph-2.8/neurophstudio-windows-2.8.exe/download | neurophstudio-windows-2.8.exe]]). We have difficulties in getting the latest version to work with Processing, and all the tutorials and documentation are not yet updated for the latest version.

Preparation

  • Follow the instructions from the assignment Creative Programming to install Processing and if needed, Arduino software environment.

  • Install Neuroph, a lightweight Java neural network framework.

    • Download neurophstudio 2.8 installer for your platform (for Windows: neurophstudio-windows-2.8.exe). We have difficulties in getting the latest version to work with Processing, and all the tutorials and documentation are not yet updated for the latest version.

    • In "Processing Sketchbook location"\libraries, create a sub-directory "neuroph".
    • In "Processing Sketchbook location"\libraries\neuroph, create a sub-directory "library".
    • From <neurophstudio directory>\neurophstudio\modules\ext copy neuroph-core-<version>.jar to <Processing Sketchbook location>\libraries\neuroph\library. Rename neuroph-core-<version>.jar to neuroph.jar.

  • If you are confident enough, you can also try to use Eclipse to program in Java. Then try to follow the instructions from the assignment Processing2Java. Remember you shall include the aforementioned jar files in the projects if you want to use neuroph.

References

Examples

Try the Perceptron example first with Neuroph Studio: This is the Perceptron example from http://neuroph.sourceforge.net/tutorials/Perceptron.html:

Java code

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

Processing code

   1 import org.neuroph.util.plugins.*;
   2 import org.neuroph.core.transfer.*;
   3 import org.neuroph.util.*;
   4 import org.neuroph.nnet.comp.layer.*;
   5 import org.neuroph.nnet.*;
   6 import org.neuroph.core.events.*;
   7 import org.neuroph.core.learning.error.*;
   8 import org.neuroph.core.data.sample.*;
   9 import org.neuroph.util.io.*;
  10 import org.neuroph.core.exceptions.*;
  11 import org.neuroph.core.learning.*;
  12 import org.neuroph.core.input.*;
  13 import org.neuroph.nnet.learning.*;
  14 import org.neuroph.util.benchmark.*;
  15 import org.neuroph.util.random.*;
  16 import org.neuroph.nnet.comp.*;
  17 import org.neuroph.core.learning.stop.*;
  18 import org.neuroph.core.data.*;
  19 import org.neuroph.core.*;
  20 import org.neuroph.nnet.comp.neuron.*;
  21 import org.neuroph.core.data.norm.*;
  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[] {
  33     0, 0
  34   }
  35   , new double[] {
  36     0
  37   }
  38   ));
  39   trainingSet.addRow(new DataSetRow(new double[] {
  40     0, 1
  41   }
  42   , new double[] {
  43     0
  44   }
  45   ));
  46   trainingSet.addRow(new DataSetRow(new double[] {
  47     1, 0
  48   }
  49   , new double[] {
  50     0
  51   }
  52   ));
  53   trainingSet.addRow(new DataSetRow(new double[] {
  54     1, 1
  55   }
  56   , new double[] {
  57     1
  58   }
  59   ));
  60 
  61   // create perceptron neural network
  62   NeuralNetwork myPerceptron = new Perceptron(2, 1);
  63 
  64   // learn the training set
  65   myPerceptron.learn(trainingSet);
  66 
  67   // test perceptron
  68   System.out.println("Testing trained perceptron");
  69   testNeuralNetwork(myPerceptron, trainingSet);
  70 
  71   // save trained perceptron
  72   myPerceptron.save("mySamplePerceptron.nnet");
  73 
  74   // load saved neural network
  75   NeuralNetwork loadedPerceptron = NeuralNetwork.load("mySamplePerceptron.nnet");
  76 
  77   // test loaded neural network
  78   System.out.println("Testing loaded perceptron");
  79   testNeuralNetwork(loadedPerceptron, trainingSet);
  80 }
  81 
  82 void testNeuralNetwork(NeuralNetwork nnet, DataSet tset) {
  83 
  84   for (DataSetRow dataRow : tset.getRows()) {
  85 
  86     nnet.setInput(dataRow.getInput());
  87     nnet.calculate();
  88     double[ ] networkOutput = nnet.getOutput();
  89     System.out.print("Input: " + Arrays.toString(dataRow.getInput()) );
  90     System.out.println(" Output: " + Arrays.toString(networkOutput) );
  91   }
  92 }

A trained XOR multilayer perceptron network

MultiLayerPerceptron.nnet

JunHu: NeurophNeuralNetwork (last edited 2016-12-12 20:50:49 by JunHu)