【发布时间】:2020-10-11 21:29:00
【问题描述】:
在运行我的模型一个时期后,它崩溃并显示以下错误消息:
InvalidArgumentError:从形状为 [56,9] 的张量中指定了一个形状为 [60,9] 的列表 [[{{node TensorArrayUnstack/TensorListFromTensor}}]] [[sequential_7/lstm_17/PartitionedCall]] [Op:__inference_train_function_29986]
这发生在我将 LSTM 层更改为 stateful=True 并且必须通过之后
batch_input_shape 参数而不是 input_shape
下面是我的代码,我确信它与我的数据的形状有关:
test_split = 0.2
history_points = 60
n = int(histories.shape[0] * test_split)
histories_train = histories[:n]
y_train = next_values_normalized[:n]
histories_test = histories[n:]
y_test = next_values_normalized[n:]
next_values_test = next_values[n:]
print(histories_train.shape)
print(y_train.shape)
-->(1421, 60, 9)
-->(1421, 1)
# model architecture
´´´model = Sequential()
model.add(LSTM(units=128, stateful=True,return_sequences=True, batch_input_shape=(60,history_points, 9)))
model.add(LSTM(units=64,stateful=True,return_sequences=True))
model.add(Dropout(0.2))
model.add(LSTM(units=32))
model.add(Dropout(0.2))
model.add(Dense(20))
ADAM=keras.optimizers.Adam(0.0005, beta_1=0.9, beta_2=0.999, amsgrad=False)
model.compile(loss='mean_squared_error', optimizer=ADAM)
model.fit(x=histories_train, y=y_train, batch_size=batchsize, epochs=50, shuffle=False, validation_split=0.2,verbose=1)
´´´
【问题讨论】:
-
我现在也有同样的问题。原因可能是:您适合 80% 的训练数据(20% 是 Val),因此您的训练数据有 1136 个样本。使用批量大小 60,您有 18 批 60 个样本和一批 56 个样本。似乎它期望训练数据可以整批整除。你的问题已经解决了吗?
标签: python tensorflow keras lstm recurrent-neural-network