Java neural networks and Neuroph – A tutorial
The java neural network Neuroph was making news recently about its integration with Hadoop. Neural networks can solve some interesting problems once they are trained. This article aims to provide the baby steps necessary to writing your first java program that loads a trained neural network.
Before you even begin to read anything that follows, a basic understanding of neural network terminology and the concept behind the same is necessary. The following articles are great starting points to understanding neural networks
Neuroph and neural networks – Part 1
Neuroph and neural networks – Part 2
Neuroph and neural networks – Part 3
Cars and Signals:
We will simulate the scenario where cars wait at a signal and move only when the lights are green. This simple example should help get you started. Our aim is to define a neural network with the easyNeurons swing application; train it; import it into java and use it in an application.
There are 3 states for this signal
1. Red – Stay where you are
2. Yellow – Start your engines
3. Green – Go
The neural network will take 3 inputs and its architecture will be based on the multi layer perceptron setting. A hidden layer with 4 nodes will decide the output. The output itself will be based on one node, whose value will determine if the cars can move or not. This is what our network looks like so far
Create the network in neuroph: Network -> Multi layer Perceptron
Basic network with no training: View -> Graph
Now we need to train the network so that the output will be as expected when the signals change. The rules are pretty simple and are shown below.To train the network we create a training set
Neuroph training set:
Train the network to respond to the inputs:
After the training rules have been laid out, it is time to train this network. Simply press on the Train button and select the appropriate training set to use.
Set the parameters by which the network should learn:
The trained network:
Now that we have our network trained, lets try giving it an input. An input of 0 0 1 means the signal is green and the vehicles can go through. The output produced in this case is shown below. The output will vary based on the function used in the training set / error rate and other factors. But what is to be highlighted here is that the output is nearing 1 when the signal is green. Our network works as expected.
Signal is green and output is 1 (well almost
):
We can confirm how this decision was taken by the network by highlighting the weights. Additionally by representing the size of each node with respect to the activation contributed for that node, we can visualize how the input message propagates
Weight / Activation highlighting:
So now that we have a neural network up and running, how do we actually use this inside java code ? Its pretty simple. Save the project as a .nnet file. Lets call this neural_traffic.nnet. To load the nnet file into your java project, simple use the classes provided by Neuroph like so
Loading a neural network into java:
public class TestTrafficNeural { NeuralNetwork network = NeuralNetwork.load("neural_traffic.nnet"); public static void main(String[] args) { new TestTrafficNeural().go(); } private void go() { calculate(1,0,0); calculate(0,1,0); calculate(0,0,1); } private void calculate(double... input) { network.setInput(input); network.calculate(); Vector output = network.getOutput(); Double answer = output.get(0); System.out.println(answer); } }
The code produces the following output
-1.6360230873976706E-6
-4.140786100885251E-6
0.9684448970000741
You can also define and train the network on the fly with code. But you would not want to do this for cases where large sets of inputs and nodes are involved. For simple problems the time taken to train a network is usually a few seconds. For large images ( Assuming you are trying to recognize images with Neuroph ) it can take a couple of hours.
This example is pretty easy to do and is certainly not a practical use case for real world problems. However I hope it gets you excited about using neural networks in your programs. With Neuroph doing this is pretty simple.








I tried running the code above of loading a neural network into java but I’m getting the following error.
Exception in thread “main” java.lang.ClassCastException: cannot assign instance of java.util.ArrayList to field org.neuroph.core.Layer.neurons of type java.util.Vector in instance of org.neuroph.core.Layer
@Bee
Perhaps the next version of the library has changed the return type ? A ClassCastException should be simple enough to solve. Simply assign the reference to the correct return type. If the Exception is from the Neuroph library itself, then it may be a bug
Ohh i have just the same problem. Tried everything but nothing helped,
Please can somebody help with this
i have got a project as a part of my summer training and that requires me to predict the thunderstorms current position by using the previous data …………and i dunno how to use neural network in that case …..i wud reely appreciate if u could gimme some sort of help or source code of other neural network based programs.
sir
it is showing error like network class not found .do i have to code separate network class or what should i import.also can’t i see the code of my neroph project
sir iam trying my best to import the neuroph code into the java file but it is not getting imported .
NeuralNetwork network = NeuralNetwork.load(“NewNeuralNetwork1.nnet”);
double[] output = network.getOutput();
but it is returning some garbage values
sir i am trying to implement nn for credit approval .i need dataset that is compatible with nueroph studio .plz tell me url through which i can get datset
I think u need to convert your out to string…
like,… arrays.toString(output);
then put this in system.out ……. @rahul
Nice site. I’ve a few sites myself. Used to control everything. Should be able to find it through Google. Type and you ought to be able to find it.
I real happy to find this internet site on bing, just what I was searching for : D as well saved to favorites.
You’re so interesting! I do not believe I’ve truly read anything like
this before. So wonderful to discover somebody with a
few unique thoughts on this topic. Seriously.
. many thanks for starting this up. This web site is something that’s needed on the web, someone with some originality!
WOW just what I was looking for. Came here by searching for java