How to Implement Multidimensional Input in LSTM?
I am implementing my own LSTM similar to the model in https://github.com/dmlc/mxnet/blob/master/example/rnn/char-rnn.ipynb. The error I am having now somehow indicates that the dimension of input is not appropriate. Any idea how should I debug this? Thanks so much!
Here are what I have when I run model.fit(X=train_iter, eval_data=test_iter, batch_end_callback=mx.callback.Speedometer(batch_size, 8)):
[06:17:01] /mxnet/dmlc-core/include/dmlc/logging.h:235: [06:17:01] src/ndarray/ndarray.cc:227: Check failed: from.shape() == to->shape() operands shape mismatch
Traceback (most recent call last):
File "train_lstm.py", line 68, in
Here is how I define my LSTM: `def lstm_general_unroll(num_lstm_layer, seq_len, num_hidden, num_label, dropout=0.):
cls_weight = mx.sym.Variable("cls_weight")
cls_bias = mx.sym.Variable("cls_bias")
param_cells = []
last_states = []
for i in range(num_lstm_layer):
param_cells.append(LSTMParam(i2h_weight=mx.sym.Variable("l%d_i2h_weight" % i),
i2h_bias=mx.sym.Variable("l%d_i2h_bias" % i),
h2h_weight=mx.sym.Variable("l%d_h2h_weight" % i),
h2h_bias=mx.sym.Variable("l%d_h2h_bias" % i)))
state = LSTMState(c=mx.sym.Variable("l%d_init_c" % i),
h=mx.sym.Variable("l%d_init_h" % i))
last_states.append(state)
assert(len(last_states) == num_lstm_layer)
data = mx.sym.Variable('data')
label = mx.sym.Variable('softmax_label')
# embed_weight = mx.sym.Variable("embed_weight")
# embed = mx.sym.Embedding(data=data, input_dim=5, weight=embed_weight, output_dim=128, name='embed')
# wordvec = mx.sym.SliceChannel(data=embed, num_outputs=seq_len, squeeze_axis=1)
wordvec = mx.sym.SliceChannel(data=data, num_outputs=seq_len, squeeze_axis=1)
hidden_all = []
for seqidx in range(seq_len):
hidden = wordvec[seqidx]
# stack LSTM
for i in range(num_lstm_layer):
if i == 0:
dp_ratio = 0.
else:
dp_ratio = dropout
next_state = lstm(num_hidden, indata=hidden,
prev_state=last_states[i],
param=param_cells[i],
seqidx=seqidx, layeridx=i, dropout=dp_ratio)
hidden = next_state.h
last_states[i] = next_state
# decoder
if dropout > 0.:
hidden = mx.sym.Dropout(data=hidden, p=dropout)
hidden_all.append(hidden)
hidden_concat = mx.sym.Concat(*hidden_all, dim=0)
pred = mx.sym.FullyConnected(data=hidden_concat, num_hidden=num_label,
weight=cls_weight, bias=cls_bias, name='pred')
################################################################################
# Make label the same shape as our produced data path
# I did not observe big speed difference between the following two ways
label = mx.sym.transpose(data=label)
label = mx.sym.Reshape(data=label, target_shape=(0,))
print "label reshape done!"
# label_slice = mx.sym.SliceChannel(data=label, num_outputs=seq_len)
# label = [label_slice[t] for t in range(seq_len)]
# label = mx.sym.Concat(*label, dim=0)
# label = mx.sym.Reshape(data=label, target_shape=(0,))
################################################################################
sm = mx.sym.SoftmaxOutput(data=pred, label=label, name='softmax')
return sm`
My iterator is also similar to BucketSentenceIter in https://github.com/dmlc/mxnet/blob/master/example/rnn/bucket_io.py.
Here are a few critical lines: `
init_state_names = [x[0] for x in self.init_states]
data[:] = self.X_data[idx]
label = self.label_buffer
label[:] = self.y_data[idx]
data_all = [mx.nd.array(data)] + self.init_state_arrays
# data_all = [data] + self.init_state_arrays
# label = np.reshape(label,(self.batch_size*self.seq_len,))
label_all = [mx.nd.array(label)]
data_names = ['data'] + init_state_names
label_names = ['softmax_label']
print "data shape:"
print data_all[0].shape
print "label.shape:"
print label_all[0].shape
data_batch = SimpleBatch(data_names, data_all, label_names, label_all)
print "__iter__ done!"
yield data_batch`
data shape: (32L, 15L, 18L) label.shape: (32L, 15L)
32 is batch size. 15 is sequence length. 18 is the dimension of input feature. In the char-cnn example, the input feature is only one number, and that is why there is one more dimension in data.
Could someone please show me what is wrong here? Many thanks!
Maybe you should feed a initial state to LSTM. If I were you, I will use the infer_shape function
@wushouchuan Thanks! I appreciate your idea. However, I think the LSTM is already initialized.
@pluskid @antinucleon Sorry to bother! I noticed that you guys implemented char-rnn.ipynb in https://github.com/dmlc/mxnet/blob/master/example/rnn/char-rnn.ipynb. Could either of you help me please? I would appreciate any comments.
I have the same question. Do you have a solution? How to train my own data in the lstm?
@KCHWang It's been a while. Did you happen to find the solution? I'm facing same problem.
We have several RNN APIs now so there really is not need to implement your own any more:
Gluon LSTM layer and Gluon LSTM cell, and examples Symbol RNN LSTMCell and examples