How to do it...

We are now familiar with the dataset. Let's now move on to building and training the neural network:

  1. Let's create a neural network. The first line of the following code block creates a symbol variable and the next lines add hidden layers:
in_layer <- mx.symbol.Variable("data")
layer1 = mx.symbol.FullyConnected(in_layer,name="dense1",num_hidden=64)
activation1 <- mx.symbol.Activation(layer1, name="relu1", act_type="relu")
layer2 = mx.symbol.FullyConnected(activation1,name="dense2",num_hidden=64)
activation2 <- mx.symbol.Activation(layer2, name="relu2", act_type="relu")
layer3 = mx.symbol.FullyConnected(activation2,name="dense3",num_hidden=1)
out = mx.symbol.LinearRegressionOutput(layer3)
  1. We set the device to use, for training the model:
devices <- mx.cpu()

  1. We proceed to set the seed and define the number of epochs. Then, we train the model:
mx.set.seed(0)
epochs = 100
model = mx.model.FeedForward.create(symbol =out,X =train_x,y = train_y,ctx = devices,num.round = epochs,optimizer = "rmsprop",array.batch.size = 50,learning.rate=0.001,eval.metric = mx.metric.rmse)
  1. Let's visualize the model:
graph.viz(model$symbol)

The following screenshot shows the visualization of the model:

  1. We evaluate model's performance on the test dataset:
predicted <- predict(model,test_x)
paste("Test error:",sqrt(mean((predicted-as.numeric(test_y))^2)))

The following screenshot shows the test error:

From the preceding screenshot, we can see that we got a test error of around 3.54.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset