## USING NEURAL NETWORKS IN JAVA CODE

Neural networks trained and saved with easyNeurons can be easily used in your Java programs. You just need to import the Neuroph Library neuroph.jar which is part of this distribution and do the following:

import org.neuroph.core.NeuralNetwork;
import java.util.Vector;

/**
* This example shows how to load and use neural network created and saved with easyNeurons
*/
public class UseNeuralNetworkDemo {

> public static void main(String[] args) {

> > // load neural network
> > NeuralNetwork myNeuralNetwork = NeuralNetwork.load("mySamplePerceptron.nnet");
> > // set network input
> > myNeuralNetwork.setInput(1, 1);
> > // calculate network
> > myNeuralNetwork.calculate();
> > // get network output
> > Vector <Double> networkOutput = myNeuralNetwork.getOutput();
> > // print network output
> > System.out.println(networkOutput);

> }

}

Also you can create, train and save neural network in Java code like this:

package org.neuroph.samples;

import org.neuroph.core.NeuralNetwork;
import org.neuroph.nnet.Perceptron;
import org.neuroph.core.learning.TrainingSet;
import org.neuroph.core.learning.TrainingElement;
import org.neuroph.core.learning.SupervisedTrainingElement;
import java.util.Vector;

/**
* This sample shows how to create, train, save and load simple Perceptron neural network
*/
public class PerceptronSample {

> public static void main(String args[]) {

> > // create training set (logical AND 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[]{0}));
> > trainingSet.addElement(new SupervisedTrainingElement(new double[]{1, 0}, new double[]{0}));
> > trainingSet.addElement(new SupervisedTrainingElement(new double[]{1, 1}, new double[]{1}));

> > // create perceptron neural network
> > NeuralNetwork myPerceptron = new Perceptron(2, 1);
> > // learn the training set
> > myPerceptron.learnInSameThread(trainingSet);

> > // test perceptron
> > System.out.println("Testing trained perceptron");
> > testNeuralNetwork(myPerceptron, trainingSet);

> > // save trained perceptron
> > myPerceptron.save("mySamplePerceptron.nnet");

> > // load saved neural network
> > NeuralNetwork loadedPerceptron = NeuralNetwork.load("mySamplePerceptron.nnet");
> > // test loaded neural network
> > System.out.println("Testing loaded perceptron");
> > testNeuralNetwork(loadedPerceptron, trainingSet);

> }

> public static void testNeuralNetwork(NeuralNetwork nnet, TrainingSet tset) {

> > for(TrainingElement trainingElement : tset.trainingElements()) {

> > > nnet.setInput(trainingElement.getInput());
> > > nnet.calculate();
> > > Vector<Double> networkOutput = nnet.getOutput();
> > > System.out.print("Input: " + trainingElement.getInput());
> > > System.out.println(" Output: " + networkOutput);

> > }

> }

}

For more examples see org.neuroph.samples package.