Sunteți pe pagina 1din 9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

CertPal
Technology and certifications Home General java Java certifications Web

Type text to search here...

Home > java > Java neural networks and Neuroph A tutorial

Java neural networks and Neuroph A tutorial


April 18th, 2010 CertPal Leave a comment Go to comments
tweets

17

retw eet

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.
www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/ 1/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

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:

www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/

2/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

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:

www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/

3/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

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 ):

www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/

4/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

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:
pbi casTsTafcerl ulc ls etrfiNua { NuaNtokntok=NuaNtokla(nua_rfi.nt) erlewr ewr erlewr.od"erltafcne"; pbi sai vi mi(tig]ag) ulc ttc od anSrn[ rs { nwTsTafcerl)g(; e etrfiNua(.o) } piaevi g( rvt od o) { cluae100; aclt(,,) cluae010; aclt(,,) cluae001; aclt(,,) }

www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/

5/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

piaevi cluaedul..ipt rvt od aclt(obe. nu) { ntokstnu(nu) ewr.eIptipt; ntokcluae) ewr.aclt(; Vco otu =ntokgtupt) etr upt ewr.eOtu(; Dul ase =otu.e() obe nwr uptgt0; Sse.u.rnl(nwr; ytmotpitnase) } }

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.

28

Categories: java Tags: java, network, neural Comments (8) Trackbacks (1) Leave a comment Trackback 1. Bee April 8th, 2011 at 10:27 | #1 Reply | Quote

tweets

17

retw eet

I tried running the code above of loading a neural network into java but Im 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. CertPal April 23rd, 2011 at 14:24 | #2 Reply | Quote @Bee Perhaps the next version of the library has changed the return type ? A ClassCastException should be
www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/ 6/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

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 Reply | Quote 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 Reply | Quote 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 Reply | Quote sir it is showing error like network class not found .do i have to code separate network class or what should i import.also cant i see the code of my neroph project 6. rahul May 9th, 2012 at 02:12 | #6 Reply | Quote 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 Reply | Quote sir i am trying to implement nn for credit approval .i need dataset that is compatible with nueroph studio
www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/ 7/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

.plz tell me url through which i can get datset 8. Nomiluks August 12th, 2012 at 05:03 | #8 Reply | Quote I think u need to convert your out to string like, arrays.toString(output); then put this in system.out . @rahul 1. April 27th, 2010 at 05:17 | #1 Newsletter KW 17 / 2010 Name (required) E-Mail (will not be published) (required) Website

Subscribe to comments feed


Submit Comment

Eclipse and the conflicting shortcut problem What is an AtomServer ? RSS

Twitter

Archives
July 2011 (2) June 2011 (2) April 2011 (1) March 2011 (1) February 2011 (1)
www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/ 8/9

9/17/12

Java neural networks and Neuroph A tutorial - CertPal

January 2011 (2) December 2010 (2) November 2010 (3) October 2010 (3) August 2010 (2) July 2010 (1) June 2010 (4) May 2010 (3) April 2010 (4) March 2010 (5) February 2010 (5) January 2010 (3) December 2009 (2) November 2009 (4) October 2009 (8) September 2009 (10) August 2009 (10)

Categories
General java Java certifications Web

Recent Posts
Why certification related career goals hurt everyone Java webcam support using gstreamer Delving deeper into Jconsole and VisualVm Protobufs and other custom formats Finding text similarities with fuzzy hashes Duplicate code for example Using greasemonkey to prototype your web UI Java and immutability Antlr tutorial: Hello Antlr CodePro overview Find open files in linux using lsof Jettison A JSON streaming parser that uses Stax Using a PipedInputstream and PipedOutputstream System.out.println performance Learning from design mistakes Will you pay for a JVM on Mac OS X Top WordPress Copyright 2009-2011 CertPal | Privacy Policy | License Theme by NeoEase. Valid XHTML 1.1 and CSS 3.

www.certpal.com/blogs/2010/04/java-neural-networks-and-neuroph-a-tutorial/

9/9

S-ar putea să vă placă și