I used MXNet previously to beat keras+tensorflow accuracy in CNN regression models. Now I am trying to implement LSTM, which in keras runs fine:

from keras.layers import LSTM,Flatten,Input
from keras import Model
import numpy as np

def make_keras_stacked_lstm():
    inp = Input(shape=(100,1))
    lstm1 = LSTM(16, return_sequences=True)(inp)
    lstm2 = LSTM(1, return_sequences=True)(lstm1)
    outp = Flatten()(lstm2)
    return Model([inp], outp)

def keras_main():
    ins = np.random.uniform(size=(1000,100,1))
    outs = np.random.uniform(size=(1000,100))
    model = make_keras_stacked_lstm()
    model.compile(optimizer='sgd', loss='mean_squared_error')
    model.fit(ins, outs, epochs=1, validation_split=.1)

if __name__ == '__main__':
    # main()

How can I translate this to MXNet, in either Symbol or gluon dialect? I found no "return_sequences=True" analogue.


1 Answer 1


The best emulation (although I didn't check full equivalency):

import mxnet as mx


stack = mx.gluon.rnn.SequentialRNNCell()
stack.add(mx.gluon.rnn.LSTMCell(16, prefix='first')
stack.add(mx.gluon.rnn.LSTMCell(1, prefix='first')

x, _ = stack.unroll(length=SEQUENCE_LENGTH,

Here layout='NTC' is for the case when input shape is (samples, timesteps, channels).


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.