How to reshape multivariate time series data for ConvLSTM2D model

Issue

I am using data having the shape (1000, 5, 7). I have reshaped this to (1000, 5, 7, 1) to meet the need of ConvLSTM2D.
While training the model with this, I get the error:

ValueError: Input 0 of layer "sequential_90" is incompatible with the layer: expected shape=(None, None, 5, 7, 1), found shape=(None, 5, 7, 1)

The error message is clear. However, I don’t know how to reshape my data.

Here is the model I am using

model = Sequential()

model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), input_shape=(None, 5, 7, 1), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same', return_sequences=True))
model.add(BatchNormalization())
model.add(Conv3D(filters=1, kernel_size=(3, 3, 3), activation='softmax', padding='same', data_format='channels_last'))

model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])

#model.summary()

Solution

As the docs state, you need a 5D tensor (samples, time, rows, cols, channels). Here is an example of the data shape you need:

import tensorflow as tf

model = tf.keras.Sequential()

model.add(tf.keras.layers.ConvLSTM2D(filters=40, kernel_size=(3, 3), input_shape=(None, 5, 7, 1), padding='same', return_sequences=True))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same', return_sequences=True))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.ConvLSTM2D(filters=40, kernel_size=(3, 3), padding='same', return_sequences=True))
model.add(tf.keras.layers.BatchNormalization())
model.add(tf.keras.layers.Conv3D(filters=1, kernel_size=(3, 3, 3), padding='same', data_format='channels_last'))
model.add(tf.keras.layers.GlobalAveragePooling3D())
model.add(tf.keras.layers.Dense(7, activation='softmax'))


model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
model.summary()
samples = 1
timesteps = 1
rows = 5
cols = 7 
channels = 1
model(tf.random.normal((samples, timesteps, rows, cols, channels))).shape

Answered By – AloneTogether

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published