I have data with a shape of (10000, 20, 15, 4)
where num samples = 10000
, num series in time = 20
, height = 15
, weight = 4
. So I have table 15x4
which is distributed over time. Here is the model I want to train it over this data:
...
model.add((LSTM(nums-1,return_sequences=True,input_shape=(20,15,4), activation='relu')))
model.add((LSTM(nums-1,return_sequences=False,input_shape=(20,15,4), activation='tanh')))
model.add(Dense(15,activation='relu'))
...
However, I get the following error:
ValueError: Input 0 is incompatible with layer lstm_1: expected ndim=3,
found ndim=4
How do I define a LSTM layer with 4D input shape?
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.
Input layer consists of 10 sequential time steps (10 × 30 sec = 5 min), each containing 32 feature-channel combinations (2 features × 16 channels). LSTM layer consists of 10 LSTM cells each having 100 hidden units. FC = fully connected layer taking output matrix (h10) from 10th LSTM cell.
The input of LSTM layer has a shape of (num_timesteps, num_features) , therefore: If each input sample has 69 timesteps, where each timestep consists of 1 feature value, then the input shape would be (69, 1) .
LSTM Default return value: The size of output is 2D array of real numbers. The second dimension is the dimensionality of the output space defined by the units parameter in Keras LSTM implementation.
LSTM layer accepts a 3D array as input which has a shape of (n_sample, n_timesteps, n_features)
. Since the features of each timestep in your data is a (15,4)
array, you need to first flatten them to a feature vector of length 60 and then pass it to your model:
X_train = X_train.reshape(10000, 20, -1)
# ...
model.add(LSTM(...,input_shape=(20,15*4), ...)) # modify input_shape accordingly
Alternatively, you can use a Flatten
layer wrapped in a TimeDistributed
layer as the first layer of your model to flatten each timestep:
model.add(TimeDistributed(Flatten(input_shape=(15,4))))
Further, note that if each timestep (i.e. array (15, 4)
) is a feature map where there is a local spatial relationship between its elements, say like an image patch, you can also use ConvLSTM2D
instead of LSTM
layer. Otherwise, flattening the timesteps and using LSTM would be fine.
As a side note: you only need to specify input_shape
argument on the first layer of the model. Specifying it on other layers would be redundant and will be ignored since their input shape is automatically inferred by Keras.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With