Keras – 当returnSequence为True时维度必须相等

我正在尝试构建一个模型,基于3个观测值的序列预测4个值,例如:

如果以下是数据

+--------------------------------+|feature |feature |feature |Value|+--------------------------------+|0.1     |0.1     |0.1     |1    |+--------------------------------+|0.2     |0.2     |0.2     |2    |+--------------------------------+|0.3     |0.3     |0.3     |3    |+--------------------------------+|0.4     |0.4     |0.4     |4    |+--------+--------+--------+-----+

我想基于以下数据预测[1,2,3,4]

+--------------------------+|feature |feature |feature |+--------------------------+|0.1     |0.1     |0.1     |+--------------------------+|0.2     |0.2     |0.2     |+--------------------------+|0.3     |0.3     |0.3     |+--------+--------+--------+

我的X,y形状如下(1228, 3, 19) (1228, 4, 1)


def get_model():  model = Sequential()  model.add(LSTM(32, activation='tanh', return_sequences=True, input_shape=(X.shape[1], X.shape[2]))),  model.add(Dense(32, activation='relu')),  model.add(Dense(4, activation='sigmoid'))  model.compile(loss='mse', optimizer="adam", metrics=['mae', 'mse'])  return model

我的模型代码:Model: "sequential_17"_________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================lstm_17 (LSTM)               (None, 3, 32)             6656      _________________________________________________________________dense_34 (Dense)             (None, 3, 32)             1056      _________________________________________________________________dense_35 (Dense)             (None, 3, 4)              132       =================================================================Total params: 7,844Trainable params: 7,844Non-trainable params: 0_________________________________________________________________

当我尝试拟合数据时:

history = model.fit(X_train, y_train, epochs=200, batch_size=64, validation_split=0.2, verbose=0, callbacks=[tensorboard_callback])

我得到了以下错误:

ValueError: Dimensions must be equal, but are 3 and 4 for

我应该如何重塑我的数据以使其工作,是否需要填充缺失的序列?


回答:

如果我理解正确的话,每个例子都有以下内容:

输入 -> (3,19)输出 -> (4, 1)

你试图基于19个值的3个序列回归4个值。如果这是正确的,那么你可以使用return_sequences=False在你的模型中,并将你的输出(y)重塑为形状(4,)而不是(4,1),例如y=np.squeeze(y, -1)。或者如果你想保持序列,使用TimeDistributedGlobalAveragePooling1D层,并对输出做同样的事情。看起来会像这样:

model = Sequential()model.add(LSTM(32, activation='tanh', return_sequences=True, input_shape=(3, 19))),model.add(TimeDistributed(Dense(32, activation='relu')))model.add(GlobalAveragePooling1D())model.add(Dense(4, activation='sigmoid'))model.summary()Model: "sequential_9"_________________________________________________________________Layer (type)                 Output Shape              Param #   =================================================================lstm_9 (LSTM)                (None, 3, 32)             6656      _________________________________________________________________time_distributed_4 (TimeDist (None, 3, 32)             1056      _________________________________________________________________global_average_pooling1d_2 ( (None, 32)                0         _________________________________________________________________dense_16 (Dense)             (None, 4)                 132       =================================================================Total params: 7,844Trainable params: 7,844Non-trainable params: 0

编辑

你当前模型的问题在于它期望你的目标/输出具有形状(3,4),而你的实际输出具有形状(4,1)

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注