Joone logo

the Joone site
SourceForge
 
   

The Java API cookbook

PDF
PDF

How to build a NN using java code

The fulcrum of the engine is the Layer object. It is composed by N neurons(that can be set by the attribute 'rows'). Imagine a feed-forward neural net composed by three layers like the following:

a very simple neural network

To build this net with joone, we must create three Layer objects and two Synapse objects:

      SigmoidLayer layer1 = new SigmoidLayer();
      SigmoidLayer layer2 = new SigmoidLayer();
      SigmoidLayer layer3 = new SygmoidLayer();
      FullSynapse synapse1 = new FullSynapse();
      FullSynapse synapse2 = new FullSynapse();

Then we complete the net connecting the three layers with the synapses:

      layer1.addOutputSynapse(synapse1);
      layer2.addInputSynapse(synapse1);
      layer2.addOutputSynapse(synapse2);
      layer3.addInputSynapse(synapse2);

Here you can see, each synapse is the output synapse of a layer and the input synapse of the next layer in the net. This simple net is ready, but it can't do any useful job, because there aren't the components to read/write the data that the net must elaborate. Look at the next example to learn how to build a real net, which can be trained and used for a real problem.

A real implementation: The XOR Problem

Suppose we must build a net to teach on the classical XOR problem. In this example, the net must learn the following XOR truth table:

The XOR truth table
Input 1Input 2Output
000
011
101
110

So, we must create a file containing this values:

	0.0;0.0;0.0
	0.0;1.0;1.0
	1.0;0.0;1.0
	1.0;1.0;0.0

Each column must be separated by a semicolon; the decimal point is not mandatory if the numbers are integer. Write this file with a text editor and save it on the file system (for instance c:\joone\xor.txt in a Windows environment). Now we'll build the neural net that, as the literature says, must have three layers:

  1. An input layer with 2 neurons, to map the two inputs of the XOR function.
  2. A hidden layer with 3 neurons, a good value to speed up the net's convergence.
  3. An output layer with 1 neuron, to represent the XOR function's output.

As shown by the following figure:

sample XOR network

First, we create the three layers (two of them use the sigmoid transfer function):

	LinearLayer input = new LinearLayer();
	SigmoidLayer hidden = new SigmoidLayer();
	SigmoidLayer output = new SygmoidLayer();

Set their dimensions:

	input.setRows(2);
	hidden.setRows(3);
	output.setRows(1);

Now we build the neural net connecting the layers, so we create the two synapses; we use the FullSynapse, that connects all the neurons on its input with all the neurons on its output (see the above figure):

	FullSynapse synapse_IH = new FullSynapse(); /* Input  -> Hidden conn. */
	FullSynapse synapse_HO = new FullSynapse(); /* Hidden -> Output conn. */

First, we connect the input layer with the hidden layer:

	input.addOutputSynapse(synapse_IH);
	hidden.addInputSynapse(synapse_IH);

And then, the hidden layer with the output layer:

	hidden.addOutputSynapse(synapse_HO);
	output.addInputSynapse(synapse_HO);

Create the NeuralNet object and add the layers:

	NeuralNet nnet = new NeuralNet();
	nnet.addLayer(input, NeuralNet.INPUT_LAYER);
	nnet.addLayer(hidden, NeuralNet.HIDDEN_LAYER);
	nnet.addLayer(output, NeuralNet.OUTPUT_LAYER);
	

Now we extract the reference of the Monitor object, in order to provide the network with all the parameters needed for its work:

	Monitor monitor = nnet.getMonitor();
	monitor.setLearningRate(0.7);
	monitor.setMomentum(0.5);

The application registers itself as a monitor's listener, so it can receive the notifications of termination from the net. To do this, the application must implement the org.joone.engine.NeuralNetListener interface.

    	monitor.addNeuralNetListener(this);

Now we must define an input for the net, then we create a org.joone.io.FileInputStream and give it all the parameters:

	FileInputSynapse inputStream = new FileInputSynapse();
	/* The first two columns contain the input values */
	inputStream.setAdvancedColumnSelector("1,2");
	/* This is the file that contains the input data */
	inputStream.setInputFile(new File("#topofpage"));

We add the input synapse to the first layer. The input synapse extends the Synapse object, then it can be attached to a layer like a synapse; so the layer doesn't deal with the kind of its input objects.

	input.addInputSynapse(inputStream);

A neural net can learn from examples, so we must provide to it with the right responses. For each input, in fact, the net must be provided with the difference between the desired response and the effective response gave from the net; the org.joone.engine.learning.TeachingSynapse is the object that has this task:

	TeachingSynapse trainer = new TeachingSynapse();
	/* Setting of the file containing the desired responses, provided by a FileInputSynapse */
	FileInputSynapse samples = new FileInputSynapse();
	samples.setInputFile(new File("#topofpage"));
	trainer.setDesired(samples);
	/* The output values are on the third column of the file */
	samples.setAdvancedColumnSelector("3");
	/* We add it to the neural network */
	nnet.setTeacher(trainer);

The TeacherSynapse object extends the Synapse object, then we can add it as the output of the last layer of the net.

	output.addOutputSynapse(trainer);

We set all the training parameters of the net:

	monitor.setTrainingPatterns(4); /* # of rows contained in the input file */
	monitor.setTotCicles(2000); /* How many times the net must be trained on the input patterns */
	monitor.setLearning(true); /* The net must be trained */
	nnet.go(); /* The network starts the training job */

(You can find the source code in the CVS repository into the org.joone.example package)

If this example seems too complex (it's only a small net with only 5 neurons!), remember that this is a low-level approach; here we have used only the core engine coding in java. You can build neural nets with joone in the following three ways (ordered by decreasing difficulty):

  1. Like the above example, by writing java code that uses the Core Engine
  2. By using the JooneTools helper class in order to simplify the use of a neural network, by hiding the complexity of the core engine
  3. By using the GUI editor provided with joone (Click here to see the XOR problem built with the editor)

Resources