我正在尝试构建一个模型,基于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)
。或者如果你想保持序列,使用TimeDistributed
和GlobalAveragePooling1D
层,并对输出做同样的事情。看起来会像这样:
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)