6221
Comment:
|
← Revision 17 as of 2020-09-15 13:33:21 ⇥
6399
|
Deletions are marked like this. | Additions are marked like this. |
Line 7: | Line 7: |
* 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. | * 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). |
Line 11: | Line 11: |
* From `<neurophstudio directory>\neurophstudio\modules\ext` copy `neuroph-core-<latestversion>.jar` to `<Processing Sketchbook location>\libraries\neuroph\library`. Rename it to `neuroph.jar`. | * 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. |
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 }