PyTorch
Avoid common PyTorch mistakes — train/eval mode, gradient leaks, device mismatches, and checkpoint gotchas.
от clawic★ 23 звезды на GitHub
Установить PyTorch в Claude Code и Claude Desktop
Зарегайся, чтобы установить скилл
Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.
- Открой команду установки в одну строку
- Сохраняй скиллы в синхронизируемую библиотеку
- Уведомления, когда скиллы обновляются
Об этом скилле
Train vs Eval Mode
model.train()enables dropout, BatchNorm updates — default after initmodel.eval()disables dropout, uses running stats — MUST call for inference- Mode is sticky — train/eval persists until explicitly changed
model.eval()doesn't disable gradients — still needtorch.no_grad()
Gradient Control
torch.no_grad()for inference — reduces memory, speeds up computationloss.backward()accumulates gradients — calloptimizer.zero_grad()before backwardzero_grad()placement matters — before forward pass, not after backward.detach()to stop gradient flow — prevents memory leak in logging
Device Management
- Model AND data must be on same device —
model.to(device)andtensor.to(device) .cuda()vs.to('cuda')— both work,.to(device)more flexible- CUDA tensors can't convert to numpy directly —
.cpu().numpy()required torch.device('cuda' if torch.cuda.is_available() else 'cpu')— portable code
DataLoader
num_workers > 0uses multiprocessing — Windows needsif __name__ == '__main__':pin_memory=Truewith CUDA — faster transfer to GPU- Workers don't share state — random seeds differ per worker, set in
worker_init_fn - Large
num_workerscan cause memory issues — start with 2-4, increase if CPU-bound
Saving and Loading
torch.save(model.state_dict(), path)— recommended, saves only weights- Loading: create model first, then
model.load_state_dict(torch.load(path)) map_locationfor cross-device —torch.load(path, map_location='cpu')if saved on GPU- Saving whole model pickles code path — breaks if code changes
In-place Operations
- In-place ops end with
_—tensor.add_(1)vstensor.add(1) - In-place on leaf variable breaks autograd — error about modified leaf
- In-place on intermediate can corrupt gradient — avoid in computation graph
tensor.databypasses autograd — legacy, prefer.detach()for safety
Memory Management
- Accumulated tensors leak memory —
.detach()logged metrics torch.cuda.empty_cache()releases cached memory — but doesn't fix leaks- Delete references and call
gc.collect()— before empty_cache if needed with torch.no_grad():prevents graph storage — crucial for validation loop
Common Mistakes
- BatchNorm with
batch_size=1fails in train mode — use eval mode ortrack_running_stats=False - Loss function reduction default is 'mean' — may want 'sum' for gradient accumulation
cross_entropyexpects logits — not softmax output.item()to get Python scalar —.numpy()or[0]deprecated/error
Разрешённые инструменты
Инструменты, которые скиллу разрешено вызывать.
Без ограничений — скилл может использовать любой инструмент.
Вложенные файлы
- _meta.json
FAQ
Что делает скилл PyTorch?
Avoid common PyTorch mistakes — train/eval mode, gradient leaks, device mismatches, and checkpoint gotchas.
Как установить скилл PyTorch?
Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.
Скилл PyTorch запускает скрипты?
Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.
Похожие скиллы
theme-factory
AnthropicToolkit for styling artifacts with a theme. These artifacts can be slides, docs, reportings, HTML landing pages, etc. There are 10 pre-set themes with…
access
AnthropicManage Discord channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…
configure
AnthropicSet up the Discord channel — save the bot token and review access policy. Use when the user pastes a Discord bot token, asks to configure Discord, asks "how…
access
AnthropicManage iMessage channel access — approve pairings, edit allowlists, set DM/group policy. Use when the user asks to pair, approve someone, check who's allowed…