{
  "id": "pytorch/loss-nan-step",
  "signature": "RuntimeError: Loss is NaN or Inf at step 1000",
  "signature_zh": "运行时错误：在第1000步时损失为NaN或Inf",
  "regex": "RuntimeError: Loss is NaN or Inf",
  "domain": "pytorch",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The loss function output becomes NaN or Inf due to numerical instability, often caused by exploding gradients, division by zero, log of zero, or incorrect input scaling.",
  "root_cause_type": "generic",
  "root_cause_zh": "损失函数输出因数值不稳定变为NaN或Inf，通常由梯度爆炸、除零、对零取对数或输入缩放不正确导致。",
  "versions": [
    {
      "version": "pytorch>=1.12",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "torchvision>=0.13",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "cuda>=11.6",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Increasing learning rate to escape local minima",
      "why_fails": "Higher learning rate exacerbates gradient explosion, making NaN more likely.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    },
    {
      "action": "Removing gradient clipping completely",
      "why_fails": "Without clipping, unchecked large gradients cause overflow, leading to NaN.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add gradient clipping: torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) before optimizer.step()",
      "success_rate": 0.85,
      "how": "Add gradient clipping: torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) before optimizer.step()",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use a smaller learning rate, e.g., optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)",
      "success_rate": 0.8,
      "how": "Use a smaller learning rate, e.g., optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)",
      "condition": "",
      "sources": []
    },
    {
      "action": "Ensure input data is normalized (e.g., mean=0, std=1) and avoid log(0) by adding a small epsilon like 1e-8",
      "success_rate": 0.9,
      "how": "Ensure input data is normalized (e.g., mean=0, std=1) and avoid log(0) by adding a small epsilon like 1e-8",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Add gradient clipping: torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) before optimizer.step()",
    "Use a smaller learning rate, e.g., optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)",
    "Ensure input data is normalized (e.g., mean=0, std=1) and avoid log(0) by adding a small epsilon like 1e-8"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/notes/faq.html#my-loss-is-nan",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.85,
  "fix_success_rate": 0.85,
  "resolvable": "true",
  "first_seen": "2023-03-15",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}