Neural Networks with R – A Simple Example

In this tutorial a neural network (or Multilayer perceptron depending on naming convention) will be build that is able to take a number and calculate the square root (or as close to as possible). Later tutorials will build upon this to make forcasting / trading models.The R library ‘neuralnet’ will be used to train and build the neural network.

There is lots of good literature on neural networks freely available on the internet, a good starting point is the neural network handout by Dr Mark Gayles at the Engineering Department Cambridge University http://mi.eng.cam.ac.uk/~mjfg/local/I10/i10_hand4.pdf, it covers just enough to get an understanding of what a neural network is and what it can do without being too mathematically advanced to overwhelm the reader.

The tutorial will produce the neural network shown in the image below. It is going to take a single input (the number that you want square rooting) and produce a single output (the square root of the input). The middle of the image contains 10 hidden neurons which will be trained. The output of the script will look like:

Input Expected Output Neural Net Output

Input 	Expected Output		 Neural Net Output
1               1     		 0.9623402772
4               2     		 2.0083461217
9               3     		 2.9958221776
16               4     		 4.0009548085
25               5     		 5.0028838579
36               6     		 5.9975810435
49               7    		 6.9968278722
64               8    		 8.0070028670
81               9    		 9.0019220736
100              10    		 9.9222007864

As you can see the neural network does a reasonable job at finding the square root, the largest error in in finding the square root of 1 which is out by ~4%

Onto the code:

?View Code RSPLUS
 install.packages('neuralnet') library("neuralnet")   #Going to create a neural network to perform sqare rooting #Type ?neuralnet for more information on the neuralnet library   #Generate 50 random numbers uniformly distributed between 0 and 100 #And store them as a dataframe traininginput <- as.data.frame(runif(50, min=0, max=100)) trainingoutput <- sqrt(traininginput)   #Column bind the data into one variable trainingdata <- cbind(traininginput,trainingoutput) colnames(trainingdata) <- c("Input","Output")   #Train the neural network #Going to have 10 hidden layers #Threshold is a numeric value specifying the threshold for the partial #derivatives of the error function as stopping criteria. net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01) print(net.sqrt)   #Plot the neural network plot(net.sqrt)   #Test the neural network on some training data testdata <- as.data.frame((1:10)^2) #Generate some squared numbers net.results <- compute(net.sqrt, testdata) #Run them through the neural network   #Lets see what properties net.sqrt has ls(net.results)   #Lets see the results print(net.results\$net.result)   #Lets display a better version of the results cleanoutput <- cbind(testdata,sqrt(testdata), as.data.frame(net.results\$net.result)) colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output") print(cleanoutput)

27 thoughts on “Neural Networks with R – A Simple Example”

1. Pingback: AI | Pearltrees

2. hi
regards
stefan mudry

3. Christy on said:

Do you have any other examples where you use neural nets or support vector machines in R for forecasting?

4. Rounak Drolia on said:

Hi, COntinue with the example, suppose now I want to predict the oyput of the Following Input Numbers: 2378,232,244.
How will I do it using this trained neural network.. Please reply
Thanks

5. Sorry for the delay, if you do this you are testing (running in the NN) your unexplored data (testdata) against the previously trained model (net.sqrt):
net.results <- compute(net.sqrt, testdata)

6. dallyon on said:

hi,
i’m using this package and would like to know if the network “sees” the data provided in the order given or does it randomize it for training. this info is very important for me to know for a project in which i’m working right now.
thnx

7. Daia Alexandru on said:

Hello GekkoQuant , do you have some example with neural net for multivariate inputs ?

8. Abhishek Dubey on said:

Thank you so much for the short and nice example 🙂

In the line
net.sqrt <- neuralnet(Output~Input,trainingdata, hidden=10, threshold=0.01)

you can update: hidden=c(10,8), just to show the example for multiple hidden layers

P.S: It took me a while to figure it out of my own

9. Anne on said:

Great example and very useful for beginners 🙂

10. farnk on said:

I don’t understand the training part. Normally you use a training data set to improve your algorithm. When the algorithm can distinguish the different clusters within your data you use this on real data. It is not clear for me how to use this trained neural network algorithm on real data.

11. Omar Khazamov on said:

It would be great if author posted a sequal to the article to demonstrate how neuralnet package can differentiate quadratic function from cube using example similar to the one above.

12. Pingback: R |

13. David on said:

I have modified the original code, I have traininginput only goes from 0 to 25, but it seems that the rescue are not right.

14. Ric on said:

Very good example!

I tried to use the backpropagation algorithm, but could not reproduce the results:

net.sqrt <- neuralnet(Output~Input, trainingData, hidden=10, linear.output=TRUE, algorithm="backprop", learningrate=0.01)

Regards,
Ric

15. Varun Mallya on said:

Hey i am getting Error in x – y : non-conformable arrays. any idea why? :-/

16. Sathish kumar on said:

Simple and Good example, nicely demonstrated

17. Elihu Feustel on said:

I cut and pasted your code. Most of the results were similar, but the Net Output for “1” was 4.48, instead of the expected 1. The remainder were very close.

Any idea what would cause this?

18. Mathieu on said:

If you try it on unknown numbers you want to know the square root, it doesn’t work well. Probably the network needs improvement in its structure. From 11, you get :
[11,] 10.837801708
[12,] 11.427199303
[13,] 11.749926314
[14,] 11.897155658
[15,] 11.956657422
[16,] 11.978826629
[17,] 11.986601940
[18,] 11.989196143
whereas one would expect 11, 12, 13, 14, …

19. It seems hidden=10 indicates that there will be one layer, in this layer, there would be 10 neurons.
c(10, 8) indicates that: two layer, the first has 10 neurons, the next has 8 neurons