{
  "id": "pytorch/invalid-argument-2d-index-target",
  "signature": "RuntimeError: 1D target tensor expected, multi-target not supported",
  "signature_zh": "运行时错误：预期一维目标张量，不支持多目标",
  "regex": "1D target tensor expected, multi-target not supported",
  "domain": "pytorch",
  "category": "shape_error",
  "subcategory": null,
  "root_cause": "A loss function like CrossEntropyLoss received a target tensor with more than one dimension (e.g., one-hot encoded labels) instead of a 1D tensor of class indices.",
  "root_cause_type": "generic",
  "root_cause_zh": "损失函数（如 CrossEntropyLoss）接收到的目标张量维度超过一维（例如独热编码标签），而非一维类别索引张量。",
  "versions": [
    {
      "version": "pytorch>=1.13.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This still produces a 2D tensor; the loss expects 1D indices.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "The error is about dimensionality, not dtype; CrossEntropyLoss expects Long type indices.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "BCEWithLogitsLoss expects a different shape and value range; it will not fix the dimensionality issue.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Convert one-hot encoded labels to class indices using `torch.argmax(target, dim=1)` before passing to the loss function. Example: `loss = criterion(output, target.argmax(dim=1))`",
      "success_rate": 0.95,
      "how": "Convert one-hot encoded labels to class indices using `torch.argmax(target, dim=1)` before passing to the loss function. Example: `loss = criterion(output, target.argmax(dim=1))`",
      "condition": "",
      "sources": []
    },
    {
      "action": "If the target is already 1D but has an extra dimension from batching, squeeze it: `target = target.squeeze()`.",
      "success_rate": 0.9,
      "how": "If the target is already 1D but has an extra dimension from batching, squeeze it: `target = target.squeeze()`.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use `torch.nn.BCEWithLogitsLoss` with proper target shape (same as output) if the task is multi-label classification.",
      "success_rate": 0.85,
      "how": "Use `torch.nn.BCEWithLogitsLoss` with proper target shape (same as output) if the task is multi-label classification.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "在传递给损失函数之前，使用 `torch.argmax(target, dim=1)` 将独热编码标签转换为类别索引。示例：`loss = criterion(output, target.argmax(dim=1))`",
    "如果目标已经是 1D 但由于批处理有额外维度，则挤压它：`target = target.squeeze()`。",
    "如果任务是多标签分类，则使用 `torch.nn.BCEWithLogitsLoss` 并具有正确的目标形状（与输出相同）。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/generated/torch.nn.CrossEntropyLoss.html",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.88,
  "fix_success_rate": 0.9,
  "resolvable": "true",
  "first_seen": "2023-08-20",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}