【问题标题】:InvalidArgumentError: Specified a list with shape [60,9] from a tensor with shape [56,9]InvalidArgumentError:从形状为 [56,9] 的张量中指定形状为 [60,9] 的列表
【发布时间】: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


【解决方案1】:

对于有状态的 LSTM,应以某种方式选择批大小,以便样本数可被批大小整除。另见此处:

Keras: What if the size of data is not divisible by batch_size?

在您的情况下,考虑到您从训练数据中提取 20% 作为验证集,您还剩下 1136 个样本。所以你应该选择一个可以整除 1136 的批大小。

此外,您可以移除一些样本或重复使用样本,以便能够选择不同的批次大小。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-08-01
    • 1970-01-01
    • 2017-08-17
    • 1970-01-01
    • 2019-09-01
    • 2018-10-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多