Keras stateful LSTM error: Specified a list with shape [4,1] from a tensor with shape [32,1]
Keras stateful LSTM error: Specified a list with shape [4,1] from a tensor with shape [32,1]
使用此代码,我在尝试 运行 使用 X_test 进行预测时遇到错误。拟合后出现错误,执行y_pred = model.predict(X_test)
时
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)
错误:
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]
在我看来,错误可能与所选的批量大小有关。在我的研究中,我读到样本数量必须被有状态 LSTM 的批量大小整除。于是我求了X_train和X_test的样本数的最大公约数,所以GCD(124,784) = 4 = batchsize。但是,现在出现此错误,我已经尝试了不同的 batchsizes,但随后出现了其他错误。有人 idea/fix 吗?
你只需要确保训练样本的数量能被batch size平分即可。这是一个工作示例:
import tensorflow as tf
X_train = tf.random.normal((784, 300, 7))
y_train = tf.random.normal((784, 300, 1))
X_test = tf.random.normal((124,300,7))
batchsize = 4
model = tf.keras.Sequential()
model.add(tf.keras.layers.Masking(mask_value=0, batch_input_shape=(batchsize, len(X_train[0]),len(X_train[0][0]))))
model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(200, return_sequences=True, stateful=True)))
model.add(tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(1)))
model.compile(loss="mse",
optimizer=tf.keras.optimizers.Adam(0.001))
model.summary()
model.fit(X_train, y_train, batch_size=batchsize, epochs=1)
Model: "sequential_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
masking_5 (Masking) (4, 300, 7) 0
bidirectional_5 (Bidirectio (4, 300, 400) 332800
nal)
time_distributed_5 (TimeDis (4, 300, 1) 401
tributed)
=================================================================
Total params: 333,201
Trainable params: 333,201
Non-trainable params: 0
_________________________________________________________________
196/196 [==============================] - 47s 131ms/step - loss: 1.0052
<keras.callbacks.History at 0x7fee901f8950>
并在预测时显式设置批量大小,因为根据 docs 如果未指定,batch_size 将默认为 32:
X_test = tf.random.normal((124,300,7))
y_pred = model.predict(X_test, batch_size=batchsize)
print(y_pred.shape)
(124, 300, 1)
还要确保你的批次在任何地方都是一样的。
使用此代码,我在尝试 运行 使用 X_test 进行预测时遇到错误。拟合后出现错误,执行y_pred = model.predict(X_test)
时
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)
错误:
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]
在我看来,错误可能与所选的批量大小有关。在我的研究中,我读到样本数量必须被有状态 LSTM 的批量大小整除。于是我求了X_train和X_test的样本数的最大公约数,所以GCD(124,784) = 4 = batchsize。但是,现在出现此错误,我已经尝试了不同的 batchsizes,但随后出现了其他错误。有人 idea/fix 吗?
你只需要确保训练样本的数量能被batch size平分即可。这是一个工作示例:
import tensorflow as tf
X_train = tf.random.normal((784, 300, 7))
y_train = tf.random.normal((784, 300, 1))
X_test = tf.random.normal((124,300,7))
batchsize = 4
model = tf.keras.Sequential()
model.add(tf.keras.layers.Masking(mask_value=0, batch_input_shape=(batchsize, len(X_train[0]),len(X_train[0][0]))))
model.add(tf.keras.layers.Bidirectional(tf.keras.layers.LSTM(200, return_sequences=True, stateful=True)))
model.add(tf.keras.layers.TimeDistributed(tf.keras.layers.Dense(1)))
model.compile(loss="mse",
optimizer=tf.keras.optimizers.Adam(0.001))
model.summary()
model.fit(X_train, y_train, batch_size=batchsize, epochs=1)
Model: "sequential_5"
_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
masking_5 (Masking) (4, 300, 7) 0
bidirectional_5 (Bidirectio (4, 300, 400) 332800
nal)
time_distributed_5 (TimeDis (4, 300, 1) 401
tributed)
=================================================================
Total params: 333,201
Trainable params: 333,201
Non-trainable params: 0
_________________________________________________________________
196/196 [==============================] - 47s 131ms/step - loss: 1.0052
<keras.callbacks.History at 0x7fee901f8950>
并在预测时显式设置批量大小,因为根据 docs 如果未指定,batch_size 将默认为 32:
X_test = tf.random.normal((124,300,7))
y_pred = model.predict(X_test, batch_size=batchsize)
print(y_pred.shape)
(124, 300, 1)
还要确保你的批次在任何地方都是一样的。