---
name: pytorch-model-explainer
description: Walks through a PyTorch nn.Sequential or nn.Module model definition layer by layer, explaining what each layer does in plain language and how the tensor's shape changes as data flows through it. Use whenever the user shares a PyTorch model definition and asks to explain it, understand it, check it makes sense, or debug why it won't build/train. Especially useful for beginners connecting course concepts (Conv2d, ReLU, MaxPool2d, Dropout, Flatten, Linear) to a specific model they've written themselves.
---

# PyTorch Model Explainer

Someone shares a `nn.Sequential(...)` or a custom `nn.Module` class. The job is to walk it top to bottom exactly the way a good lesson would: what each layer does, and — critically — what shape the data is at each step, since that's where beginners' models actually break.

## How to respond

For each layer, in order, give one short line covering:

1. **What this layer does**, in plain language (not just its name).
2. **The tensor's shape after this layer**, computed explicitly from the shape before it. Do the arithmetic — don't just assert it.

Format as a simple running list, e.g.:

```
Input:            (batch, 1, 28, 28)
Conv2d(1,25,3,padding=1)  → detects local patterns; padding keeps size the same
                  (batch, 25, 28, 28)
MaxPool2d(2)      → halves height & width, keeps the strongest signal
                  (batch, 25, 14, 14)
Flatten()         → lays the grid out as one long row of numbers
                  (batch, 25*14*14) = (batch, 4900)
Linear(4900, 128) → expects exactly 4900 incoming values per example
                  (batch, 128)
```

Then, after the walkthrough, give a one-paragraph plain-language summary of what the whole model is doing end to end (e.g. "this model looks for local visual patterns, shrinks the image down twice while keeping the strongest signals, then makes its final decision from those compressed features").

## Layer vocabulary (define plainly, every time — don't assume familiarity)

- **Linear** — every input number connects to every output number; a fully-connected layer.
- **Conv2d** — slides a small learned filter across the image to detect local patterns (edges, textures) wherever they appear.
- **ReLU** — turns negative numbers to 0, leaves positive numbers unchanged; this is what lets stacking layers actually add power.
- **MaxPool2d(k)** — shrinks height and width by a factor of k, keeping only the strongest value in each little window.
- **BatchNorm2d / BatchNorm1d** — normalizes values within a batch so training is more stable and faster.
- **Dropout(p)** — randomly zeroes out a fraction p of values during training only, to fight overfitting (a model memorizing training data instead of learning general patterns).
- **Flatten()** — reshapes a multi-dimensional tensor into one long row per example, needed before a Linear layer.

## Common things to flag automatically

- **A shape that won't actually work** — e.g. a `Linear` layer's first argument doesn't match what the previous layer actually outputs. State the exact numbers and the exact mismatch, the same way the `pytorch-error-explainer` skill would if this were a real runtime crash.
- **Missing activation functions** between stacked `Linear` or `Conv2d` layers — flag that stacking linear operations with no activation between them collapses mathematically into one big linear operation, so nothing extra is being learned.
- **Dropout or BatchNorm present with no mention of `model.train()` / `model.eval()`** in surrounding code (if shown) — gently note that these layers behave differently in training vs. evaluation, so calling the right mode matters.
- **Output layer size** — if this looks like a classification model, check the final layer's output size matches the number of classes the user says they're predicting, and mention it explicitly if it looks off.

## Tone

Same voice throughout: warm, plain, short sentences, define every technical term the first time it's used in this response — never assume the reader already knows what "in_features" or "stride" means just because they wrote the code.
