{
  "id": "tensorflow/checkpoint-optimizer-slot-variable-mismatch",
  "signature": "NotFoundError: Key optimizer/slot_variable not found in checkpoint",
  "signature_zh": "NotFoundError: 检查点中未找到键 optimizer/slot_variable",
  "regex": "NotFoundError: Key optimizer/slot_variable not found in checkpoint",
  "domain": "tensorflow",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "The checkpoint was saved with a different optimizer (e.g., Adam) but is being restored with a model that uses a different optimizer (e.g., SGD), so the slot variables (e.g., m, v) are missing.",
  "root_cause_type": "generic",
  "root_cause_zh": "检查点使用不同的优化器（例如 Adam）保存，但恢复时模型使用了不同的优化器（例如 SGD），因此插槽变量（如 m、v）缺失。",
  "versions": [
    {
      "version": "tensorflow 2.6",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tensorflow 2.7",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "tensorflow 2.8",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "",
      "why_fails": "While it resolves the mismatch, it discards all previously trained weights.",
      "fail_rate": 0.7,
      "condition": "",
      "sources": []
    },
    {
      "action": "",
      "why_fails": "Checkpoint files are protocol buffers; manual edits corrupt the file and cause DataLossError.",
      "fail_rate": 0.95,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Restore only the model weights using tf.train.Checkpoint(model=model).restore(path) and skip optimizer slot variables by not including optimizer in the checkpoint object. Then reinitialize the optimizer.",
      "success_rate": 0.9,
      "how": "Restore only the model weights using tf.train.Checkpoint(model=model).restore(path) and skip optimizer slot variables by not including optimizer in the checkpoint object. Then reinitialize the optimizer.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Switch to the original optimizer that was used during checkpoint saving. For example, if the checkpoint was saved with Adam, use tf.keras.optimizers.Adam() during restoration.",
      "success_rate": 0.95,
      "how": "Switch to the original optimizer that was used during checkpoint saving. For example, if the checkpoint was saved with Adam, use tf.keras.optimizers.Adam() during restoration.",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "使用 tf.train.Checkpoint(model=model).restore(path) 仅恢复模型权重，并跳过优化器插槽变量（不在检查点对象中包含优化器）。然后重新初始化优化器。",
    "切换到保存检查点时使用的原始优化器。例如，如果检查点使用 Adam 保存，则在恢复时使用 tf.keras.optimizers.Adam()。"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.tensorflow.org/guide/checkpoint#loading_checkpoints",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.87,
  "fix_success_rate": 0.88,
  "resolvable": "true",
  "first_seen": "2023-04-10",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}