Neural Networks from Scratch using R

Just for fun I decided to code up a basic neural network from scratch using the R language. I did so using the R6 OOP model. My three observations/conclusions are:

1. Because R is interpreted, R is too slow for large problems.
2. R6 doesn’t provide enough advantage over RC to warrant its use.
3. Coding raw R isn’t too difficult.


First I installed the non-base R6 OOP package. The R language has three base, built-in, OOP models — S3, S4, RC. I figured I’d try the non-built-in R6 model. R6 is very much like RC but R6 has private data members as opposed to RC where everything is public. Anyway, I didn’t see enough advantage to R6 to warrant the extra installation.

I’ve coded up neural networks several times, in different languages. As with any language, it took me a few hours to get used to the syntax (most of my coding during the past few months has been in C# and JavaScript). For me, R syntax is not significantly better or worse than any other C-family language.

I created a separate R file to hold the neural network definition. Then I wrote a calling script. I used the classic Iris data set where the goal is to predict the species (setosa, versicolor, or virginica) of an iris flower based on the flower’s petal length and width, and the sepal (a green leaf-like structure) length and width.


When I ran the demo script, it took about 15 seconds to train a model, using the 150-item data set and only 100 training iterations. Ouch. This is very, very slow. I got 98.67% accuracy which I know is a reasonable result based on my implementations in other languages.

Well, it was a fun experiment. There are a few non-built-in R neural network packages, including one named nnet, but they all have weaknesses. If I had a month to spare, I’d code an R neural network package using a combination of C/C++ (for speed) and an R front end.

This entry was posted in Machine Learning, R Language. Bookmark the permalink.