# 运行时错误：在第1000步时损失为NaN或Inf

- **ID:** `pytorch/loss-nan-step`
- **领域:** pytorch
- **类别:** runtime_error
- **验证级别:** ai_generated
- **修复率:** 85%

## 根因

损失函数输出因数值不稳定变为NaN或Inf，通常由梯度爆炸、除零、对零取对数或输入缩放不正确导致。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| pytorch>=1.12 | active | — | — |
| torchvision>=0.13 | active | — | — |
| cuda>=11.6 | active | — | — |

## 解决方案

1. ```
   Add gradient clipping: torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) before optimizer.step()
   ```
2. ```
   Use a smaller learning rate, e.g., optimizer = torch.optim.Adam(model.parameters(), lr=1e-4)
   ```
3. ```
   Ensure input data is normalized (e.g., mean=0, std=1) and avoid log(0) by adding a small epsilon like 1e-8
   ```

## 无效尝试

- **Increasing learning rate to escape local minima** — Higher learning rate exacerbates gradient explosion, making NaN more likely. (95% 失败率)
- **Removing gradient clipping completely** — Without clipping, unchecked large gradients cause overflow, leading to NaN. (90% 失败率)
