LSTM layer value error: dimension must be equal

Viewed 97

I am trying to create a very simple 2 layer LSTM model for sequential prediction. The input data shape is 2D. I want to pad my input but I wasn't sure how to do it, so I manually padded my input data. That is why the pad_inputs is commented out. However, when I run this model I get an error saying:

ValueError: Dimensions must be equal, but are 45 and 400 for '{{node lstm_1/while_11/SelectV2}} = SelectV2[T=DT_FLOAT](lstm_1/while_11/Tile, lstm_1/while_11/lstm_cell_35/mul_2, lstm_1/while_11/Placeholder_2)' with input shapes: [?,45], [?,400], [?,400].
inputs = keras.Input(shape=(timesteps, len(paramlist)), dtype="float32")
# pad_inputs = preprocessing.sequence.pad_sequences(X_train_arr, value=-1, padding='post')(inputs)
mask_inputs = Masking(mask_value=-1.)(inputs)
l1 = LSTM(units=400, activation=activation_method_LSTM, use_bias=False, return_sequences=True, name='lstm_1')(inputs, mask=mask_inputs)
l2 = Dropout(0.3)(l1)
l3 = LSTM(units=400, activation=activation_method_LSTM, use_bias=False, return_sequences=True, name='lstm_2')(l2)
l4 = Dropout(0.3)(l3)
outputs = Dense(n_out)(l4)

model = keras.Model(inputs, outputs)

keras.utils.plot_model(model, 'final_approach_prediction_model.png', show_shapes=True)

model.summary()
1 Answers

Problem with l1 = LSTM(...). LSTM takes specific input dimension, it takes either inputs or mask_inputs. As the error says expected dimension 45 and 400 but given dimension[?,45] and [?, 400]

Find the below working sample code

import tensorflow as tf
n_out = 24
samples, timesteps, features = 32, 10, 8
model = tf.keras.models.Sequential()
inputs = tf.keras.Input(shape=(timesteps, features), dtype="float32")
mask = tf.keras.layers.Masking(mask_value=-1.)(inputs)
l1 = tf.keras.layers.LSTM(units=400, activation='relu', use_bias=False, return_sequences=True, name='lstm_1')(mask)
l2 = tf.keras.layers.Dropout(0.3)(l1)
l3 = tf.keras.layers.LSTM(units=400, activation='relu', use_bias=False, return_sequences=True, name='lstm_2')(l2)
l4 = tf.keras.layers.Dropout(0.3)(l3)
outputs = tf.keras.layers.Dense(n_out)(l4)

model = keras.Model(inputs, outputs)
Related