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

[…] Neural Networks with R – A Simple Example | Gekko Quant – Quantitative Trading Category:Reinforcement learning This is a book that touches on a lot of topic on AI and machine learning. This is probably the closest to "Code Complete" of AI. Peter Norvig, one of the authoer, is now working at Google. […]

hi

please send me your telephone number

regards

stefan mudry

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

[…] package also offers a plot method for neural network objects and I encourage interested readers to check it out. I have created the function for the nnet package given my own preferences for aesthetics, so its […]

[…] package also offers a plot method for neural network objects and I encourage interested readers to check it out. I have created the function for the nnet package given my own preferences for aesthetics, so its […]

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

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)

[…] http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-example/ This entry was posted in Uncategorized on June 4, 2013 by admin. […]

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

[…] Оригинал статьи здесь. […]

[…] article (http://gekkoquant.com/2012/05/26/neural-networks-with-r-simple-example/) gives a lucid and well-annotated example of how to use {neuralnet} to calculate square roots of an […]

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

Thanks for the great code as well as the comment! I have referenced it on my own site , http://use-r.com. Great works!

http://use-r.com

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

Great example and very useful for beginners

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.

[…] I encountered another more basic example in a simple demonstration of using machine learning module in the R language. This is just meant as a simple demo and not a […]

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.

[…] Neural Networks […]

[…] Neural Networks with R by Gekko Quant […]

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

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