With this code I get an error while I try to run the prediction with X_test. The error occurs after the fitting, while y_pred = model.predict(X_test)
is executed.
X_train.input_shape()
-> (784, 300, 7)
y_train.input_shape()
-> (784, 300, 1)
X_test.input_shape()
-> (124,300,7)
y_test.input_shape()
-> (124,300,1)
batchsize = 4
model = Sequential()
model.add(Masking(mask_value=0, batch_input_shape=(batchsize, len(X_train[0]),len(X_train[0][0]))))
model.add(Bidirectional(LSTM(200, return_sequences=True, stateful=True)))
model.add(TimeDistributed(Dense(len(classdict))))
model.compile(loss="sparse_categorical_crossentropy",
optimizer=Adam(0.001),
metrics=['accuracy'])
model.summary()
model.fit(X_train, y_train, epochs=1, batch_size=batchsize)
y_pred = model.predict(X_test)
Error:
Traceback (most recent call last):
File "lib/python3.7/site-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "lib/python3.7/site-packages/tensorflow/python/eager/execute.py", line 55, in quick_execute
inputs, attrs, num_outputs)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:
Specified a list with shape [4,1] from a tensor with shape [32,1]
[[{{node TensorArrayUnstack_1/TensorListFromTensor}}]]
[[sequential/bidirectional/backward_lstm/PartitionedCall]] [Op:__inference_predict_function_13360]
In my opinion, the error could be related to the selected batchsize. In my research I read that the number of samples has to be divisible by the batchsize for stateful LSTMs. So I searched for the greatest common divisor of the number of samples of X_train and X_test, so GCD(124,784) = 4 = batchsize. However, now this error occurs, I have already tried different batchsizes, but then other errors occur. Has someone a idea/fix for this?