Regression Example with MXNet in R

   In this tutorial, we'll learn how to train and predict regression data with MXNet deep learning framework in R. This link explains how to install R MXNet package. To create a neural network model, we use the MXNet feedforward neural network function, mx.model.FeedForward.create() and set linear regression for the output layer with the mx.symbol.LinearRegressionOutput() function.
   The post covers:
  1. Preparing data
  2. Building the model
  3. Predicting data and plotting the result
  4. Checking the accuracy
   We'll start by loading the mxnet package in R.

library(mxnet)


Preparing data

   In this tutorial, we'll create regression dataset with a simple rule below and split it into the train and test parts after separating its x-input and y-output parts.

N = 450
n = seq(1:N)
a = n/10+4*sin(n/10)+sample(-1:6,N,replace=T)+rnorm(N)
b = n/8+4*sin(n/10)+sample(-3:3,N,replace=T)+rnorm(N) 
c = n/6+4*sin(n/10)+sample(-5:1,N,replace=T)+rnorm(N)
y = (a+b+c)/3+rnorm(N)
 
df = data.frame(a,b,c,y)
head(df)
         a          b          c          y
1 1.559084 -3.4897805 -0.6143715 -1.6718729
2 5.290081 -2.7466366  1.3203803  1.0249076
3 2.764863 -1.1433255 -3.1107472 -0.5660052
4 8.842324  0.6967253  1.1809927  5.5651492
5 7.402110  0.6273192 -1.7591250  0.9609917
6 3.813864  2.3840024  3.4351513  2.1177884
 
x = data.matrix(df[,-4])
y = df[,4]
 
train_x = x[1:400,]
test_x = x[401:N,]
 
train_y = y[1:400]
test_y = y[401:N]


Building the model

Next, we'll prepare the parameters and create the model with the parameters and training data. We use mx.model.FeedForward.create() function to create a model.

data = mx.symbol.Variable("data")
fc1 = mx.symbol.FullyConnected(data, num_hidden=1)
lro = mx.symbol.LinearRegressionOutput(fc1)
 
mx.set.seed(0)
model = mx.model.FeedForward.create(lro, 
                                     X=train_x, y=train_y,
                                     ctx=mx.cpu(),     
                                     num.round=40, 
                                     array.batch.size=20,
                                     learning.rate=0.00001, 
                                     momentum=0.9,  
                                     eval.metric=mx.metric.mse)
Start training with 1 devices
[1] Train-mse=279.624058824746
[2] Train-mse=41.5399258801105
[3] Train-mse=4.71303664679869
[4] Train-mse=1.75548642196466
[5] Train-mse=0.899442114957017
[6] Train-mse=0.966937831006965
[7] Train-mse=0.918482769352445
[8] Train-mse=0.929133895446455
[9] Train-mse=0.924377720609216
[10] Train-mse=0.924362452519844
[11] Train-mse=0.923188036572499
[12] Train-mse=0.922308222367186
[13] Train-mse=0.921425433957493
[14] Train-mse=0.920561695364338
[15] Train-mse=0.919737325637187
[16] Train-mse=0.918934431886255
[17] Train-mse=0.918159988889264
[18] Train-mse=0.917409833462004
[19] Train-mse=0.916684650362127
[20] Train-mse=0.915982854526742
[21] Train-mse=0.915305351405739
[22] Train-mse=0.914648634659553
[23] Train-mse=0.914014018268204
[24] Train-mse=0.913400324399546
[25] Train-mse=0.912804778154901
[26] Train-mse=0.912231012459789
[27] Train-mse=0.911675292090948
[28] Train-mse=0.911137063780071
[29] Train-mse=0.910616904537146
[30] Train-mse=0.910112880176902
[31] Train-mse=0.90962559830598
[32] Train-mse=0.909155088548903
[33] Train-mse=0.908699242024499
[34] Train-mse=0.908257617779034
[35] Train-mse=0.907831121553153
[36] Train-mse=0.907417949508187
[37] Train-mse=0.907018242374173
[38] Train-mse=0.906631261404693
[39] Train-mse=0.906257077384882
[40] Train-mse=0.905894270687463


Predicting data and plotting the result

First, we'll fit the train data and visualize it.

y_pred = predict(model, train_x)

x_axes = seq(1:length(y_pred))
plot(x_axes, train_y, type="l", col="red")
lines(x_axes, y_pred, col="blue")
legend("topleft", legend=c("y-original", "y-predicted"),
       col=c("red", "blue"), lty=1,cex=0.8,bty = "n") 

 Next, we'll predict test data and visualize it in a plot.

x_axes = seq(1:length(test_y_pred))
 
plot(x_axes, test_y, col="red", type="l")
lines(x_axes, test_y_pred, col="blue")
legend("topleft", legend=c("y-original", "y-predicted"),
       col=c("red", "blue"), lty=1,cex=0.8, bty = "n")




Checking the accuracy

We'll check the MAE, MSE, RMSE, and R-squared rate of predicted result.

d = test_y-test_y_pred
mse = mean((d)^2)
mae = mean(abs(d))
rmse = sqrt(mse)
R2 = 1-(sum((d)^2)/sum((test_x-mean(test_x))^2))
 
cat(" MAE:", mae, "\n", "MSE:", mse, "\n", 
    "RMSE:", rmse, "\n", "R-squared:", R2)
 MAE: 0.7153431 
 MSE: 0.8252871 
 RMSE: 0.9084531 
 R-squared: 0.997569

   In this post, we've briefly learned how to use MXNet to analyze regression data.
The full source code is listed below.

library(mxnet)
 
set.seed(123)
N = 450
n = seq(1:N)
a = n/10+4*sin(n/10)+sample(-1:6,N,replace=T)+rnorm(N)
b = n/8+4*sin(n/10)+sample(-3:3,N,replace=T)+rnorm(N) 
c = n/6+4*sin(n/10)+sample(-5:1,N,replace=T)+rnorm(N)
y = (a+b+c)/3+rnorm(N)
 
df = data.frame(a,b,c,y)
head(df)
 
x = data.matrix(df[,-4])
y = df[,4]
 
train_x = x[1:400,]
test_x = x[401:N,]
 
train_y = y[1:400]
test_y = y[401:N]
 
data = mx.symbol.Variable("data")
fc1 = mx.symbol.FullyConnected(data, num_hidden=1)
lro = mx.symbol.LinearRegressionOutput(fc1)
 
mx.set.seed(0)
model = mx.model.FeedForward.create(lro, 
                                     X=train_x, y=train_y,
                                     ctx=mx.cpu(),     
                                     num.round=40, 
                                     array.batch.size=10,
                                     learning.rate=0.0001, 
                                     momentum=0.95,  
                                     eval.metric=mx.metric.rmse)
 
y_pred = predict(model, train_x)
x_axes = seq(1:length(y_pred))
plot(x_axes, train_y, type="l", col="red")
lines(x_axes, y_pred, col="blue")
legend("topleft", legend=c("y-original", "y-predicted"),
       col=c("red", "blue"), lty=1,cex=0.8,bty = "n")

test_y_pred = predict(model, test_x)
 
x_axes = seq(1:length(test_y_pred))
plot(x_axes, test_y, col="red", type="l")
lines(x_axes, test_y_pred, col="blue")
legend("topleft", legend=c("y-original", "y-predicted"),
       col=c("red", "blue"), lty=1,cex=0.8, bty = "n")
 
d = test_y-test_y_pred
mse = mean((d)^2)
mae = mean(abs(d))
rmse = sqrt(mse)
R2 = 1-(sum((d)^2)/sum((test_x-mean(test_x))^2))
 
cat(" MAE:", mae, "\n", "MSE:", mse, "\n", 
    "RMSE:", rmse, "\n", "R-squared:", R2)


Reference:
  1. MXNet for R Tutorials

1 comment:

  1. Very helpful, thanks!
    How can i plot error vs epoch for the models?

    ReplyDelete