ankkhedia commented on a change in pull request #12664: [MXNET-637] Multidimensional LSTM example for MXNetR URL: https://github.com/apache/incubator-mxnet/pull/12664#discussion_r221105293
########## File path: R-package/vignettes/MultidimLstm.Rmd ########## @@ -0,0 +1,401 @@ +LSTM time series example +============================================= + +This tutorial shows how to use an LSTM model with multivariate data, and generate predictions from it. For demonstration purposes, we used an open source [pollution data](https://archive.ics.uci.edu/ml/datasets/Beijing+PM2.5+Data). +The tutorial is an illustration of how to use LSTM models with MXNet-R. We are forecasting the air pollution with data recorded at the US embassy in Beijing, China for five years. + +Dataset Attribution: +"PM2.5 data of US Embassy in Beijing" +We want to predict pollution levels(PM2.5 concentration) in the city given the above dataset. + +```r +Dataset description: +No: row number +year: year of data in this row +month: month of data in this row +day: day of data in this row +hour: hour of data in this row +pm2.5: PM2.5 concentration +DEWP: Dew Point +TEMP: Temperature +PRES: Pressure +cbwd: Combined wind direction +Iws: Cumulated wind speed +Is: Cumulated hours of snow +Ir: Cumulated hours of rain +``` + +We use past PM2.5 concentration, dew point, temperature, pressure, wind speed, snow and rain to predict +PM2.5 concentration levels. + +Load and pre-process the data +--------- +The first step is to load in the data and preprocess it. It is assumed that the data has been downloaded in a .csv file: data.csv from the [pollution dataset](https://archive.ics.uci.edu/ml/datasets/Beijing+PM2.5+Data) + + ```r +## Loading required packages +library("readr") +library("dplyr") +library("mxnet") +library("abind") + ``` + + + + ```r +mx.set.seed(1234) +## Preprocessing steps +Data <- read.csv(file = "/Users/khedia/Downloads/data.csv", header = TRUE, sep = ",") + +## Extracting specific features from the dataset as variables for time series We extract +## pollution, temperature, pressue, windspeed, snowfall and rainfall information from dataset +df <- data.frame(Data$pm2.5, Data$DEWP, Data$TEMP, Data$PRES, Data$Iws, Data$Is, Data$Ir) +df[is.na(df)] <- 0 + +## Now we normalise each of the feature set to a range(0,1) +df <- matrix(as.matrix(df), ncol = ncol(df), dimnames = NULL) +rangenorm <- function(x) { + (x - min(x))/(max(x) - min(x)) +} +df <- apply(df, 2, rangenorm) +df <- t(df) + ``` +For using multidimesional data with MXNet-R, we need to convert training data to the form +(n_dim x seq_len x num_samples). For one-to-one RNN flavours labels should be of the form (seq_len x num_samples) while for many-to-one flavour, the labels should be of the form (1 x num_samples). Please note that MXNet-R currently supports only these two flavours of RNN. +We have used n_dim = 7, seq_len = 100, and num_samples = 430 because the dataset has 430 samples, each the length of 100 timestamps, we have seven time series as input features so each input has dimesnion of seven at each time step. + + +```r +n_dim <- 7 +seq_len <- 100 +num_samples <- 430 + +## extract only required data from dataset +trX <- df[1:n_dim, 25:(24 + (seq_len * num_samples))] + +## the label data(next PM2.5 concentration) should be one time step +## ahead of the current PM2.5 concentration +trY <- df[1, 26:(25 + (seq_len * num_samples))] + +## reshape the matrices in the format acceptable by MXNetR RNNs +trainX <- trX +dim(trainX) <- c(n_dim, seq_len, num_samples) +trainY <- trY +dim(trainY) <- c(seq_len, num_samples) +``` + + + +Defining and training the network +--------- + +```r +batch.size <- 32 + +# take first 300 samples for training - remaining 100 for evaluation +train_ids <- 1:300 +eval_ids <- 301:400 + +## The number of samples used for training and evaluation is arbitrary. I have kept aside few +## samples for testing purposes create dataiterators +train.data <- mx.io.arrayiter(data = trainX[, , train_ids, drop = F], label = trainY[, train_ids], + batch.size = batch.size, shuffle = TRUE) + +eval.data <- mx.io.arrayiter(data = trainX[, , eval_ids, drop = F], label = trainY[, eval_ids], + batch.size = batch.size, shuffle = FALSE) + +## Create the symbol for RNN +symbol <- rnn.graph(num_rnn_layer = 1, num_hidden = 5, input_size = NULL, num_embed = NULL, num_decode = 1, + masking = F, loss_output = "linear", dropout = 0.2, ignore_label = -1, cell_type = "lstm", output_last_state = T, + config = "one-to-one") + +mx.metric.mse.seq <- mx.metric.custom("MSE", function(label, pred) { + label = mx.nd.reshape(label, shape = -1) + pred = mx.nd.reshape(pred, shape = -1) + res <- mx.nd.mean(mx.nd.square(label - pred)) + return(as.array(res)) +}) + + + +ctx <- mx.cpu() + +initializer <- mx.init.Xavier(rnd_type = "gaussian", factor_type = "avg", magnitude = 3) + +optimizer <- mx.opt.create("adadelta", rho = 0.9, eps = 1e-05, wd = 1e-06, clip_gradient = 1, rescale.grad = 1/batch.size) + +logger <- mx.metric.logger() +epoch.end.callback <- mx.callback.log.train.metric(period = 10, logger = logger) + +## train the network +system.time(model <- mx.model.buckets(symbol = symbol, train.data = train.data, eval.data = eval.data, + num.round = 100, ctx = ctx, verbose = TRUE, metric = mx.metric.mse.seq, initializer = initializer, + optimizer = optimizer, batch.end.callback = NULL, epoch.end.callback = epoch.end.callback)) +ctx <- mx.cpu() +``` +Output: +``` +Start training with 1 devices +[1] Train-MSE=0.197570244409144 +[1] Validation-MSE=0.0153861071448773 +[2] Train-MSE=0.0152517843060195 +[2] Validation-MSE=0.0128299412317574 +[3] Train-MSE=0.0124418652616441 +[3] Validation-MSE=0.010827143676579 +[4] Train-MSE=0.0105128229130059 +[4] Validation-MSE=0.00940261723008007 +[5] Train-MSE=0.00914482437074184 +[5] Validation-MSE=0.00830172537826002 +[6] Train-MSE=0.00813581114634871 +[6] Validation-MSE=0.00747016374953091 +[7] Train-MSE=0.00735094994306564 +[7] Validation-MSE=0.00679832429159433 +[8] Train-MSE=0.00672049634158611 +[8] Validation-MSE=0.00623159145470709 +[9] Train-MSE=0.00620287149213254 +[9] Validation-MSE=0.00577476259786636 +[10] Train-MSE=0.00577280316501856 +[10] Validation-MSE=0.00539038667920977 +[11] Train-MSE=0.00540679777041078 +[11] Validation-MSE=0.00506085657980293 +[12] Train-MSE=0.0050867410376668 +[12] Validation-MSE=0.00477395416237414 +[13] Train-MSE=0.00480019277893007 +[13] Validation-MSE=0.00450056773843244 +[14] Train-MSE=0.00453343892004341 +[14] Validation-MSE=0.00424888811539859 +[15] Train-MSE=0.00428280527703464 +[15] Validation-MSE=0.00400642631575465 +[16] Train-MSE=0.00405749503988773 +[16] Validation-MSE=0.00380465737544 +[17] Train-MSE=0.00386031914968044 +[17] Validation-MSE=0.00360809749690816 +[18] Train-MSE=0.00368094681762159 +[18] Validation-MSE=0.00342673255363479 +[19] Train-MSE=0.00352097053546459 +[19] Validation-MSE=0.00327468500472605 +[20] Train-MSE=0.0033796411473304 Review comment: done ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected] With regards, Apache Git Services
