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, 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
#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 <-, 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)
#Plot the neural network
#Test the neural network on some training data
testdata <-^2) #Generate some squared numbers
net.results <- compute(net.sqrt, testdata) #Run them through the neural network
#Lets see what properties net.sqrt has
#Lets see the results
#Lets display a better version of the results
cleanoutput <- cbind(testdata,sqrt(testdata),
colnames(cleanoutput) <- c("Input","Expected Output","Neural Net Output")

26 thoughts on “Neural Networks with R – A Simple Example

  1. Pingback: AI | Pearltrees

  2. Pingback: Visualizing neural networks from the nnet package – R is my friend

  3. Pingback: Visualizing neural networks from the nnet package | spider's space

  4. 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

  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. Pingback: Neural Networks with R – A Simple Example | Gekko Quant – Quantitative Trading | luiz p. c. de freitas

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

  8. Pingback: Пример обучения нейронной сети на R (перевод) | Кукл.РФ

  9. Pingback: R365: Day 23 – Neural Networks | Something Like Science

  10. 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

  11. 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.

  12. Pingback: Dedomenocracy: unsupervised government | kenneumeister

  13. 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.

  14. Pingback: R |

  15. Pingback: Neural Networks with R by Gekko Quant | Sentient but Algorithmic

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


  17. 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. 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, …

Leave a Reply

Your email address will not be published. Required fields are marked *