---
name: pytorch-error-explainer
description: Explains a PyTorch error, exception, or traceback in plain, jargon-free language — what actually went wrong, why, and the concrete fix — instead of just restating the stack trace. Use this whenever the user pastes a PyTorch error message or traceback, or asks "why is my PyTorch code failing," "what does this error mean," or "how do I fix this" about a deep learning script. Especially useful for beginners coming out of a course like Deep Learning Foundations who know the concepts but haven't seen the error messages yet.
---

# PyTorch Error Explainer

Beginners who understand tensors, layers, and training loops conceptually still freeze the first time PyTorch throws a wall of red text at them. This skill's job is to translate the traceback into the same plain language the underlying course concepts were taught in — never assume familiarity with the error's own vocabulary.

## How to respond

For any PyTorch error the user shares, structure the answer in exactly three short parts:

1. **What actually happened** — one or two sentences, zero jargon, describing the real-world situation (not the error's wording). E.g. "PyTorch expected a batch of images shaped like `(32, 1, 28, 28)` but got something shaped `(32, 28, 28)` instead — it's missing the channel dimension."
2. **Why it happened** — connect it to the *concept*, not just the syntax. If it's a shape mismatch, explain which step (reshape, unsqueeze, a layer's expected input) skipped or miscounted a dimension. If it's a dtype error, explain what a dtype is and which two types collided. Do not just say "there's a dtype mismatch" — say what a dtype is if the user seems early-stage.
3. **The concrete fix** — one corrected line or small snippet, with a one-line explanation of why it fixes it. Never hand back a wall of alternative solutions; pick the most likely one first, then briefly mention if there's a common alternate cause.

## Common errors and how to explain them plainly

- **`RuntimeError: shape '[...]' is invalid for input of size N`** or **`size mismatch`** — a reshape, flatten, or layer's expected input doesn't match what's actually flowing through. Walk through the actual numbers: what shape came in, what shape was expected, where the mismatch is. This is almost always a batch dimension, channel dimension, or a `Linear` layer's `in_features` not matching what `Flatten()` actually produced.
- **`RuntimeError: expected scalar type Float but found Double`** (or vice versa) — two tensors of different numeric precision were combined. Explain: PyTorch is strict about number types matching exactly; this usually comes from mixing a NumPy array's default type with a model that expects `float32`. Fix: `.float()` on the offending tensor.
- **`RuntimeError: Trying to backward through the graph a second time`** — gradients were already used and cleared once; calling `.backward()` again on the same computation without `optimizer.zero_grad()` or recomputing the forward pass first. Explain the training-loop order (from the course): zero grad → forward → loss → backward → step, and point to whichever step got skipped or reordered.
- **`RuntimeError: CUDA out of memory`** (or on CPU, just very slow / hangs) — batch size or model size is too large for available memory. Explain in terms of "how many images are being processed as one group" and suggest lowering `batch_size`.
- **`AttributeError: 'NoneType' object has no attribute ...`** on a gradient or output — usually means a variable that was expected to hold a tensor is actually `None`, often because a function returned nothing, or `.backward()` was never called before checking `.grad`.
- **Model gives the same prediction for every input** — not a crash, but treat it the same way: walk through likely causes plainly — did training actually run (check the loop ran epochs > 0), is the learning rate near zero, is the loss actually decreasing, was `model.eval()` accidentally left off before checking predictions (if the model has BatchNorm, this can make outputs collapse).

## Tone

Match the course's own voice: warm, direct, no unexplained jargon, short sentences. If a term must be used (dtype, gradient, in_features), define it in the same breath the first time — never assume the reader already knows the vocabulary just because they hit the error.

Do not just paste a "here are 5 possible causes" list. Diagnose the most likely cause from the actual error and code shown, explain it, fix it. If genuinely ambiguous between two causes, say so briefly and ask one clarifying question rather than dumping every possibility.
