6449
Comment:
|
← Revision 17 as of 2020-09-15 13:33:21 ⇥
6399
|
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 install Processing and if needed, Arduino software environment. * Install [[http://neuroph.sourceforge.net/|Neuroph]], a lightweight Java neural network framework. * 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. |
* Install Processing and if needed, also the Arduino software environment. * Install [[http://neuroph.sourceforge.net/|Neuroph]], a lightweight Java neural network framework. You might have to install [[http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html|JDK]] 8 first. JDK 8 works beter with Neuroph than later versions (now 2.98). * Download neurophstudio nstaller for your platform. |
Line 11: | Line 11: |
* 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 [[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. |
* From `<neurophstudio directory>\neurophstudio\modules\ext` copy `neuroph-core-2.98.jar` to `<Processing Sketchbook location>\libraries\neuroph\library`. Rename it to `neuroph.jar`. * From `<neurophstudio directory>\neurophstudio\modules\ext` copy `neuroph-core-2.8.jar` to `<Processing Sketchbook location>\libraries\neuroph\library`. Keep the same name. * For Windows, you might have to run NeurophStudio as an Administrator. |
Line 15: | Line 16: |
* [[http://neuroph.sourceforge.net/documentation.html|Neuroph Documentation]] * [[http://wiki.id.tue.nl/creapro|Assignment Creative Programming]] * [[http://wiki.id.tue.nl/processing2java|Assignment Processing to java]] |
* http://neuroph.sourceforge.net/documentation.html |
Line 21: | Line 20: |
* We will use the Perceptron Example: http://neuroph.sourceforge.net/tutorials/Perceptron.html | |
Line 22: | Line 22: |
Try the Perceptron example first with Neuroph Studio. /!\ For Windows 7 and up, run Neuroph Studio as an administrator. | |
Line 24: | Line 23: |
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 26: | Line 25: |
If you have difficulties with the terminology, read https://en.wikibooks.org/wiki/Artificial_Neural_Networks/Neural_Network_Basics. | About Learning rate and momentum: https://www.quora.com/What-is-the-difference-between-momentum-and-learning-rate |
Line 29: | Line 28: |
{{{ #!java |
{{{#!java |
Line 43: | Line 41: |
public static void main(String args[]) { | public static void main(String args[]) { |
Line 45: | Line 43: |
// create training set (logical AND function) DataSet trainingSet = new DataSet(2, 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 })); |
// create training set (logical AND function) DataSet trainingSet = new DataSet(2, 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 52: | Line 50: |
// create perceptron neural network NeuralNetwork myPerceptron = new Perceptron(2, 1); |
// create perceptron neural network NeuralNetwork myPerceptron = new Perceptron(2, 1); |
Line 55: | Line 53: |
// learn the training set myPerceptron.learn(trainingSet); |
// learn the training set myPerceptron.learn(trainingSet); |
Line 58: | Line 56: |
// test perceptron System.out.println("Testing trained perceptron"); testNeuralNetwork(myPerceptron, trainingSet); |
// test perceptron System.out.println("Testing trained perceptron"); testNeuralNetwork(myPerceptron, trainingSet); |
Line 62: | Line 60: |
// save trained perceptron myPerceptron.save("mySamplePerceptron.nnet"); |
// save trained perceptron myPerceptron.save("mySamplePerceptron.nnet"); |
Line 65: | Line 63: |
// load saved neural network NeuralNetwork loadedPerceptron = NeuralNetwork.load("mySamplePerceptron.nnet"); |
// load saved neural network NeuralNetwork loadedPerceptron = NeuralNetwork.load("mySamplePerceptron.nnet"); |
Line 68: | Line 66: |
// test loaded neural network System.out.println("Testing loaded perceptron"); testNeuralNetwork(loadedPerceptron, trainingSet); } |
// test loaded neural network System.out.println("Testing loaded perceptron"); testNeuralNetwork(loadedPerceptron, trainingSet); } |
Line 73: | Line 71: |
public static void testNeuralNetwork(NeuralNetwork nnet, DataSet tset) { | public static void testNeuralNetwork(NeuralNetwork nnet, DataSet tset) { |
Line 75: | Line 73: |
for (DataSetRow dataRow : tset.getRows()) { | for (DataSetRow dataRow : tset.getRows()) { |
Line 77: | Line 75: |
nnet.setInput(dataRow.getInput()); nnet.calculate(); double[] networkOutput = nnet.getOutput(); System.out.print("Input: " + Arrays.toString(dataRow.getInput())); System.out.println(" Output: " + Arrays.toString(networkOutput)); } } |
nnet.setInput(dataRow.getInput()); nnet.calculate(); double[] networkOutput = nnet.getOutput(); System.out.print("Input: " + Arrays.toString(dataRow.getInput())); System.out.println(" Output: " + Arrays.toString(networkOutput)); } } |
Line 85: | Line 83: |
}}} === Processing code === . /!\ Notice the use of the sketchPath() function for saving the data file next to your Processing sketch. |
|
Line 86: | Line 87: |
}}} === Processing code === /!\ Notice the use of the sketchPath() function for saving the data file next to your Processing sketch. {{{ #!java |
{{{#!java |
Line 117: | Line 113: |
* This sample shows how to create, train, save and load simple Perceptron neural network | * This sample shows how to create, train, save and load simple Perceptron neural network |
Line 137: | Line 133: |
Line 162: | Line 158: |
Preparation
- Install Processing and if needed, also the Arduino software environment.
Install Neuroph, a lightweight Java neural network framework. You might have to install JDK 8 first. JDK 8 works beter with Neuroph than later versions (now 2.98).
- Download neurophstudio nstaller for your platform.
- 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-2.98.jar to <Processing Sketchbook location>\libraries\neuroph\library. Rename it to neuroph.jar.
From <neurophstudio directory>\neurophstudio\modules\ext copy neuroph-core-2.8.jar to <Processing Sketchbook location>\libraries\neuroph\library. Keep the same name.
For Windows, you might have to run NeurophStudio as an Administrator.
References
Examples
We will use the Perceptron Example: 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.
About Learning rate and momentum: https://www.quora.com/What-is-the-difference-between-momentum-and-learning-rate
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 }