Correctly formatting data for lstm recurrent neural network in R / mxnet

2.7k Views Asked by At

I want to train an lstm neural net using the mx.lstm function in the R package mxnet. My data comprises n feature vectors, a vector of labelled classes and a time vector, much like this dummy example where X1, X2, X3 are the features:

dat <- data.frame(
  X1 = rnorm(100, 1, sd = 1),
  X2 = rnorm(100, 2, sd = 1),
  X3 = rnorm(100, 3, sd = 1),
  class = sample(c(1,0), replace = T, 100),
  time =  seq(0.01,1,0.01))

Help for mx.lstm states that the train.data argument requires "mx.io.DataIter or list(data=R.array, label=R.array) The Training set".

I have tried this:

library(mxnet)

# Convert dummy data into suitable format
trainDat <- list(data = array(c(dat$X1, dat$X2, dat$X3), dim = c(100,3)), 
label = array(dat[,4], dim = c(100,1)))

# Set the basic network parameters for the lstm (arbitrary for this example)
batch.size = 32
seq.len = 32
num.hidden = 16
num.embed = 16
num.lstm.layer = 1
num.round = 1
learning.rate = 0.1
wd = 0.00001
clip_gradient = 1
update.period = 1

# Run the model
model <- mx.lstm(train.data = trainDat,
             ctx=mx.cpu(),
             num.round=num.round, 
             update.period=update.period,
             num.lstm.layer=num.lstm.layer, 
             seq.len=seq.len,
             num.hidden=num.hidden, 
             num.embed=num.embed, 
             num.label=vocab,
             batch.size=batch.size, 
             input.size=vocab,
             initializer=mx.init.uniform(0.1), 
             learning.rate=learning.rate,
             wd=wd,
             clip_gradient=clip_gradient)

Which returns "Error in mx.io.internal.arrayiter(as.array(data), as.array(label), unif.rnds, : basic_string::_M_replace_aux"

There is an example lstm on the mxnet website, but the data used are quite different to mine and I can't make sense of it.

http://mxnet.io/tutorials/r/charRnnModel.html

So, my question is how do I transform my data into a suitable format for mx.lstm?

1

There are 1 best solutions below

0
On BEST ANSWER

I tried to reproduce your error and got a more detailed message:

Error in mx.io.internal.arrayiter(as.array(data), as.array(label), unif.rnds, : io.cc:50: Seems X, y was passed in a Row major way, MXNetR adopts a column major convention. Please pass in transpose of X instead

I fixed the error by passing data and label arrays to aperm().

trainDat <- list(data = aperm(array(c(dat$X1, dat$X2, dat$X3), dim = c(100,3))), label = aperm(array(dat[,4], dim = c(100,1))))