ValueError:层“顺序”的输入0与层不兼容:预期形状=(无,60,5),发现形状=(无,60,7)

原文标题ValueError: Input 0 of layer “sequential” is incompatible with the layer: expected shape=(None, 60, 5), found shape=(None, 60, 7)

regressor.add(LSTM(units = 60, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 70, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))
regressor.add(LSTM(units = 90, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))

regressor.add(LSTM(units = 140, activation = 'relu', return_sequences = True, input_shape = (X_train.shape[1], 5)))
regressor.add(Dropout(0.2))

regressor.add(Dense(units =1))
regressor.summary()


regressor.compile(optimizer = 'adam', loss='mean_absolute_error')
regressor.fit(X_train, Y_train, epochs = 20, batch_size =50)[enter image description here][1]

在运行此代码时;在我准备预测模型时出现的值错误。帮我改正

Epoch 1/20
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-535f5f5c29a7> in <module>()
----> 1 regressor.fit(X_train, Y_train, epochs = 20, batch_size =50)

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py in error_handler(*args, **kwargs)
    65     except Exception as e:  # pylint: disable=broad-except
    66       filtered_tb = _process_traceback_frames(e.__traceback__)
---> 67       raise e.with_traceback(filtered_tb) from None
    68     finally:
    69       del filtered_tb

/usr/local/lib/python3.7/dist-packages/tensorflow/python/framework/func_graph.py in autograph_handler(*args, **kwargs)
  1145           except Exception as e:  # pylint:disable=broad-except
  1146             if hasattr(e, "ag_error_metadata"):
-> 1147               raise e.ag_error_metadata.to_exception(e)
  1148             else:
  1149               raise

ValueError: in user code:

   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function  *
       return step_function(self, iterator)
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function  **
       outputs = model.distribute_strategy.run(run_step, args=(data,))
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step  **
       outputs = model.train_step(data)
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 859, in train_step
       y_pred = self(x, training=True)
   File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
       raise e.with_traceback(filtered_tb) from None
   File "/usr/local/lib/python3.7/dist-packages/keras/engine/input_spec.py", line 264, in assert_input_compatibility
       raise ValueError(f'Input {input_index} of layer "{layer_name}" is '

   ValueError: Input 0 of layer "sequential" is incompatible with the layer: expected shape=(None, 60, 5), found shape=(None, 60, 7)

运行代码;我在我的 python 解释器上遇到了这个错误。让我知道正确的兼容性!

原文链接:https://stackoverflow.com//questions/71900765/valueerror-input-0-of-layer-sequential-is-incompatible-with-the-layer-expect

回复

我来回复
  • bui的头像
    bui 评论

    该错误几乎说明了一切。模型中的第一个 LSTM 层需要一批时间序列,每个时间序列有 60 个时间步长和每个时间步长 5 个特征,但不知何故,你给它提供了一批时间序列,每个时间序列有 60 个步长和 7 个特征。你可以检查你的X_train.shape[2]看它是否是 7。

    此外,您使用 LSTM 层输出的方式不正确。您可能想通过这个答案和官方 tensorflow 文档来查看将return_sequences设置为True的 LSTM 层的输出是什么。

    2年前 0条评论