## page was renamed from AdmovelNeuralNetwork <> == Preparation == * Follow the instructions from the assignment [[http://wiki.id.tue.nl/creapro|Creative Programming]] to prepare the SoftwareEnvironment, including Processing, Arduino and the AdMoVeo Robot. If you are not planning to use the GUI components from the Control``P5 library, you can skip the Control``P5 part. * Install [[http://neuroph.sourceforge.net/|Neuroph]], a lightweight Java neural network framework. * Download [[http://garr.dl.sourceforge.net/project/neuroph/neuroph%202.5/neurophstudio-windows.exe | Neuroph Studio]] and install it. * Download [[http://sourceforge.net/projects/neuroph/files/neuroph%202.5/neuroph-2.5b.zip/download | neuroph-2.5b.zip]]. Unzip it into C:\Programs. In C:\Programs\neuroph-2.5b you shall be able to find neuroph-2.5b.jar. (You can unzip it to anywhere you want, but then remember where it is for later reference). * In "Processing Sketchbook location"\libraries, create a sub-directory "neuroph". * In "Processing Sketchbook location"\libraries\neuroph, create a sub-directory "library". * From C:\Programs\neuroph-2.5b copy neuroph-2.5b.jar to "Processing Sketchbook location"\libraries\neuroph\library. Rename neuroph-2.5b.jar to neuroph.jar. * From C:\Programs\neuroph-2.5b\lib copy two jar files found there to "Processing Sketchbook location"\libraries\neuroph\library. * 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://http://wiki.id.tue.nl/processing2java|Processing2Java]]. Remember you shall include the aforementioned jar files in the projects if you want to use neuroph. == References == * CreaproProcessingLibrary * [[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]] * [[MaxJavaArduino | Workshop on how to interface Max/msp, Java, a neural network engine and Arduino]] == Examples == === Java -> Processing === {{{ #!java import org.neuroph.core.NeuralNetwork; import org.neuroph.nnet.MultiLayerPerceptron; import org.neuroph.core.learning.TrainingSet; import org.neuroph.core.learning.TrainingElement; import org.neuroph.core.learning.SupervisedTrainingElement; import java.util.Vector; import org.neuroph.util.TransferFunctionType; /** * This sample shows how to create, train, save and load simple Multi Layer Perceptron */ void setup() { // create training set (logical XOR function) TrainingSet trainingSet = new TrainingSet(); trainingSet.addElement(new SupervisedTrainingElement(new double[] { 0, 0 } , new double[] { 0 } )); trainingSet.addElement(new SupervisedTrainingElement(new double[] { 0, 1 } , new double[] { 1 } )); trainingSet.addElement(new SupervisedTrainingElement(new double[] { 1, 0 } , new double[] { 1 } )); trainingSet.addElement(new SupervisedTrainingElement(new double[] { 1, 1 } , new double[] { 0 } )); // create multi layer perceptron MultiLayerPerceptron myMlPerceptron = new MultiLayerPerceptron(TransferFunctionType.TANH, 2, 3, 1); // learn the training set myMlPerceptron.learnInSameThread(trainingSet); // test perceptron System.out.println("Testing trained neural network"); testNeuralNetwork(myMlPerceptron, trainingSet); // save trained neural network myMlPerceptron.save("myMlPerceptron.nnet"); // load saved neural network NeuralNetwork loadedMlPerceptron = NeuralNetwork.load("myMlPerceptron.nnet"); // test loaded neural network System.out.println("Testing loaded neural network"); testNeuralNetwork(loadedMlPerceptron, trainingSet); noLoop(); } void testNeuralNetwork(NeuralNetwork nnet, TrainingSet tset) { nnet.setInput(new double[]{1, 0}); nnet.calculate(); double networkOutput = nnet.getOutput()[0]; System.out.println(" Output: " + networkOutput); } }}} === AdMoVeo XOR === {{{ #!java import org.neuroph.core.NeuralNetwork; import org.neuroph.nnet.MultiLayerPerceptron; import processing.serial.*; import nl.tue.id.creapro.admoveo.*; AdMoVeo admoveo; int bgcolor = 0; int left = 0, right = 0; int result = 0; NeuralNetwork nnet; void setup() { nnet = NeuralNetwork.load("C:/Users/jhu/Documents/NetBeansProjects/XORProject/Neural Networks/NewNeuralNetwork.nnet"); admoveo = new AdMoVeo(this, "COM3"); admoveo.getLeftDistanceSensor().enable(); admoveo.getRightDistanceSensor().enable(); } void draw() { background(0,0,bgcolor); } void inputAvailable(Sensor sensor, int oldValue, int newValue){ if(sensor == admoveo.getLeftDistanceSensor()){ println(newValue); left = newValue < 900? 1: 0; } if(sensor == admoveo.getRightDistanceSensor()){ right = newValue < 900? 1: 0; } nnet.setInput(new double[] {left, right}); nnet.calculate(); int output = nnet.getOutput()[0]>.5?1:0; //println(""+ left + " " + right + " " + output); if(output!=result){ bgcolor = output *255; admoveo.getBlueLed().setPower(bgcolor); result = output; } } }}} === A trained XOR multilayer perceptron network === [[attachment:MultiLayerPerceptron.nnet]]