I would like to convert this Lasagne code:

et = {}
net['input'] = lasagne.layers.InputLayer((100, 1, 24, 113))
net['conv1/5x1'] = lasagne.layers.Conv2DLayer(net['input'], 64, (5, 1))
net['shuff'] = lasagne.layers.DimshuffleLayer(net['conv1/5x1'], (0, 2, 1, 3))
net['lstm1'] = lasagne.layers.LSTMLayer(net['shuff'], 128)

in Keras code. Currently I came up with this:

multi_input = Input(shape=(1, 24, 113), name='multi_input')
y = Conv2D(64, (5, 1), activation='relu', data_format='channels_first')(multi_input)
y = LSTM(128)(y)

But I get the error: Input 0 is incompatible with layer lstm_1: expected ndim=3, found ndim=4

I know the difficult part is to connect an LSTM to the output of Conv2D. Maybe using TimeDistributed?


You need to reshape the output of the Conv2d before you feed into the lstm. The output of Conv2d is 4d whereas the input for LSTM required is 3d.

Just print the output of Conv2d first, that will give you an idea on how to reshape the tensor as input to LSTM.

  • $\begingroup$ The output of my conv2D is (filters, timestamps, features) so I did y = Reshape((filters*timestamps,features))(conv_outpu). Do you think is right? And why Lasagne is different and doesn't need a reshape? $\endgroup$ – Francesco Pegoraro Nov 5 '18 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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