Differences between revisions 2 and 8 (spanning 6 versions)
Revision 2 as of 2016-12-12 21:09:55
Size: 6274
Editor: JunHu
Comment:
Revision 8 as of 2016-12-12 22:46:15
Size: 6455
Editor: JunHu
Comment:
Deletions are marked like this. Additions are marked like this.
Line 6: Line 6:
 * 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 8:
  * 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.
Line 12: Line 12:
 * 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 [[http://wiki.id.tue.nl/processing2java|Processing2Java]]. Remember you shall include the aforementioned jar files in the projects if you want to use neuroph.  * 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 [[http://wiki.id.tue.nl/processing2java|Processing2Java]]. Remember you shall include the aforementioned jar files in the projects if you want to use neuroph.ph.
Line 18: Line 18:
 * [[MaxJavaArduino | Workshop on how to interface Max/msp, Java, a neural network engine and Arduino]]
Line 21: Line 21:
Try the Perceptron example first with Neuroph Studio:
This is the Perceptron example from http://neuroph.sourceforge.net/tutorials/Perceptron.html:

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.
Line 85: Line 89:
 /!\ Notice the use of the sketchPath() function for saving the data file next to your Processing sketch.
Line 87: Line 92:
import org.neuroph.util.plugins.*; import org.neuroph.core.*;
import org.neuroph.core.data.*;
import org.neuroph.core.data.norm.*;
import org.neuroph.core.data.sample.*;
import org.neuroph.core.events.*;
import org.neuroph.core.exceptions.*;
import org.neuroph.core.input.*;
import org.neuroph.core.learning.error.*;
import org.neuroph.core.learning.*;
import org.neuroph.core.learning.stop.*;
Line 89: Line 103:
import org.neuroph.util.*; import org.neuroph.nnet.*;
import org.neuroph.nnet.comp.*;
Line 91: Line 106:
import org.neuroph.nnet.*;
import org.neuroph.core.events.*;
import org.neuroph.core.learning.error.*;
import org.neuroph.core.data.sample.*;
import org.neuroph.util.io.*;
import org.neuroph.core.exceptions.*;
import org.neuroph.core.learning.*;
import org.neuroph.core.input.*;
import org.neuroph.nnet.comp.neuron.*;
Line 101: Line 109:
import org.neuroph.util.*;
import org.neuroph.util.io.*;
import org.neuroph.util.plugins.*;
Line 102: Line 113:
import org.neuroph.nnet.comp.*;
import org.neuroph.core.learning.stop.*;
import org.neuroph.core.data.*;
import org.neuroph.core.*;
import org.neuroph.nnet.comp.neuron.*;
import org.neuroph.core.data.norm.*;
Line 118: Line 123:
  trainingSet.addRow(new DataSetRow(new double[] {
    0, 0
  }
  , new double[] {
    0
  }
  
));
  trainingSet.addRow(new DataSetRow(new double[] {
    0, 1
  }
  , new double[] {
    0
  }
  
));
  trainingSet.addRow(new DataSetRow(new double[] {
    1, 0
  }
  , new double[] {
    0
  }
  
));
  trainingSet.addRow(new DataSetRow(new double[] {
    1, 1
  }
  , new double[] {
    1
  }
  
));
  trainingSet.addRow(new DataSetRow(new double[] { 0, 0 }, new double[] { 0 }));
  trainingSet.addRow(new DataSetRow(new double[] { 0, 1 }, new double[] { 0 }));
  trainingSet.addRow(new DataSetRow(new double[] { 1, 0 }, new double[] { 0 }));
  trainingSet.addRow(new DataSetRow(new double[] { 1, 1 }, new double[] { 1 }));
Line 154: Line 135:
  System.out.println("Testing trained perceptron");   println("Testing trained perceptron");
Line 156: Line 137:
     println(dataPath(""));
Line 158: Line 141:
  myPerceptron.save("mySamplePerceptron.nnet");   myPerceptron.save(sketchPath("mySamplePerceptron.nnet"));
Line 161: Line 144:
  NeuralNetwork loadedPerceptron = NeuralNetwork.load("mySamplePerceptron.nnet");   NeuralNetwork loadedPerceptron = NeuralNetwork.load(sketchPath("mySamplePerceptron.nnet"));
Line 164: Line 147:
  System.out.println("Testing loaded perceptron");   println("Testing loaded perceptron");
Line 175: Line 158:
    System.out.print("Input: " + Arrays.toString(dataRow.getInput()) );
    System.out.println(" Output: " + Arrays.toString(networkOutput) );
    print("Input: " + Arrays.toString(dataRow.getInput()) );
    println(" Output: " + Arrays.toString(networkOutput) );
Line 181: Line 164:



=== A trained XOR multilayer perceptron network ===
[[attachment:MultiLayerPerceptron.nnet]]

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.ph.

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

  • /!\ Notice the use of the sketchPath() function for saving the data file next to your Processing sketch.

   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 }

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