'Keras hyperparameter tuning error: ValueError: Dimensions must be equal, but are 10 and 20 for '{{node mean_absolute_error/sub}} = Sub[T=DT_FL

I am trying to do hyperparameter tuning for my forecasting model. I am trying to forecast the next 20 values by looking previous 10 values. So x_train's shape is (571, 10, 1), Y_Train's shape is (571, 20, 1). When I do model.fit with those values it's accepted. But when i try to do :

tuner.search(train_data_multi,epochs=20,batch_size=64)

It's throwing me this error:

ValueError: Dimensions must be equal, but are 10 and 20 for '{{node mean_absolute_error/sub}} = Sub[T=DT_FLOAT](sequential/dense/BiasAdd, Cast)' with input shapes: [?,10,35], [?,20,1].

And this is how am i preparing my data:

batch_size = 64
buffer_size = 1000

train_data_multi = tf.data.Dataset.from_tensor_slices((x_train_multi, y_train_multi))
train_data_multi = train_data_multi.cache().shuffle(buffer_size).batch(batch_size)

val_data_multi = tf.data.Dataset.from_tensor_slices((x_val_multi, y_val_multi))
val_data_multi = val_data_multi.batch(batch_size)

test_data_multi = tf.data.Dataset.from_tensor_slices((x_test_multi, y_test_multi))
test_data_multi = test_data_multi.batch(batch_size)

print(train_data_multi)
print(x_train_multi.shape)

My model:

EPOCHS = 500
steps = int( np.ceil(x_train_multi.shape[0] / batch_size) )
val_steps=int( np.ceil(x_val_multi.shape[0] / batch_size) )

from keras.layers import Dropout

multi_step_model = tf.keras.models.Sequential()
multi_step_model.add(tf.keras.layers.LSTM(64,input_shape=x_train_multi.shape[-2:],activation=None,return_sequences=True))
multi_step_model.add(tf.keras.layers.LSTM(128,input_shape=x_train_multi.shape[-2:],activation=None,return_sequences=True))
multi_step_model.add(tf.keras.layers.LSTM(128,input_shape=x_train_multi.shape[-2:],activation=None,return_sequences=True))
multi_step_model.add(tf.keras.layers.LSTM(64,input_shape=x_train_multi.shape[-2:],activation=None,return_sequences=False))
multi_step_model.add(Dropout(0.2))
multi_step_model.add(tf.keras.layers.Dense(20)) # for 72 outputs

multi_step_model.compile(optimizer=tf.keras.optimizers.RMSprop(clipvalue=1.0), loss='mae')

multi_step_model.summary()
multi_step_history = multi_step_model.fit(train_data_multi, epochs=EPOCHS,
                                          steps_per_epoch=steps,
                                          validation_data=val_data_multi,
                                          validation_steps=val_steps)

How can I fix this issue?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source