{
  "id": "tensorflow/not-implemented-error-graph-mode-while-loop",
  "signature": "NotImplementedError: while_loop is not supported in eager mode",
  "signature_zh": "未实现错误：急切模式不支持while_loop",
  "regex": "NotImplementedError: while_loop is not supported in eager mode",
  "domain": "tensorflow",
  "category": "runtime_error",
  "subcategory": null,
  "root_cause": "Using tf.while_loop inside a tf.function that is not properly traced, or mixing eager execution with graph-only operations.",
  "root_cause_type": "generic",
  "root_cause_zh": "在未正确追踪的tf.function内部使用tf.while_loop，或将急切执行与仅图操作混合使用。",
  "versions": [
    {
      "version": "tensorflow 2.10.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tensorflow 2.11.0",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "tf.function requires all loop variables to be tensors; Python objects cause tracing errors.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "TF2 is designed for eager mode; disabling it is not recommended and can cause compatibility issues.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Replace tf.while_loop with a Python while loop inside a tf.function that uses tf.constant for bounds:\n@tf.function\ndef my_loop(n):\n    i = tf.constant(0)\n    while i < n:\n        # loop body\n        i += 1\n    return i\nresult = my_loop(tf.constant(5))",
      "success_rate": 0.8,
      "how": "Replace tf.while_loop with a Python while loop inside a tf.function that uses tf.constant for bounds:\n@tf.function\ndef my_loop(n):\n    i = tf.constant(0)\n    while i < n:\n        # loop body\n        i += 1\n    return i\nresult = my_loop(tf.constant(5))",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use tf.range and tf.map_fn for vectorized operations instead of explicit loops:\n@tf.function\ndef my_fn(n):\n    return tf.map_fn(lambda x: x*2, tf.range(n))\nresult = my_fn(tf.constant(5))",
      "success_rate": 0.75,
      "how": "Use tf.range and tf.map_fn for vectorized operations instead of explicit loops:\n@tf.function\ndef my_fn(n):\n    return tf.map_fn(lambda x: x*2, tf.range(n))\nresult = my_fn(tf.constant(5))",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Replace tf.while_loop with a Python while loop inside a tf.function that uses tf.constant for bounds:\n@tf.function\ndef my_loop(n):\n    i = tf.constant(0)\n    while i < n:\n        # loop body\n        i += 1\n    return i\nresult = my_loop(tf.constant(5))",
    "Use tf.range and tf.map_fn for vectorized operations instead of explicit loops:\n@tf.function\ndef my_fn(n):\n    return tf.map_fn(lambda x: x*2, tf.range(n))\nresult = my_fn(tf.constant(5))"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.tensorflow.org/api_docs/python/tf/while_loop",
  "official_doc_section": null,
  "error_code": "NIE",
  "verification_tier": "ai_generated",
  "confidence": 0.82,
  "fix_success_rate": 0.78,
  "resolvable": "true",
  "first_seen": "2023-10-05",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}