{
  "id": "tensorflow/optimizer-nan-loss",
  "signature": "tensorflow.python.framework.errors_impl.InvalidArgumentError: Loss is inf or nan : Tensor had NaN values",
  "signature_zh": "tensorflow.python.framework.errors_impl.InvalidArgumentError: 损失为 inf 或 nan: 张量包含 NaN 值",
  "regex": "Loss is inf or nan.*Tensor had NaN values",
  "domain": "tensorflow",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "The loss function produced NaN values, often due to exploding gradients, division by zero, or log of zero in the loss computation.",
  "root_cause_type": "generic",
  "root_cause_zh": "损失函数产生了 NaN 值，通常是由于梯度爆炸、除以零或损失计算中对零取对数。",
  "versions": [
    {
      "version": "tensorflow 2.8.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tensorflow 2.9.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tensorflow 2.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "This may delay NaN but does not fix the root cause; the loss can still explode later.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "SGD is also susceptible to exploding gradients without clipping.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Add gradient clipping in the optimizer: `optimizer = tf.keras.optimizers.Adam(clipnorm=1.0)` or use `tf.clip_by_global_norm`. Also check for log(0) by adding a small epsilon: `loss = -tf.reduce_sum(y_true * tf.math.log(y_pred + 1e-10))`.",
      "success_rate": 0.85,
      "how": "Add gradient clipping in the optimizer: `optimizer = tf.keras.optimizers.Adam(clipnorm=1.0)` or use `tf.clip_by_global_norm`. Also check for log(0) by adding a small epsilon: `loss = -tf.reduce_sum(y_true * tf.math.log(y_pred + 1e-10))`.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Add gradient clipping in the optimizer: `optimizer = tf.keras.optimizers.Adam(clipnorm=1.0)` or use `tf.clip_by_global_norm`. Also check for log(0) by adding a small epsilon: `loss = -tf.reduce_sum(y_true * tf.math.log(y_pred + 1e-10))`."
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.tensorflow.org/guide/keras/train_and_evaluate",
  "official_doc_section": null,
  "error_code": "NAN_LOSS",
  "verification_tier": "ai_generated",
  "confidence": 0.9,
  "fix_success_rate": 0.75,
  "resolvable": "partial",
  "first_seen": "2023-03-12",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}