Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

训练模型时报错: Incompatible shapes: [14] vs. [14,50] #50

Open
DragonwolfAside opened this issue May 23, 2023 · 2 comments
Open

Comments

@DragonwolfAside
Copy link

DragonwolfAside commented May 23, 2023

环境:
Python 3.10.10
Tensorflow 2.12.0

日志:

Tue May 23 12:23:08 2023 正在处理训练数据...
Tue May 23 12:23:08 2023 循环轮数:5 batch size:20
Epoch 1/5
Traceback (most recent call last):
  File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\main.py", line 35, in <module>
    main()
  File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\main.py", line 22, in main
    seq.train_model(size, epoch)
  File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\seq2seq.py", line 79, in train_model
    model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
  File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\tensorflow\python\eager\execute.py", line 52, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.InvalidArgumentError: Graph execution error:

Detected at node 'Equal' defined at (most recent call last):
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\main.py", line 35, in <module>
      main()
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\main.py", line 22, in main
      seq.train_model(size, epoch)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\seq2seq.py", line 79, in train_model
      model.fit([encoder_input_data, decoder_input_data], decoder_target_data,
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\engine\training.py", line 1685, in fit
      tmp_logs = self.train_function(iterator)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\engine\training.py", line 1284, in train_function
      return step_function(self, iterator)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\engine\training.py", line 1268, in step_function
      outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\engine\training.py", line 1249, in run_step
      outputs = model.train_step(data)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\engine\training.py", line 1055, in train_step
      return self.compute_metrics(x, y, y_pred, sample_weight)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\engine\training.py", line 1149, in compute_metrics
      self.compiled_metrics.update_state(y, y_pred, sample_weight)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\engine\compile_utils.py", line 605, in update_state
      metric_obj.update_state(y_t, y_p, sample_weight=mask)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\utils\metrics_utils.py", line 77, in decorated
      update_op = update_state_fn(*args, **kwargs)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\metrics\base_metric.py", line 140, in update_state_fn
      return ag_update_state(*args, **kwargs)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\metrics\base_metric.py", line 691, in update_state
      matches = ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\metrics\accuracy_metrics.py", line 426, in categorical_accuracy
      return metrics_utils.sparse_categorical_matches(
    File "C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot\venv\lib\site-packages\keras\utils\metrics_utils.py", line 971, in sparse_categorical_matches
      matches = tf.cast(tf.equal(y_true, y_pred), backend.floatx())
Node: 'Equal'
Incompatible shapes: [14] vs. [14,50]
         [[{{node Equal}}]] [Op:__inference_train_function_19230]

(venv) C:\Users\Administrator\Desktop\NLP\OSSAS Chatbot>
@3588044667HZ
Copy link

我的也这样

@RepentStar
Copy link

RepentStar commented Dec 9, 2023

代码执行错误提示“InvalidArgumentError: Graph execution error”通常是因为TensorFlow计算图中的某个操作失败了。具体到这段代码,错误是在train_model函数中触发的,而错误的根本原因是“Detected at node Equal defined at”和“Incompatible shapes: [2] vs. [2,15]”。
这表明在计算图中有两个形状不兼容的Tensor在进行比较操作(Equal节点)。一个形状是[2],另一个是[2,15],显然这两个形状不能进行比较。
要解决这个问题,你需要检查train_model函数中所有进行比较的操作,确保比较的Tensor具有相同的形状。特别是,需要检查在模型编译阶段定义的损失函数和评价指标,以及在训练过程中使用的任何自定义函数或层。
你可以通过以下步骤来调试这个问题:
确认在模型编译阶段,损失函数和评价指标使用的Tensor形状是否一致。
如果使用自定义层或函数,检查这些层或函数的输入和输出Tensor的形状是否匹配。
在训练循环中,检查传入模型的数据(输入)和模型的预测结果(输出)的形状是否一致。
解决上述问题后,错误应该会得到解决。如果问题依然存在,可能需要进一步检查模型的架构,以确保在训练过程中所有Tensor的形状都是预期的。
以上内容由AI回答。
这个项目几年来并没有任何变化,两年前我训练也是正常的,不知道为什么现在就不行了,报错类型和这个issue一样,训练数据一模一样,不知道哪里出了问题
注:我两年前用的是.exe进行的训练,现在出现这个问题的好像都是用源代码训练的

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants