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. August 31st, 2014 at 02:32 | #1

    With havin so much content do you ever run into any problems of plagorism or
    copyright infringement? My site has a lot of exclusive content I’ve either created myself or outsourced but it seems a lot of it
    is popping it up all over the web without my permission. Do you know any techniques to help reduce content from being stolen?
    I’d genuinely appreciate it.

  2. August 31st, 2014 at 02:55 | #2

    You need to take part in a contest for one of the greatest blogs online.
    I am going to recommend this site!

  3. September 1st, 2014 at 04:21 | #3

    This is the right web site for everyone who wants to find
    out about this topic. You realize so much its almost hard to
    argue with you (not that I personally would want to…HaHa).
    You definitely put a fresh spin on a topic which has been written about for years.
    Excellent stuff, just great!

  4. September 1st, 2014 at 18:10 | #4

    great publish, very informative. I wonder why the opposite specialists of this sector don’t understand this.
    You should continue your writing. I’m confident, you have a great readers’ base already!

  5. September 3rd, 2014 at 03:04 | #5

    Hello my loved one! I want to say that this article is amazing, nice written and include almost all important infos.

    I’d like to see extra posts like this .

  6. September 8th, 2014 at 08:41 | #6

    These tiny components can damage easily if a large amount of current passes through them.
    They also sustain enough space between the elements
    to allow warm dissipation. Imagine how it feels when all of you in the same state
    wear the Real Madrid football kit to represent your love
    and support for the team, while the team is playing its game.

  7. September 8th, 2014 at 15:52 | #7

    Your style is unique in comparison to other people I have read stuff from.
    Thank you for posting when you’ve got the opportunity, Guess I’ll just book mark
    this site.

  8. September 9th, 2014 at 08:40 | #8

    I’m really enjoying the theme/design of your blog.

    Do you ever run into any browser compatibility problems?

    A small number of my blog readers have complained about my website not operating correctly in Explorer but looks great in Firefox.
    Do you have any ideas to help fix this issue?

  9. September 10th, 2014 at 01:40 | #9

    Hmm it seems like your site ate my first comment (it
    was super long) so I guess I’ll just sum it up what I had written and say, I’m
    thoroughly enjoying your blog. I too am an aspiring blog writer but I’m still new to the whole thing.

    Do you have any points for novice blog writers?
    I’d definitely appreciate it.

  10. September 10th, 2014 at 02:58 | #10

    There are several operating systems that a user can use, such as Linux, Macintosh, UNIX and Windows.

    Research is something that will take up a lot
    of your time and energy. Dedicated hosting offers most of the resources in a server but is normally had at a top
    quality price.

  11. September 12th, 2014 at 20:48 | #11

    Write more, thats all I have to say. Literally, itt seems as though you relied on the vidro to make your point.
    You clearly know what youre talking about, why waswte youur intelligence on just
    posting videos to yoiur blog when you could be giving us something enlightening to
    read?

  12. September 14th, 2014 at 02:28 | #12

    Nice post. I learn something totally new and challenging on websites I stumbleupon every day.
    It’s always interesting to read content from other writers and practice a little something
    from other web sites.

  13. September 21st, 2014 at 03:08 | #13

    Hello there, just became aware off your blog through
    Google, and found that it’s truly informative.

    I’m gonna watch out for brussels. I’ll applreciate if
    you continue this in future. Numerous peopl will be benefited
    from your writing. Cheers!

  14. September 21st, 2014 at 04:11 | #14

    绉併伅鎰熼姌銈掑彈銇戙€佺銇█銈忋仾銇戙倢銇般仾銈夈仾銇勩€?

  15. September 21st, 2014 at 11:25 | #15

    This site definitely has all the info I needed concerning this subject and didn’t know who to ask.

Comment pages
1 2 650
  1. April 27th, 2010 at 05:17 | #1
  2. September 4th, 2014 at 00:59 | #2