# NotFoundError: 检查点中未找到键 optimizer/slot_variable

- **ID:** `tensorflow/checkpoint-optimizer-slot-variable-mismatch`
- **领域:** tensorflow
- **类别:** data_error
- **验证级别:** ai_generated
- **修复率:** 88%

## 根因

检查点使用不同的优化器（例如 Adam）保存，但恢复时模型使用了不同的优化器（例如 SGD），因此插槽变量（如 m、v）缺失。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| tensorflow 2.6 | active | — | — |
| tensorflow 2.7 | active | — | — |
| tensorflow 2.8 | active | — | — |

## 解决方案

1. ```
   使用 tf.train.Checkpoint(model=model).restore(path) 仅恢复模型权重，并跳过优化器插槽变量（不在检查点对象中包含优化器）。然后重新初始化优化器。
   ```
2. ```
   切换到保存检查点时使用的原始优化器。例如，如果检查点使用 Adam 保存，则在恢复时使用 tf.keras.optimizers.Adam()。
   ```

## 无效尝试

- **** — While it resolves the mismatch, it discards all previously trained weights. (70% 失败率)
- **** — Checkpoint files are protocol buffers; manual edits corrupt the file and cause DataLossError. (95% 失败率)
