我试图适合LSTM型号,但我会得到均值的错误

发布于 2025-01-26 04:07:55 字数 8765 浏览 1 评论 0原文

有人帮我。我是 rnn 的新手,特别是 lstm 模型。我正在尝试使用多变量时间序列数据异常检测来训练 lstm 模型。我遇到了这个错误。

节点:'Mean_squared_error/squaredDifference' 需要广播形状 [[{{node mean_squared_error/squaredDifference}}]] [op:__ inperion_train_function_5545]

[[{{node mean_squared_error /

model = Sequential()
model.add(LSTM(128, input_shape=(x_train.shape[1], x_train.shape[2])))
model.add(Dropout(rate=0.2))
model.add(RepeatVector(x_train.shape[1]))
model.add(LSTM(128, return_sequences=True))
model.add(Dropout(rate=0.2))
model.add(TimeDistributed(Dense(x_train.shape[2])))
model.compile(optimizer='adam', loss='mae')
model.summary()
#x_train shape: (249989, 10, 8)

Model summary:
Model: "sequential_5"
____________________________________________________________________
 Layer (type)                    Output Shape              Param #   
====================================================================
 lstm_22 (LSTM)                  (None, 10, 50)            11800     
                                                                 
 dropout_15 (Dropout)            (None, 10, 50)            0         
                                                                 
 lstm_23 (LSTM)                  (None, 50)                20200     
                                                                 
 dropout_16 (Dropout)            (None, 50)                0         
                                                               
 repeat_vector_7 (RepeatVector)  (None, 10, 50)            0         
                                                                       
 lstm_24 (LSTM)                  (None, 10, 50)            20200     
                                                                 
 dropout_17 (Dropout)            (None, 10, 50)            0         
                                                                 
 dense_9 (Dense)                 (None, 10, 32)            1632      
                                                                 
 dropout_18 (Dropout)            (None, 10, 32)            0         
                                                                 
 dense_10 (Dense)                (None, 10, 1)             33        
                                                                 
====================================================================
Total params: 53,865
Trainable params: 53,865
Non-trainable params: 0

squaredDifference}} ]

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-64-696fd2112f18> in <module>()
      1 #Fitting the RNN to the training set
----> 2 model.fit(x_train, y_train, validation_split=0.2, callbacks=callbacks, epochs=25, batch_size=72, shuffle=False)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
      53     An exception on error.
      54   """
---> 55   device_name = ctx.device_name
      56   # pylint: disable=protected-access
      57   try:

InvalidArgumentError: Graph execution error:

Detected at node 'mean_squared_error/SquaredDifference' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)
    File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
      app.start()
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
      self._run_once()
    File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
      handle._run()
    File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
      handler_func(fileobj, events)
    File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 452, in _handle_events
      self._handle_recv()
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 481, in _handle_recv
      self._run_callback(callback, msg)
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback
      callback(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
      return self.dispatch_shell(stream, msg)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
      handler(stream, idents, msg)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
      user_expressions, allow_stdin)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
      interactivity=interactivity, compiler=compiler, result=result)
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2828, in run_ast_nodes
      if self.run_code(code, result):
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-39-696fd2112f18>", line 2, in <module>
      model.fit(x_train, y_train, validation_split=0.2, callbacks=callbacks, epochs=25, batch_size=72, shuffle=False)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1384, in fit
      threading. If unspecified, `use_multiprocessing` will default to
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function
      If an integer, specifies how many training epochs to run before a
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function
      the dataset will be consumed, the evaluation will start from the
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step
      `steps_per_epoch` argument. This argument is not supported with
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
      validation_data=None,
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
      0 = silent, 1 = progress bar, 2 = one line per epoch.
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
      loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
      call_fn = tf.__internal__.autograph.tf_convert(self.call, tf.__internal__.autograph.control_status_ctx())
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call
      ag_fn = tf.__internal__.autograph.tf_convert(self.fn, tf.__internal__.autograph.control_status_ctx())
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1329, in mean_squared_error
      ...     100. * np.mean(np.abs((y_true - y_pred) / y_true), axis=-1))
Node: 'mean_squared_error/SquaredDifference'
required broadcastable shapes
    [[{{node mean_squared_error/SquaredDifference}}]] [Op:__inference_train_function_5545]

Someone help me out. I am new to RNN and, specifically, LSTM models. I am trying to train an LSTM model for anomaly detection using multivariate time series data. I am getting this error.

Node: 'mean_squared_error/SquaredDifference'
required broadcastable shapes
[[{{node mean_squared_error/SquaredDifference}}]] [Op:__inference_train_function_5545]

Below is my model:

model = Sequential()
model.add(LSTM(128, input_shape=(x_train.shape[1], x_train.shape[2])))
model.add(Dropout(rate=0.2))
model.add(RepeatVector(x_train.shape[1]))
model.add(LSTM(128, return_sequences=True))
model.add(Dropout(rate=0.2))
model.add(TimeDistributed(Dense(x_train.shape[2])))
model.compile(optimizer='adam', loss='mae')
model.summary()
#x_train shape: (249989, 10, 8)

Model summary:
Model: "sequential_5"
____________________________________________________________________
 Layer (type)                    Output Shape              Param #   
====================================================================
 lstm_22 (LSTM)                  (None, 10, 50)            11800     
                                                                 
 dropout_15 (Dropout)            (None, 10, 50)            0         
                                                                 
 lstm_23 (LSTM)                  (None, 50)                20200     
                                                                 
 dropout_16 (Dropout)            (None, 50)                0         
                                                               
 repeat_vector_7 (RepeatVector)  (None, 10, 50)            0         
                                                                       
 lstm_24 (LSTM)                  (None, 10, 50)            20200     
                                                                 
 dropout_17 (Dropout)            (None, 10, 50)            0         
                                                                 
 dense_9 (Dense)                 (None, 10, 32)            1632      
                                                                 
 dropout_18 (Dropout)            (None, 10, 32)            0         
                                                                 
 dense_10 (Dense)                (None, 10, 1)             33        
                                                                 
====================================================================
Total params: 53,865
Trainable params: 53,865
Non-trainable params: 0

Complete error message:

---------------------------------------------------------------------------
InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-64-696fd2112f18> in <module>()
      1 #Fitting the RNN to the training set
----> 2 model.fit(x_train, y_train, validation_split=0.2, callbacks=callbacks, epochs=25, batch_size=72, shuffle=False)

1 frames
/usr/local/lib/python3.7/dist-packages/tensorflow/python/eager/execute.py in quick_execute(op_name, num_outputs, inputs, attrs, ctx, name)
      53     An exception on error.
      54   """
---> 55   device_name = ctx.device_name
      56   # pylint: disable=protected-access
      57   try:

InvalidArgumentError: Graph execution error:

Detected at node 'mean_squared_error/SquaredDifference' defined at (most recent call last):
    File "/usr/lib/python3.7/runpy.py", line 193, in _run_module_as_main
      "__main__", mod_spec)
    File "/usr/lib/python3.7/runpy.py", line 85, in _run_code
      exec(code, run_globals)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel_launcher.py", line 16, in <module>
      app.launch_new_instance()
    File "/usr/local/lib/python3.7/dist-packages/traitlets/config/application.py", line 846, in launch_instance
      app.start()
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelapp.py", line 499, in start
      self.io_loop.start()
    File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 132, in start
      self.asyncio_loop.run_forever()
    File "/usr/lib/python3.7/asyncio/base_events.py", line 541, in run_forever
      self._run_once()
    File "/usr/lib/python3.7/asyncio/base_events.py", line 1786, in _run_once
      handle._run()
    File "/usr/lib/python3.7/asyncio/events.py", line 88, in _run
      self._context.run(self._callback, *self._args)
    File "/usr/local/lib/python3.7/dist-packages/tornado/platform/asyncio.py", line 122, in _handle_events
      handler_func(fileobj, events)
    File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 452, in _handle_events
      self._handle_recv()
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 481, in _handle_recv
      self._run_callback(callback, msg)
    File "/usr/local/lib/python3.7/dist-packages/zmq/eventloop/zmqstream.py", line 431, in _run_callback
      callback(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 283, in dispatcher
      return self.dispatch_shell(stream, msg)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 233, in dispatch_shell
      handler(stream, idents, msg)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/kernelbase.py", line 399, in execute_request
      user_expressions, allow_stdin)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/ipkernel.py", line 208, in do_execute
      res = shell.run_cell(code, store_history=store_history, silent=silent)
    File "/usr/local/lib/python3.7/dist-packages/ipykernel/zmqshell.py", line 537, in run_cell
      return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2718, in run_cell
      interactivity=interactivity, compiler=compiler, result=result)
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2828, in run_ast_nodes
      if self.run_code(code, result):
    File "/usr/local/lib/python3.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
      exec(code_obj, self.user_global_ns, self.user_ns)
    File "<ipython-input-39-696fd2112f18>", line 2, in <module>
      model.fit(x_train, y_train, validation_split=0.2, callbacks=callbacks, epochs=25, batch_size=72, shuffle=False)
    File "/usr/local/lib/python3.7/dist-packages/keras/utils/traceback_utils.py", line 64, in error_handler
      return fn(*args, **kwargs)
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1384, in fit
      threading. If unspecified, `use_multiprocessing` will default to
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function
      If an integer, specifies how many training epochs to run before a
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function
      the dataset will be consumed, the evaluation will start from the
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step
      `steps_per_epoch` argument. This argument is not supported with
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 860, in train_step
      validation_data=None,
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 919, in compute_loss
      0 = silent, 1 = progress bar, 2 = one line per epoch.
    File "/usr/local/lib/python3.7/dist-packages/keras/engine/compile_utils.py", line 201, in __call__
      loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 141, in __call__
      call_fn = tf.__internal__.autograph.tf_convert(self.call, tf.__internal__.autograph.control_status_ctx())
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 245, in call
      ag_fn = tf.__internal__.autograph.tf_convert(self.fn, tf.__internal__.autograph.control_status_ctx())
    File "/usr/local/lib/python3.7/dist-packages/keras/losses.py", line 1329, in mean_squared_error
      ...     100. * np.mean(np.abs((y_true - y_pred) / y_true), axis=-1))
Node: 'mean_squared_error/SquaredDifference'
required broadcastable shapes
    [[{{node mean_squared_error/SquaredDifference}}]] [Op:__inference_train_function_5545]

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

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

发布评论

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

评论(1

空袭的梦i 2025-02-02 04:07:55

这通常会发生,因为您对LSTM的最后一层具有return_sepences = true,这是softmax layer或最终概率计算层的意外行为。

This would happen generally because of you having return_sequences = True for the last layer of the LSTM which is unexpected behavior for the softmax layer or the final probability calculation layer.

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