Neural Networks using H2o Package in R

What are Neural Networks?
An artificial neural network (or neural network for short) is a predictive model motivated by the way the brain operates. Think of the brain as a collection of neurons wired together. Each neuron looks at the outputs of the other neurons that feed into it, does a calculation, and then either fires or doesn’t.

Fig 1

Accordingly, artificial neural networks consist of artificial neurons, which perform similar calculations over their inputs. Neural networks can solve a wide variety of problems like handwriting recognition and face detection, and they are used heavily in deep learning, one of the trendiest subfields of data science. However, most neural networks are “Black Boxes”—inspecting their details don’t give you much understanding of how they’re solving a problem. And large neural networks can be difficult to train. For most problems you’ll encounter as a budding data scientist, they’re probably not the right choice. These might graphically look like

Fig 2

Where in the input layer consists of the independent variables and the output layer in the variable of interest that is the effect on which is to be measured or checked. The hidden layers are user-defined and play a crucial role in the accuracy of the model. The greater the number of the layers, the more complex our Neural Network model becomes.  Such networks with multiple layers are called Deep Neural Networks. To decide the number of layers or the number of nodes in a layer is quite a difficult task.

Unfortunately, there is no reliable rule to determine the number of neurons in the hidden layer. The appropriate number depends on the number of input nodes, the amount of training data, the amount of noisy data, and the complexity of the learning task, among many other factors. It is on the user to decide these parameters.

It has been proven that a neural network with at least one hidden layer of sufficient neurons is a universal function approximator. This means that neural networks can be used to approximate any continuous function to an arbitrary precision over a finite interval.

Step by Step Analysis
There are a  couple of different ways to build neural networks in R but the main focus in this blog will be using the H2o package for our analysis. We’ll be using the Concrete Data throughout our analysis, which is easily available on the UCI Machine Learning repository. We’ll follow a certain ladder of steps which will be a little different from the standard methods. Given by:

  1. Initialise the H2o package.
  2. Read in the data
  3. Data pre-processing if required
  4. Convert data into H2o readable format
  5. Split the data into training and testing sets
  6. Check for the accuracy of the model on the test data

First lets read in the data from the csv file and check its structure:

concrete <- read.csv(“concrete.csv”)
str(concrete)

So our data here has 1030 observations with 9 variables. Our variable of interest is the strength of concrete. We aim  to perform a regression task, where we try to predict the strength of the concrete on the basis of the remaining variables.

Before we proceed to the analysis part we do some changes in the data to ensure robustness in the final model. We can clearly see that the data has very high and low magnitude values. For this we need to first normalize the data:

normalise <- function(x){
return((x-min(x))/(max(x)-min(x)))
}
data <- as.data.frame(lapply(concrete,normalise))
str(data)

This now looks much better and cleaner. Moving on to the steps defined above, we begin our analysis using the H2o package.

install.packages(“h2o”)
library(h2o)
h2o.init(max_mem_size = “2G”, nthreads = 2, ip=”localhost”, port=54321)

This way we initialize the H2o package. Now we have the data that can be read in by the h2o package. Converting the data might take a few seconds, depending upon the configuration of your laptop. Wait until the conversion shows 100%. To check the first few values you can use the head function.

d.hex <- as.h2o(data,destination_frame = “d.hex”)
head(d.hex)

For splitting the dataset, instead of the caret package, we use the inbuilt functions from the h2o package only.

set.seed(99)
split <-h2o.splitFrame(data=d.hex,ratios = 0.75)
train <- split[[1]] > test <- split[[2]]

Now comes the main part of our analysis:

model_nn <- h2o.deeplearning(x=1:8,y=”strength”, training_frame = train,hidden =5,model_id = “model_nn”)

The code here explains that our independent variables are the first 8 variables and dependent variable is the “strength” variable. We use 5 nodes in the single hidden layer and assign model_nn as the model name.

To check the performance of the model on the test data:

perf <- h2o.performance(model_nn,test)
perf

We get a few performance measures but we stick to only RMSE at this point. The next step is to make predictions and to check how these predictions match with the test data.

pred<- as.data.frame(h2o.predict(model_nn, test))
test1 <- as.data.frame(test)
cor(pred,test1$strength)

In order to find the correlation between the predicted values and the strength variable in the test data we need to transform both to into a data frame first. The correlation comes out to be 82% which is quite good. But is much lower than the standard “neuralnet” method in R. You can always add more nodes and layers to check how accuracy changes and can select a model that gives a better correlation value.

This is how you can use H2o package to build Deep Learning models in R.

You might also like More from author