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

发布于 2025-01-22 08:41:26 字数 3066 浏览 2 评论 0原文

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解释器上遇到了这个错误。 让我知道正确的兼容性!

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]

On running this code ; the value error raised while I was preparing my model for prediction . Help me to rectify it

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)

On running the code ; I came across this error on my python interpretor.
Let me know the correct compatibility!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

子栖 2025-01-29 08:41:26

错误几乎说明了一切。模型中的第一个LSTM层期望有时间序列,每个时间序列都有60个时间段和 5 每个时间步中的功能,但是您以某种方式将其喂给了一个时间序列,每个时间序列都有60个步骤, 7 功能。您可以检查x_train.shape [2]以查看是否是7。

此外,您使用LSTM层的输出的方式不正确。您可能想通过此答案官方Tensorflow文档查看LSTM层的输出是什么,该输出是什么,return> return> return> >设置为true

The error pretty much says it all. The first LSTM layer in your model expects a batch of time series, each having 60 timesteps and 5 features per timestep, but somehow you fed it a batch of time series each having 60 steps and 7 features. You might check your X_train.shape[2] to see if it is 7.

Also, the way you're using the output of LSTM layers is incorrect. You might want to go through this answer and official tensorflow documentation to see what are the outputs of a LSTM layer with return_sequences set to True.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文