{
  "id": "pytorch/optimizer-state-dict-mismatch",
  "signature": "ValueError: optimizer state dict mismatch: loaded state dict contains parameters that are not in the current optimizer. Expected keys: ['param_groups', 'state']. Got: ['param_groups', 'state', 'extra_key']",
  "signature_zh": "值错误：优化器状态字典不匹配：加载的状态字典包含当前优化器中没有的参数。期望键：['param_groups', 'state']。得到：['param_groups', 'state', 'extra_key']",
  "regex": "ValueError: optimizer state dict mismatch: loaded state dict contains parameters that are not in the current optimizer\\. Expected keys: \\[.*\\]\\. Got: \\[.*\\]",
  "domain": "pytorch",
  "category": "data_error",
  "subcategory": null,
  "root_cause": "The saved optimizer state dict contains keys that do not match the current optimizer's parameter groups, often due to a change in model architecture or optimizer configuration between save and load.",
  "root_cause_type": "generic",
  "root_cause_zh": "保存的优化器状态字典包含与当前优化器参数组不匹配的键，通常由于保存和加载之间模型架构或优化器配置发生变化。",
  "versions": [
    {
      "version": "pytorch>=1.9",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "python>=3.7",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Ignoring the error by setting strict=False in load_state_dict",
      "why_fails": "The optimizer may silently skip mismatched parameters, leading to incorrect training state.",
      "fail_rate": 0.6,
      "condition": "",
      "sources": []
    },
    {
      "action": "Re-saving the optimizer state dict without changes",
      "why_fails": "The mismatch persists because the underlying architecture changed.",
      "fail_rate": 0.9,
      "condition": "",
      "sources": []
    },
    {
      "action": "Manually editing the state dict file to remove extra keys",
      "why_fails": "Editing state dict files manually is error-prone and may corrupt the data.",
      "fail_rate": 0.8,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Ensure the model and optimizer are constructed identically before loading: recreate the model and optimizer with the same configuration as when the state dict was saved.",
      "success_rate": 0.9,
      "how": "Ensure the model and optimizer are constructed identically before loading: recreate the model and optimizer with the same configuration as when the state dict was saved.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Use strict=False and then manually align parameters: `optimizer.load_state_dict(state_dict, strict=False)` then iterate over param_groups to fix mismatches.",
      "success_rate": 0.8,
      "how": "Use strict=False and then manually align parameters: `optimizer.load_state_dict(state_dict, strict=False)` then iterate over param_groups to fix mismatches.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Implement a custom loading function that filters out unexpected keys: `filtered_dict = {k: v for k, v in state_dict.items() if k in expected_keys}; optimizer.load_state_dict(filtered_dict)`",
      "success_rate": 0.75,
      "how": "Implement a custom loading function that filters out unexpected keys: `filtered_dict = {k: v for k, v in state_dict.items() if k in expected_keys}; optimizer.load_state_dict(filtered_dict)`",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Ensure the model and optimizer are constructed identically before loading: recreate the model and optimizer with the same configuration as when the state dict was saved.",
    "Use strict=False and then manually align parameters: `optimizer.load_state_dict(state_dict, strict=False)` then iterate over param_groups to fix mismatches.",
    "Implement a custom loading function that filters out unexpected keys: `filtered_dict = {k: v for k, v in state_dict.items() if k in expected_keys}; optimizer.load_state_dict(filtered_dict)`"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://pytorch.org/docs/stable/optim.html#torch.optim.Optimizer.load_state_dict",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.84,
  "fix_success_rate": 0.8,
  "resolvable": "true",
  "first_seen": "2023-04-22",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}