Install any skill in seconds. Free to start, no credit card required.
Get Started Free →brycewang-stanford/pytorch-guide
Avoid common PyTorch mistakes and apply robust training patterns
Passed 21 of 23 test cases with the skill, and 20 of 23 without it.
1 fixed20 passed either way2 still failing0 broke
Tokens, turns and time describe resource use, not a dollar price.
A test case the skill fixed. Same model, same prompt, with and without the skill.
I defined a PyTorch Dataset subclass with `def __init__(self, data, transforms=[]): self.transforms = transforms`. Some users report that transforms added in one instance contaminate other instances. How should I rewrite this constructor?
| Checked | Without skill | With skill |
|---|---|---|
| Sets default value of transforms parameter to None in constructor signature. | Passed | Passed |
| Initializes self.transforms using transforms or [] pattern. | Missed | Passed |
The instructions your model reads when the skill loads.
PyTorch is the dominant deep learning framework in academic research, used in the majority of papers at NeurIPS, ICML, and ICLR. Its eager execution model, Pythonic API, and seamless integration with the Python scientific stack make it the default choice for prototyping and publishing research code.
However, PyTorch's flexibility is a double-edged sword. Subtle bugs -- forgetting model.eval(), accumulating gradients across batches, incorrect device placement, memory leaks from detached tensors -- can silently corrupt results without raising errors. These issues are especially dangerous in research settings where ground truth is unknown.
This guide catalogs the most common PyTorch mistakes, provides battle-tested training patterns, and covers performance optimization techniques that every researcher should know. The patterns here are drawn from top-tier ML research codebases and the PyTorch team's own best practice recommendations.
# MISTAKE 1: Forgetting model.eval() and torch.no_grad()
# This causes dropout and batch norm to behave incorrectly during evaluation
# and wastes memory by tracking gradients
# WRONG
def evaluate(model, dataloader):
total_correct = 0
for x, y in dataloader:
output = model(x) # Dropout still active! BN using batch stats!
total_correct += (output.argmax(1) == y).sum().item()
# RIGHT
@torch.no_grad()
def evaluate(model, dataloader):
model.eval()
total_correct = 0
for x, y in dataloader:
output = model(x)
total_correct += (output.argmax(1) == y).sum().item()
model.train() # Restore training mode
return total_correct
# MISTAKE 2: Not zeroing gradients (they accumulate by default!)
test cases passed, vs 20 / 23 without
Published skills whose descriptions closely match this one, with their headline benchmark lift.
More skills from the same source and nearby in the registry, with their headline benchmark lift.