pytorch
assertion_error
ai_generated
true
RuntimeError: 在第 500 步损失为 NaN 或 Inf。考虑降低学习率、添加梯度裁剪或检查输入数据中的异常值。
RuntimeError: Loss is NaN or Inf at step 500. Consider reducing learning rate, adding gradient clipping, or checking input data for outliers.
ID: pytorch/nan-loss-step
80%修复率
90%置信度
1证据数
2023-02-10首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| torch>=1.8 | active | — | — | — |
| torch>=2.0 | active | — | — | — |
根因分析
训练损失变为 NaN 或 Inf,原因包括梯度爆炸、数值不稳定(例如,对零取对数)或输入数据中存在极端异常值。
English
Training loss becomes NaN or Inf due to exploding gradients, numerical instability (e.g., log of zero), or corrupted input data with extreme values.
官方文档
https://pytorch.org/docs/stable/notes/numerical_stability.html解决方案
-
Add gradient clipping: torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) after loss.backward() and before optimizer.step()
-
Reduce learning rate by factor of 10: optimizer = torch.optim.Adam(model.parameters(), lr=1e-4) instead of 1e-3
-
Add a small epsilon to log inputs: loss = F.cross_entropy(logits, targets) inside a try block with torch.clamp(logits, min=-100, max=100)
无效尝试
常见但无效的做法:
-
Setting torch.set_default_dtype(torch.float64) to increase precision
65% 失败
Float64 may delay but not prevent NaN if the root cause is exploding gradients or log(0); also slows training significantly.
-
Restarting training from scratch without changes
95% 失败
The same hyperparameters and data will produce the same NaN at the same step; the issue is deterministic.