PyTorch Development Patterns

PyTorch deep learning patterns and best practices for building robust, efficient, and reproducible training pipelines, model…

You say
Buy it · $29 Read it before you buy $29 Written by affaan-m · unverified publisher
Context cost
3k tokensestimated from the bundle, loaded when it triggers
Bundle
1 file · 11.8 kBtext throughout, nothing executable
Licence
MITpaid listing
Last change
no release on file
Servers it uses
Noneruns standalone

What it does

PyTorch deep learning patterns and best practices for building robust, efficient, and reproducible training pipelines, model architectures, and data loading. Use when writing or reviewing PyTorch training loops, model architectures, or data loading, or when a run will not reproduce.

Installed, it changes the agent in these ways.

What this skill changes about the agent is not written down here yet. The listing was collected from its source, and the description is in its own SKILL.md.

Expertise

Domain judgement the base model does not have.

data science

The skill itself

This is the whole product. A skill is instructions the model reads, so there is nothing behind the listing you cannot see first — the front matter loads with every session, and the body below it loads when the skill triggers.

SKILL.md11.8 kB · 398 lines
--- name: pytorch-patterns description: PyTorch deep learning patterns and best practices for building robust, efficient, and reproducible training pipelines, model architectures, and data loading. Use when writing or reviewing PyTorch training loops, model architectures, or data loading, or when a run will not reproduce. metadata: origin: ECC ---
8# PyTorch Development Patterns
9
10Idiomatic PyTorch patterns and best practices for building robust, efficient, and reproducible deep learning applications.
11
12## When to Activate
13
14- Writing new PyTorch models or training scripts
15- Reviewing deep learning code
16- Debugging training loops or data pipelines
17- Optimizing GPU memory usage or training speed
18- Setting up reproducible experiments
19
20## Core Principles
21
22### 1. Device-Agnostic Code
23
24Always write code that works on both CPU and GPU without hardcoding devices.
25
26```python
27# Good: Device-agnostic
28device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
29model = MyModel().to(device)
30data = data.to(device)
31
32# Bad: Hardcoded device
33model = MyModel().cuda() # Crashes if no GPU
34data = data.cuda()
35```
36
37### 2. Reproducibility First
38
39Set all random seeds for reproducible results.
40
41```python
42# Good: Full reproducibility setup
43def set_seed(seed: int = 42) -> None:
44 torch.manual_seed(seed)
45 torch.cuda.manual_seed_all(seed)
46 np.random.seed(seed)
47 random.seed(seed)
48 torch.backends.cudnn.deterministic = True
49 torch.backends.cudnn.benchmark = False
50
51# Bad: No seed control
52model = MyModel() # Different weights every run
53```
54
55### 3. Explicit Shape Management
56
57Always document and verify tensor shapes.
58
59```python
60# Good: Shape-annotated forward pass
61def forward(self, x: torch.Tensor) -> torch.Tensor:
62 # x: (batch_size, channels, height, width)
63 x = self.conv1(x) # -> (batch_size, 32, H, W)
64 x = self.pool(x) # -> (batch_size, 32, H//2, W//2)
65 x = x.view(x.size(0), -1) # -> (batch_size, 32*H//2*W//2)
66 return self.fc(x) # -> (batch_size, num_classes)
67
68# Bad: No shape tracking
69def forward(self, x):
70 x = self.conv1(x)
71 x = self.pool(x)
72 x = x.view(x.size(0), -1) # What size is this?
73 return self.fc(x) # Will this even work?
74```
75
76## Model Architecture Patterns
77
78### Clean nn.Module Structure
79
80```python
81# Good: Well-organized module
82class ImageClassifier(nn.Module):
83 def __init__(self, num_classes: int, dropout: float = 0.5) -> None:
84 super().__init__()
85 self.features = nn.Sequential(
86 nn.Conv2d(3, 64, kernel_size=3, padding=1),
87 nn.BatchNorm2d(64),
88 nn.ReLU(inplace=True),
89 nn.MaxPool2d(2),
90 )
91 self.classifier = nn.Sequential(
92 nn.Dropout(dropout),
93 nn.Linear(64 * 16 * 16, num_classes),
94 )
95
96 def forward(self, x: torch.Tensor) -> torch.Tensor:
97 x = self.features(x)
98 x = x.view(x.size(0), -1)
99 return self.classifier(x)
100
101# Bad: Everything in forward
102class ImageClassifier(nn.Module):
103 def __init__(self):
104 super().__init__()
105
106 def forward(self, x):
107 x = F.conv2d(x, weight=self.make_weight()) # Creates weight each call!
108 return x
109```
110
111### Proper Weight Initialization
112
113```python
114# Good: Explicit initialization
115def _init_weights(self, module: nn.Module) -> None:
116 if isinstance(module, nn.Linear):
117 nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
118 if module.bias is not None:
119 nn.init.zeros_(module.bias)
120 elif isinstance(module, nn.Conv2d):
121 nn.init.kaiming_normal_(module.weight, mode="fan_out", nonlinearity="relu")
122 elif isinstance(module, nn.BatchNorm2d):
123 nn.init.ones_(module.weight)
124 nn.init.zeros_(module.bias)
125
126model = MyModel()
127model.apply(model._init_weights)
128```
129
130## Training Loop Patterns
131
132### Standard Training Loop
133
134```python
135# Good: Complete training loop with best practices
136def train_one_epoch(
137 model: nn.Module,
138 dataloader: DataLoader,
139 optimizer: torch.optim.Optimizer,
140 criterion: nn.Module,
141 device: torch.device,
142 scaler: torch.amp.GradScaler | None = None,
143) -> float:
144 model.train() # Always set train mode
145 total_loss = 0.0
146
147 for batch_idx, (data, target) in enumerate(dataloader):
148 data, target = data.to(device), target.to(device)
149
150 optimizer.zero_grad(set_to_none=True) # More efficient than zero_grad()
151
152 # Mixed precision training
153 with torch.amp.autocast("cuda", enabled=scaler is not None):
154 output = model(data)
155 loss = criterion(output, target)
156
157 if scaler is not None:
158 scaler.scale(loss).backward()
159 scaler.unscale_(optimizer)
160 torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
161 scaler.step(optimizer)
162 scaler.update()
163 else:
164 loss.backward()
165 torch.nn.utils.clip_grad_norm_(model.parameters(), max_norm=1.0)
166 optimizer.step()
167
168 total_loss += loss.item()
169
170 return total_loss / len(dataloader)
171```
172
173### Validation Loop
174
175```python
176# Good: Proper evaluation
177@torch.no_grad() # More efficient than wrapping in torch.no_grad() block
178def evaluate(
179 model: nn.Module,
180 dataloader: DataLoader,
181 criterion: nn.Module,
182 device: torch.device,
183) -> tuple[float, float]:
184 model.eval() # Always set eval mode — disables dropout, uses running BN stats
185 total_loss = 0.0
186 correct = 0
187 total = 0
188
189 for data, target in dataloader:
190 data, target = data.to(device), target.to(device)
191 output = model(data)
192 total_loss += criterion(output, target).item()
193 correct += (output.argmax(1) == target).sum().item()
194 total += target.size(0)
195
196 return total_loss / len(dataloader), correct / total
197```
198
199## Data Pipeline Patterns
200
201### Custom Dataset
202
203```python
204# Good: Clean Dataset with type hints
205class ImageDataset(Dataset):
206 def __init__(
207 self,
208 image_dir: str,
209 labels: dict[str, int],
210 transform: transforms.Compose | None = None,
211 ) -> None:
212 self.image_paths = list(Path(image_dir).glob("*.jpg"))
213 self.labels = labels
214 self.transform = transform
215
216 def __len__(self) -> int:
217 return len(self.image_paths)
218
219 def __getitem__(self, idx: int) -> tuple[torch.Tensor, int]:
220 img = Image.open(self.image_paths[idx]).convert("RGB")
221 label = self.labels[self.image_paths[idx].stem]
222
223 if self.transform:
224 img = self.transform(img)
225
226 return img, label
227```
228
229### Efficient DataLoader Configuration
230
231```python
232# Good: Optimized DataLoader
233dataloader = DataLoader(
234 dataset,
235 batch_size=32,
236 shuffle=True, # Shuffle for training
237 num_workers=4, # Parallel data loading
238 pin_memory=True, # Faster CPU->GPU transfer
239 persistent_workers=True, # Keep workers alive between epochs
240 drop_last=True, # Consistent batch sizes for BatchNorm
241)
242
243# Bad: Slow defaults
244dataloader = DataLoader(dataset, batch_size=32) # num_workers=0, no pin_memory
245```
246
247### Custom Collate for Variable-Length Data
248
249```python
250# Good: Pad sequences in collate_fn
251def collate_fn(batch: list[tuple[torch.Tensor, int]]) -> tuple[torch.Tensor, torch.Tensor]:
252 sequences, labels = zip(*batch)
253 # Pad to max length in batch
254 padded = nn.utils.rnn.pad_sequence(sequences, batch_first=True, padding_value=0)
255 return padded, torch.tensor(labels)
256
257dataloader = DataLoader(dataset, batch_size=32, collate_fn=collate_fn)
258```
259
260## Checkpointing Patterns
261
262### Save and Load Checkpoints
263
264```python
265# Good: Complete checkpoint with all training state
266def save_checkpoint(
267 model: nn.Module,
268 optimizer: torch.optim.Optimizer,
269 epoch: int,
270 loss: float,
271 path: str,
272) -> None:
273 torch.save({
274 "epoch": epoch,
275 "model_state_dict": model.state_dict(),
276 "optimizer_state_dict": optimizer.state_dict(),
277 "loss": loss,
278 }, path)
279
280def load_checkpoint(
281 path: str,
282 model: nn.Module,
283 optimizer: torch.optim.Optimizer | None = None,
284) -> dict:
285 checkpoint = torch.load(path, map_location="cpu", weights_only=True)
286 model.load_state_dict(checkpoint["model_state_dict"])
287 if optimizer:
288 optimizer.load_state_dict(checkpoint["optimizer_state_dict"])
289 return checkpoint
290
291# Bad: Only saving model weights (can't resume training)
292torch.save(model.state_dict(), "model.pt")
293```
294
295## Performance Optimization
296
297### Mixed Precision Training
298
299```python
300# Good: AMP with GradScaler
301scaler = torch.amp.GradScaler("cuda")
302for data, target in dataloader:
303 with torch.amp.autocast("cuda"):
304 output = model(data)
305 loss = criterion(output, target)
306 scaler.scale(loss).backward()
307 scaler.step(optimizer)
308 scaler.update()
309 optimizer.zero_grad(set_to_none=True)
310```
311
312### Gradient Checkpointing for Large Models
313
314```python
315# Good: Trade compute for memory
316from torch.utils.checkpoint import checkpoint
317
318class LargeModel(nn.Module):
319 def forward(self, x: torch.Tensor) -> torch.Tensor:
320 # Recompute activations during backward to save memory
321 x = checkpoint(self.block1, x, use_reentrant=False)
322 x = checkpoint(self.block2, x, use_reentrant=False)
323 return self.head(x)
324```
325
326### torch.compile for Speed
327
328```python
329# Good: Compile the model for faster execution (PyTorch 2.0+)
330model = MyModel().to(device)
331model = torch.compile(model, mode="reduce-overhead")
332
333# Modes: "default" (safe), "reduce-overhead" (faster), "max-autotune" (fastest)
334```
335
336## Quick Reference: PyTorch Idioms
337
338| Idiom | Description |
339|-------|-------------|
340| model.train() / model.eval() | Always set mode before train/eval |
341| torch.no_grad() | Disable gradients for inference |
342| optimizer.zero_grad(set_to_none=True) | More efficient gradient clearing |
343| .to(device) | Device-agnostic tensor/model placement |
344| torch.amp.autocast | Mixed precision for 2x speed |
345| pin_memory=True | Faster CPU→GPU data transfer |
346| torch.compile | JIT compilation for speed (2.0+) |
347| weights_only=True | Secure model loading |
348| torch.manual_seed | Reproducible experiments |
349| gradient_checkpointing | Trade compute for memory |
350
351## Anti-Patterns to Avoid
352
353```python
354# Bad: Forgetting model.eval() during validation
355model.train()
356with torch.no_grad():
357 output = model(val_data) # Dropout still active! BatchNorm uses batch stats!
358
359# Good: Always set eval mode
360model.eval()
361with torch.no_grad():
362 output = model(val_data)
363
364# Bad: In-place operations breaking autograd
365x = F.relu(x, inplace=True) # Can break gradient computation
366x += residual # In-place add breaks autograd graph
367
368# Good: Out-of-place operations
369x = F.relu(x)
370x = x + residual
371
372# Bad: Moving data to GPU inside the training loop repeatedly
373for data, target in dataloader:
374 model = model.cuda() # Moves model EVERY iteration!
375
376# Good: Move model once before the loop
377model = model.to(device)
378for data, target in dataloader:
379 data, target = data.to(device), target.to(device)
380
381# Bad: Using .item() before backward
382loss = criterion(output, target).item() # Detaches from graph!
383loss.backward() # Error: can't backprop through .item()
384
385# Good: Call .item() only for logging
386loss = criterion(output, target)
387loss.backward()
388print(f"Loss: {loss.item():.4f}") # .item() after backward is fine
389
390# Bad: Not using torch.save properly
391torch.save(model, "model.pt") # Saves entire model (fragile, not portable)
392
393# Good: Save state_dict
394torch.save(model.state_dict(), "model.pt")
395```
396
397__Remember__: PyTorch code should be device-agnostic, reproducible, and memory-conscious. When in doubt, profile with torch.profiler and check GPU memory with torch.cuda.memory_summary().
398
In the file
SKILL.md1,279 words
Files1
LicenceMIT
Why you can read it

Nothing in a skill executes. The client loads the text and the model follows it, so a skill can be audited the way a runbook is — by reading it.

What it costs in context

Skills are not billed by the call. They are paid for in context: every token the instructions occupy is a token your code, your diff and your conversation cannot use. Here is what this one takes and when it takes it.

≈90
always loaded
The name and description, so the model knows the skill exists and when to reach for it.
2,860
on trigger
The instruction body, read only when the skill fires.
1.5%
of a 200k window
Ten skills this size would take about 15% of the window before you open a file.
050k100k150k200k context window

3k tokens, estimated from the bundle at four bytes to the token, held for the rest of the session once it triggers. Middling. Fine to keep on in a project where you use it weekly, worth unloading in one where you never do.

Servers bill, skills cost

A server charges by the month. A skill charges once per session, in context, and then keeps charging it for as long as the session lives.

Before and after

The same question, put to the same model twice: once as it comes, and once with these instructions loaded.

No worked example has been published for this skill yet.

Adoption
Installsnone yet
Ratingno reviews yet

The procedure it runs

The procedure has not been published here. It is in the skill’s own SKILL.md, which its author has not sent to the marketplace yet.

Prose, not code

These steps are written for a model to follow, not executed by a runtime. It can still be told to skip one, and it will say so when it does.

Servers it uses

None. This skill calls no MCP servers at all.

Everything it needs is in the instructions, so it works in a project with nothing connected — the model reads the file and changes how it works with what it can already reach.

It writes no files and reaches no network. All it changes is how the model reasons and writes.

What it asks for
Writes filesno
Network accessno

Read from the allowed-tools line of this skill’s own SKILL.md. A skill grants no permissions of its own — it can only ask for tools your client already has.

What it will not do

Every skill is narrow, and the useful ones say where they stop. These are the jobs this one is the wrong tool for.

What this skill is not for has not been published here. Nothing is implied by that: it is a section the author has not filled in.

What is in the bundle

1 file, 11.8 kB on disk. A bundle is text throughout: the instructions the model reads, plus the templates it fills in.

  • SKILL.md11.8 kB
What is not in it

No dependencies and nothing executable: a skill is text the agent reads, so the bundle is 1 file you can review in full before installing. The MIT licence covers the templates and examples as well as the instructions.

Install

Installing copies the bundle into your project. Nothing runs at install time — the files sit on disk until the model reads them.

$29 once
PyTorch Development Patterns · MIT · affaan-m
one-time
Price$29 once
LicenceMIT — the author’s, unchanged by this purchase
Paid throughStripe, once, on the card you add at the checkout
Keeps workingfor good — the files are yours once they are on disk
Updatesevery update its author ships, delivered through this account

You can read the whole bundle before paying — the SKILL.md above is the product, not a preview of it. What the money buys is the delivery: the folder packaged and handed to your machine by key, every update its author ships, and our support if it does not do what this listing says. The terms of use are MIT, set by the author and unchanged by buying it here.

Payment runs through Stripe, on a page like this one rather than a redirect. Once there is an account it joins the same mcprush invoice as everything else you run, so there is never a second card to enter.

Which clients pick it up on their own

A skill is a folder of text. A client with a skills folder reads it without being told; everywhere else the same text works, it is just handed to the model rather than found.

Claude Code.claude/skills/
Claude Desktop
ChatGPT
Cursor.cursor/skills/
VS Code.github/skills/
Codex CLI.agents/skills/
Gemini CLI.gemini/skills/
Grok.grok/skills/
Zed.agents/skills/
Windsurf.windsurf/skills/
Agent SDK.claude/skills/
HTTP / API
This release
Versionnot versioned
Publishedno release date on file
Price$29
Referenceaffaan-m/pytorch-development-patterns

Versions

Its author publishes no version number, so there is nothing here to pin to: what you install is the folder as it stands today. Instructions change more often than APIs do — a skill can be rewritten entirely without anything it depends on moving.

v
  • No earlier releases have been published to the marketplace.
Pinning

Nothing to pin to: this skill carries no version number of its own. What you install is what the folder holds on the day you install it.

Reviews

no reviews yet · no installs yet

Nobody has reviewed this skill yet. The rating is the mean of the reviews written here, so there is none until somebody writes the first.

Who can post

Only accounts that have had the skill installed for fourteen days, so a review is written after living with it rather than after reading it. Publishers may reply once.

Publisher
Servers0