Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to feed into LSTM with 4 dimensional input?

I have a sequence input in this shape: (6000, 64, 100, 50)

The 6000 is just the number of sample sequences. Each sequences is 64 in length.

I plan to fit this input into an LSTM using Keras.

I setup my input this way:

input = Input(shape=(64, 100, 50))

This gives me an input shape of (?, 64, 100, 50)

However, when I put input into my LSTM like so:

x = LSTM(256, return_sequences=True)(input)

I get this error:

Input 0 is incompatible with layer lstm_37: expected ndim=3, found ndim=4

This would have worked if my input shape was something like (?, 64, 100), but not when I've a 4th dimension.

Does this mean that LSTM can only take an input of 3 dimensional? How can I feed a 4 or even higher dimension input into LSTM using Keras?

like image 512
Carven Avatar asked Nov 21 '17 10:11

Carven


People also ask

What should be the dimension of the input to a LSTM layer?

The input of the LSTM is always is a 3D array. (batch_size, time_steps, seq_len) . The output of the LSTM could be a 2D array or 3D array depending upon the return_sequences argument.

How many inputs does LSTM have?

There are 3 inputs to the LSTM cell: ht−1 previous timestep (t-1) Hidden State value. ct−1 previous timestep (t-1) Cell State value. xt current timestep (t) Input value.

How the inputs are selected for LSTM RNN models?

The input data for the LSTM has to be 3D. The input data has to be reshaped into (samples, time steps, features) . This means that targets and features must have the same shape. You need to set a number of time steps for your problem, in other words, how many samples will be used to make a prediction.


2 Answers

The answer is you can't.

The Keras Documentation provides the following information for Recurrent Layer:

Input shape

3D tensor with shape (batch_size, timesteps, input_dim).

In your case you have 64 timesteps where each step is of shape (100, 50). The easiest way to get the model working is to reshape your data to (100*50).

Numpy provides an easy function to do so:

X = numpy.zeros((6000, 64, 100, 50), dtype=numpy.uint8)
X = numpy.reshape(X, (6000, 64, 100*50))

Wheter this is reasonable or not highly depends on your data.

like image 84
mapeza Avatar answered Oct 21 '22 04:10

mapeza


you can also consider TimeDistributed(LSTM(...))

inp = Input(shape=(64, 100, 50))
x = TimeDistributed(LSTM(256, return_sequences=True))(inp)

model = Model(inp, x)
model.compile('adam', 'mse')
model.summary()
like image 35
Marco Cerliani Avatar answered Oct 21 '22 05:10

Marco Cerliani