Neural Networks is one of the most common machine learning algorithms and with good reason. Neural networks are particularly good when applied to problems, such as image recognition and natural language processing, where there is a large amount of input data.
Through an input layer, one or more hidden layers, and an output layer, a neural network works by connecting up a series of neurons with weights assigned to each connection. As each connection is activated, a calculation is performed on the connection before passing through an activation function at each level of the hidden layers. Commonly, these activation functions are either the RELU, sigmoid or tanh. Their purpose is usually to determine whether the next layer should be activated.
Within this tutorial, we’re going to develop a very simple classification neural network on the commonly used iris dataset. Before starting, you should install the neuralnet, ggplot2, dplyr, and reshape2 libraries. Solutions to these exercises are available here.
Load the neuralnet, ggplot2, and dplyr libraries, along with the iris dataset. Set the seed to 123.
Explore the distributions of each feature present in the iris dataset. Feel free to get creative here. I myself opted for a violin plot.
Convert your observation class and Species into one hot vector.
Write a generic function to standardize a column of data.
Now standardize the predictors. Hint: lapply will be useful here.
Combine your one hot labels and standardized predictors.
Define your formula that your neuralnet will be run on. You’ll need to use the as.formula function here.
Create a neural network object, now using the tanh function and two hidden layers of size 16 and 12. You will also need to tell the neural network that you’re performing a classification algorithm here, not regression. You’ll want to refer to the neuralnet documentation as to how to define this.
Plot your neural network.
Using the compute function and your neural network object’s net.result attribute, calculate the overall accuracy of your neural network.
- Become a Top R Programmer Fast with our Individual Coaching Program
- Explore all our (>4000) R exercises
- Find an R course using our R Course Finder directory
- Subscribe to receive weekly updates and bonus sets by email
- Share with your friends and colleagues using the buttons below