# NotImplementedError：当模型有多个输出且使用不同损失函数时，不支持将模型保存为HDF5格式

- **ID:** `tensorflow/keras-model-save-h5-weights`
- **领域:** tensorflow
- **类别:** config_error
- **错误码:** `KHS`
- **验证级别:** ai_generated
- **修复率:** 95%

## 根因

Keras的HDF5序列化不支持在多个输出上使用不同损失函数的模型；该格式无法正确存储损失配置。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| tensorflow>=2.11.0 | active | — | — |
| keras>=2.11 | active | — | — |
| python>=3.8 | active | — | — |

## 解决方案

1. ```
   Save the model using the SavedModel format instead: `model.save('my_model', save_format='tf')` (or `model.export('my_model')` in TF2.x). This format supports multiple outputs and heterogeneous losses.
   ```
2. ```
   Save only the model weights using `model.save_weights('model_weights.h5')` and then recreate the model architecture with the same loss configuration in code, then load the weights.
   ```

## 无效尝试

- **Manually editing the HDF5 file to add loss entries.** — HDF5 files are binary and not meant for manual editing; the internal structure is complex and likely to break the model loading. (95% 失败率)
- **Setting all loss functions to the same value temporarily to save, then restoring them.** — Even if the save succeeds, the model's loss configuration is lost; loading the model will have incorrect loss functions. (70% 失败率)
