当返回顺序为真时,Keras尺寸必须相等

2024-05-08 16:56:52 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图建立一个模型,根据一系列的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    |
+--------+--------+--------+-----+

我想根据

+--------------------------+
|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

My Model code:
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,844
Trainable params: 7,844
Non-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

我应该如何重塑数据以使其正常工作,我应该填充缺失的序列吗


Tags: 数据noneaddmodelreturntrainparamsactivation
1条回答
网友
1楼 · 发布于 2024-05-08 16:56:52

如果理解正确,每个示例都有以下内容:

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

其中,您尝试基于3个19个值的序列回归4个值。如果这是正确的,那么您可以在模型中使用return_sequences=False,并将输出(y)重塑为(4,)的形状,而不是像y=np.squeeze(y, -1)那样的(4,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,844
Trainable params: 7,844
Non-trainable params: 0

编辑

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

相关问题 更多 >