# RuntimeError: Loss is NaN or Inf at step 1000

- **ID:** `pytorch/loss-nan-step`
- **Domain:** pytorch
- **Category:** runtime_error
- **Verification:** ai_generated
- **Fix Rate:** 85%

## Root Cause

The loss function output becomes NaN or Inf due to numerical instability, often caused by exploding gradients, division by zero, log of zero, or incorrect input scaling.

## Version Compatibility

| Version | Status | Introduced | Deprecated |
|---------|--------|------------|------------|
| pytorch>=1.12 | active | — | — |
| torchvision>=0.13 | active | — | — |
| cuda>=11.6 | active | — | — |

## Workarounds

1. **Add gradient clipping: torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0) before optimizer.step()** (85% success)
   ```
   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)** (80% success)
   ```
   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** (90% success)
   ```
   Ensure input data is normalized (e.g., mean=0, std=1) and avoid log(0) by adding a small epsilon like 1e-8
   ```

## Dead Ends

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