【问题标题】:Tensorflow: Incompatible shapes: [32,12] vs. [32,4]Tensorflow:不兼容的形状:[32,12] vs. [32,4]
【发布时间】:2021-06-23 14:25:06
【问题描述】:

我给自己惹了点麻烦。我有 4 个特征,我想同时预测它们中的每一个。我的回顾是 12,我想预测 12 个时间步长。是否可以并行预测所有 4 个目标?

我必须遵循一段代码。 train_df 上的 shape 为 (40000, 4),val_df 为 (8000, 4)。

win_length=12
batch=32
n_features=4

train_generator = TimeseriesGenerator(train_df, train_df, length=win_length, sampling_rate=1, batch_size=batch)
val_generator = TimeseriesGenerator(val_df, val_df, length=win_length, sampling_rate=1, batch_size=batch)

model = Sequential()
model.add(LSTM(128, activation='tanh', input_shape=(win_length, n_features), return_sequences=True))
model.add(LSTM(128, activation='tanh', return_sequences=True))
model.add(LSTM(64, activation='tanh', return_sequences=True))
model.add(TimeDistributed(Dense(1)))
model.compile(loss='mse', optimizer='adam')
model.summary()

model.fit_generator(train_generator, validation_data=val_generator)

我从 fit_generator-function 中得到以下错误,我似乎不知道是怎么回事。有什么想法吗?

tensorflow.python.framework.errors_impl.InvalidArgumentError:  Incompatible shapes: [32,12] vs. [32,4]

【问题讨论】:

    标签: python-3.x tensorflow machine-learning keras


    【解决方案1】:

    我不太明白TimeseriesGenerator 是如何工作的,但您似乎希望有两个相同的数据和目标序列,因此mse。我检查了自己,TimeseriesGenerator 没有产生两个相同的序列(形状为 (32, 12, 4) 的数据和形状为 (32, 4) 的目标)。我现在最好的选择是手动实现生成器。另外我觉得model.add(TimeDistributed(Dense(1)))应该改成model.add(TimeDistributed(Dense(4)))

    【讨论】:

    • 是的,我想对数据和目标使用相同的序列的原因是我想预测数据集中的所有特征,所以特征本质上也成为我的目标。 Dense(1) 应该是 Dense(4) 的原因是我要预测 4 个目标,对吧?
    • 是的,因为输出形状将是 [batch_size, 12, 4],我猜这与您的理想输出相似。
    • 没错,因为我想提前 12 个时间步预测每个特征。 [32, 12, 4] 确实意味着 32 个批次和 12 个时间步长(行)用于所有 4 个特征,对吧?
    • 是的,就是这样。
    猜你喜欢
    • 1970-01-01
    • 2018-01-10
    • 2021-05-12
    • 2021-05-29
    • 1970-01-01
    • 2019-10-20
    • 1970-01-01
    • 2020-07-16
    • 1970-01-01
    相关资源
    最近更新 更多