You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

手语检测模型训练遇InvalidArgumentError问题求助

手语检测模型训练报错:logits与标签维度不匹配

项目描述

这是一个手语检测代码,使用的库:

from keras.models import Sequential

相关训练代码

训练模型时执行以下代码块出现错误:

classifier.fit(
        training_set,
        steps_per_epoch=12841,
        epochs=5,
        validation_data=test_set,
        validation_steps=4268)

错误信息

Traceback (most recent call last):
  File "c:/Users/nagar/Desktop/Sign-Language-to-Text-master/train.py", line 66, in <module>
    classifier.fit(
  File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\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 'categorical_crossentropy/softmax_cross_entropy_with_logits' defined at (most recent call last):
    File "c:/Users/nagar/Desktop/Sign-Language-to-Text-master/train.py", 
line 66, in <module>
      classifier.fit(
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\engine\training.py", line 1650, in fit
      tmp_logs = self.train_function(iterator)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\engine\training.py", line 1249, in train_function
      return step_function(self, iterator)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\engine\training.py", line 1233, in step_function      outputs = model.distribute_strategy.run(run_step, args=(data,))    
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\engine\training.py", line 1222, in run_step     
      outputs = model.train_step(data)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\engine\training.py", line 1024, in train_step   
      loss = self.compute_loss(x, y, y_pred, sample_weight)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\engine\training.py", line 1082, in compute_loss 
      return self.compiled_loss(
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\engine\compile_utils.py", line 265, in __call__ 
      loss_value = loss_obj(y_t, y_p, sample_weight=sw)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\losses.py", line 152, in __call__
      losses = call_fn(y_true, y_pred)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\losses.py", line 284, in call
      return ag_fn(y_true, y_pred, **self._fn_kwargs)
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\losses.py", line 2004, in categorical_crossentropy
      return backend.categorical_crossentropy(
    File "C:\Users\nagar\Desktop\Sign-Language-to-Text-master\Virtual-Env\lib\site-packages\keras\backend.py", line 5538, in categorical_crossentropy
      return tf.nn.softmax_cross_entropy_with_logits(
Node: 'categorical_crossentropy/softmax_cross_entropy_with_logits'       
logits and labels must be broadcastable: logits_size=[10,27] labels_size=[10,29]
         [[{{node categorical_crossentropy/softmax_cross_entropy_with_logits}}]] [Op:__inference_train_function_1784]
2022-12-16 19:17:46.716640: W tensorflow/core/kernels/data/generator_dataset_op.cc:108] Error occurred when finalizing GeneratorDataset iterator: 
FAILED_PRECONDITION: Python interpreter state is not initialized. The process may be terminated.
         [[{{node PyFunc}}]]

解决方案

核心问题是模型输出的类别数(27)和数据集标签的类别数(29)不匹配,导致交叉熵计算时维度无法对齐。按以下步骤修复:

  • 调整模型最后一层输出维度
    找到模型定义的最后一层(通常是Dense层),将units参数改为29,确保和数据集类别数一致:

    # 原错误代码示例
    # model.add(Dense(27, activation='softmax'))
    # 修改后
    model.add(Dense(29, activation='softmax'))
    
  • 验证数据集标签处理逻辑

    • 如果用ImageDataGenerator加载数据,确认class_mode='categorical'时,数据集目录下的类别文件夹数量为29
    • 如果是自定义标签,检查独热编码后的标签维度是否为29
  • 重新编译模型
    修改模型结构后,必须重新执行model.compile()步骤,确保损失函数与模型输出匹配

  • 修正训练步骤数
    steps_per_epoch和validation_steps的合理值应为「数据集样本数 ÷ batch_size」,当前12841和4268的数值过大,建议检查数据加载时的batch_size参数,调整步骤数至合理范围,避免资源浪费


内容的提问来源于stack exchange,提问作者Nagarjun A

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.08 09:01:14