pytorch

PyTorch

Specific best practices and architectural patterns when working with PyTorch.

Details

Language / Topic
pythonPython
Category
framework

Rules

balanced

PyTorch

- Always use `torch.no_grad()` during inference to save memory. Move tensors to the same device explicitly — `tensor.to(device)`. Use `DataLoader` with `num_workers>0` and `pin_memory=True` for GPU training.

PyTorch

- Always use `torch.no_grad()` during inference to save memory. Move tensors to the same device explicitly — `tensor.to(device)`. Use `DataLoader` with `num_workers>0` and `pin_memory=True` for GPU training.
- Use `nn.Module` subclasses for all models — implement `forward()` only, never `__call__`. Use `torch.amp.autocast` and `GradScaler` for mixed-precision training. Save model state dicts (`model.state_dict()`), not entire models. Set `model.eval()` before inference and `model.train()` before training. Use `torch.utils.data.Dataset` for custom data — implement `__len__` and `__getitem__`.