Home > java > Java neural networks and Neuroph – A tutorial

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

Intro to neural networks

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.




Categories: java Tags: , ,
  1. Bee
    April 8th, 2011 at 10:27 | #1

    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

  2. April 23rd, 2011 at 14:24 | #2

    @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

  3. streetspirit
    May 16th, 2011 at 23:41 | #3

    Ohh i have just the same problem. Tried everything but nothing helped,
    Please can somebody help with this

  4. vikram
    June 18th, 2011 at 17:45 | #4

    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.

  5. rahul
    May 7th, 2012 at 01:28 | #5

    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

  6. rahul
    May 9th, 2012 at 02:12 | #6

    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

  7. parag
    July 21st, 2012 at 10:49 | #7

    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

  8. Nomiluks
    August 12th, 2012 at 05:03 | #8

    I think u need to convert your out to string…

    like,… arrays.toString(output);
    then put this in system.out ……. @rahul

  9. February 12th, 2013 at 08:48 | #9

    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.

  10. April 27th, 2013 at 11:04 | #10

    I real happy to find this internet site on bing, just what I was searching for : D as well saved to favorites.

  11. May 11th, 2013 at 02:20 | #11

    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!

  12. May 19th, 2013 at 09:36 | #12

    WOW just what I was looking for. Came here by searching for java

  13. Aye Min Oo
    July 10th, 2013 at 03:07 | #13

    Sir I first create your project in neuroph studio and it run well and right in that studio then I took the .nnet file and used it in my java code then i get different result.and that result is wrong . So I closed neuroph studio and open it again and test it again. Wow ! then neuroph studio also show me the different result and that different result is the same as the result from my java code. What is the problem ? any one experience this. show me the way to solve it.I am in real trouble.

  14. August 6th, 2013 at 14:10 | #14

    Amazing short article, I’m now subbed to your weblog
    Nice weblog man, recently been reading for quit some time but only just decided to take a note of and remark
    Amazing submit, I’ve been deciphering google for hours looking
    for this sort of information!
    I’m hoping you will end up updating this web site regularly while i love your content!

  15. April 11th, 2014 at 18:05 | #15

    You are able to certainly see your knowledge in the perform you publish. The planet hopes for a lot more passionate writers including you who’re not afraid to mention how they think. On a regular basis observe your heart.

  16. April 12th, 2014 at 03:49 | #16

    Hello there, just grew to become alert to your blog by Google, and located that it’s actually informative. I am gonna observe out for brussels. I’ll enjoy should you proceed this in long term. Several individuals might be benefited from the writing. Cheers!

  17. Ram
    April 19th, 2014 at 17:04 | #17

    Exception in thread “main” java.lang.UnsupportedClassVersionError: org/neuroph/core/NeuralNetwork : Unsupported major.minor version 51.0
    at java.lang.ClassLoader.defineClass1(Native Method)
    at java.lang.ClassLoader.defineClass(ClassLoader.java:620)
    at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:124)
    at java.net.URLClassLoader.defineClass(URLClassLoader.java:260)
    at java.net.URLClassLoader.access$000(URLClassLoader.java:56)
    at java.net.URLClassLoader$1.run(URLClassLoader.java:195)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(URLClassLoader.java:188)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:306)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:276)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:251)
    at java.lang.ClassLoader.loadClassInternal(ClassLoader.java:319)
    at neuroph.(neuroph.java:10)
    at neuroph.main(neuroph.java:14)

  18. April 27th, 2014 at 17:04 | #18

    Hey, you used to write wonderful, but the last several posts have

    been kinda boring… I miss your great writings.
    Past few posts are just a

    little out of track! come on!

  19. May 29th, 2014 at 21:04 | #19

    If you are going away to watch funny videos on the internet then I suggest you to pay a quick visit this web page, it carries genuinely so humorous not only videos but also additional stuff.

  20. Ijaz Ahmad
    June 6th, 2014 at 08:17 | #20

    Need help for displaying hopfield neural network in Neuroph studio. this is the error message when i am trying to display the network. “A java.lang.NullPointerException exception has occurred.”

  21. Ijaz Ahmad
    June 6th, 2014 at 08:19 | #21

    Whcih one is the stable version of Neuroph

  22. June 25th, 2014 at 20:08 | #22

    What’s up, its fastidious paragraph on the topic of media print, we all know media is a great source of information.

  23. July 1st, 2014 at 06:25 | #23

    Your method of explaining all in this post is genuinely pleasant, every one be capable of easily be aware of it, Thanks a lot.

  24. July 7th, 2014 at 13:37 | #24

    An outstanding share! I have just forwarded this onto a coworker who was doing a little homework on this.
    And he in fact ordered me breakfast because I found it for
    him… lol. So allow me to reword this…. Thanks for the meal!!
    But yeah, thanx for spending some time to talk about this topic here on your website.

  25. rahul
    July 26th, 2014 at 16:30 | #25

    hi, I want to develop a neuro-fuzzy model using neuroph. I cannot see the NFR type for network development while setting neural network name and type. This list doesn’t contain NFR (neuro fuy reasoning) type. whereas, the following paper cites NFR in neuroph. I was wondering where am I going wrong or how can I activate NFR in neuroph for neuro-fuzzy modeling?

    http://neuroph.sourceforge.net/Neuro%20Fuzzy%20reasoner%20for%20student%20modeling.pdf

  1. April 27th, 2010 at 05:17 | #1