Create Multi.java

package neuro;

//package org.neuroph.samples;

import org.neuroph.core.NeuralNetwork;
import org.neuroph.nnet.MultiLayerPerceptron;
import org.neuroph.nnet.learning.MomentumBackpropagation;
import org.neuroph.core.data.DataSet;
import org.neuroph.core.data.DataSetRow;
import org.neuroph.core.events.NeuralNetworkCalculatedEvent;

import java.util.Arrays;
import java.util.Vector;

import org.neuroph.util.TransferFunctionType;

/**
* This sample shows how to create, train, save and load simple Multi Layer Perceptron
*/
public class Multi {

public static void main(String[] args) {

// create training set (logical XOR 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[]{1}));
trainingSet.addRow(new DataSetRow(new double[]{1, 0}, new double[]{1}));
trainingSet.addRow(new DataSetRow(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.learn(trainingSet);

MomentumBackpropagation learningRule = (MomentumBackpropagation) myMlPerceptron.getLearningRule();
//learningRule.setLearningRate(0.4);
//learningRule.setMomentum(0.6);
System.out.println("getLearningRate"+learningRule.getLearningRate());
System.out.println("getMaxError"+learningRule.getMaxError());
System.out.println("getMaxIterations"+learningRule.getMaxIterations());
System.out.println("getMinErrorChange"+learningRule.getMinErrorChange());
System.out.println("getMinErrorChangeIterationsCount"+learningRule.getMinErrorChangeIterationsCount());
System.out.println("getMinErrorChangeIterationsLimit"+learningRule.getMinErrorChangeIterationsLimit());
System.out.println("getMomentum"+learningRule.getMomentum());
System.out.println("getPreviousEpochError"+learningRule.getPreviousEpochError());
System.out.println("getTotalNetworkError"+learningRule.getTotalNetworkError());
System.out.println("getCurrentIteration"+learningRule.getCurrentIteration());

// 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.createFromFile("myMlPerceptron.nnet");

// test loaded neural network
System.out.println("Testing loaded neural network");
testNeuralNetwork(loadedMlPerceptron, trainingSet);

}

public static void testNeuralNetwork(NeuralNetwork nnet, DataSet tset) {
//NeuralNetworkCalculatedEvent calculatedEvent = new NeuralNetworkCalculatedEvent(nnet);
//calculatedEvent.
for(DataSetRow dataRow : tset.getRows()) {

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) );

}

}

 

}