Created
December 4, 2018 13:06
-
-
Save korkridake/d88dd2a9de29b1afbeb8d46e45ca55ce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # ------------------------------------------------------------------------------------------- | |
| # ------------------------------------------------------------------------------------------- | |
| # Develop a Neural Network with MXNet in Five Minutes (Regression) | |
| # R Notebook Author: @Korkrid Akepanidtaworn | |
| # Tutorial from MXNet Community | |
| # Source: https://mxnet.incubator.apache.org/tutorials/r/fiveMinutesNeuralNetwork.html | |
| # ------------------------------------------------------------------------------------------- | |
| # ------------------------------------------------------------------------------------------- | |
| library(mlbench) | |
| library(tidyverse) | |
| library(caret) | |
| # To install MXNet, you need to run these 4 lines of codes | |
| cran <- getOption("repos") | |
| cran["dmlc"] <- "https://apache-mxnet.s3-accelerate.dualstack.amazonaws.com/R/CRAN/" | |
| options(repos = cran) | |
| # install.packages("mxnet") | |
| library(mxnet) | |
| # ------------------------------------------------------------------------------------------- | |
| # ------------------------------------------------------------------------------------------- | |
| # Load Data | |
| # ------------------------------------------------------------------------------------------- | |
| # ------------------------------------------------------------------------------------------- | |
| data(BostonHousing, package="mlbench") | |
| # ------------------------------------------------------------------------------------------- | |
| # Split Training/Testing 80:20 | |
| # ------------------------------------------------------------------------------------------- | |
| # ------------------------------------------------------------------------------------------- | |
| # 80% of the sample size | |
| smp_size <- floor(0.80 * nrow(BostonHousing)) | |
| # Set the seed to make your partition reproducible | |
| set.seed(1234) | |
| train_ind <- sample(seq_len(nrow(BostonHousing)), size = smp_size) | |
| train <- BostonHousing[train_ind, ] | |
| test <- BostonHousing[-train_ind, ] | |
| train_x = as.matrix(train[, 1:13]) | |
| class(train_x) <- "numeric" | |
| train_y = as.numeric(as.matrix(train[, 14])) | |
| test_x = as.matrix(test[, 1:13]) | |
| class(test_x) <- "numeric" | |
| test_y = as.numeric(as.matrix(test[, 14])) | |
| # Define the input data | |
| data <- mx.symbol.Variable("data") | |
| # A fully connected hidden layer | |
| # data: input source | |
| # num_hidden: number of neurons in this hidden layer | |
| fc1 <- mx.symbol.FullyConnected(data, num_hidden=1) | |
| # Use linear regression for the output layer | |
| lro <- mx.symbol.LinearRegressionOutput(fc1) | |
| mx.set.seed(1234) | |
| model <- mx.model.FeedForward.create(symbol = lro, | |
| X=train_x, | |
| y=train_y, | |
| ctx=mx.cpu(), | |
| num.round=50, | |
| array.batch.size=20, | |
| learning.rate=2e-6, | |
| momentum=0.9, | |
| eval.metric= mx.metric.rmse) | |
| ## Auto detect layout of input matrix, use rowmajor. | |
| ## Start training with 1 devices | |
| ## [1] Train-rmse=16.063282524034 | |
| ## [2] Train-rmse=12.2792375712573 | |
| ## [3] Train-rmse=11.1984634005885 | |
| ## [4] Train-rmse=10.2645236892904 | |
| ## [5] Train-rmse=9.49711005504284 | |
| ## [6] Train-rmse=9.07733734175182 | |
| ## [7] Train-rmse=9.07884450847991 | |
| ## [8] Train-rmse=9.10463850277417 | |
| ## [9] Train-rmse=9.03977049028532 | |
| ## [10] Train-rmse=8.96870685004475 | |
| ## [11] Train-rmse=8.93113287361574 | |
| ## [12] Train-rmse=8.89937257821847 | |
| ## [13] Train-rmse=8.87182096922953 | |
| ## [14] Train-rmse=8.84476075083586 | |
| ## [15] Train-rmse=8.81464673014974 | |
| ## [16] Train-rmse=8.78672567900196 | |
| ## [17] Train-rmse=8.76265872846474 | |
| ## [18] Train-rmse=8.73946101419974 | |
| ## [19] Train-rmse=8.71651926303267 | |
| ## [20] Train-rmse=8.69457600919277 | |
| ## [21] Train-rmse=8.67354928674563 | |
| ## [22] Train-rmse=8.65328755392436 | |
| ## [23] Train-rmse=8.63378039680078 | |
| ## [24] Train-rmse=8.61488162586984 | |
| ## [25] Train-rmse=8.5965105183022 | |
| ## [26] Train-rmse=8.57868133563275 | |
| ## [27] Train-rmse=8.56135851937663 | |
| ## [28] Train-rmse=8.5444819772098 | |
| ## [29] Train-rmse=8.52802114610432 | |
| ## [30] Train-rmse=8.5119504512622 | |
| ## [31] Train-rmse=8.49624261719241 | |
| ## [32] Train-rmse=8.48087453238701 | |
| ## [33] Train-rmse=8.46582689119887 | |
| ## [34] Train-rmse=8.45107881002491 | |
| ## [35] Train-rmse=8.43661331401712 | |
| ## [36] Train-rmse=8.42241575909639 | |
| ## [37] Train-rmse=8.40847217331365 | |
| ## [38] Train-rmse=8.39476931796395 | |
| ## [39] Train-rmse=8.38129658373974 | |
| ## [40] Train-rmse=8.36804269059018 | |
| ## [41] Train-rmse=8.35499817678397 | |
| ## [42] Train-rmse=8.34215505742154 | |
| ## [43] Train-rmse=8.32950441908131 | |
| ## [44] Train-rmse=8.31703985777311 | |
| ## [45] Train-rmse=8.30475363906755 | |
| ## [46] Train-rmse=8.29264031506106 | |
| ## [47] Train-rmse=8.28069372820073 | |
| ## [48] Train-rmse=8.26890902770415 | |
| ## [49] Train-rmse=8.25728089053853 | |
| ## [50] Train-rmse=8.24580511500735 | |
| pred_test = predict(model, test_x) # Array of Regression Predictions | |
| pred_test = as.numeric(t(pred_test)) # Vector of Predictions | |
| sqrt(mean((pred_test - test_y)^2)) # RMSE = 9.452149 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment