Home > Back-end >  How to reshape multivariate time series data for ConvLSTM2D model
How to reshape multivariate time series data for ConvLSTM2D model

Time:07-02

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()

CodePudding user response:

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
  • Related