Pages

Topics

Tools

↑↓navigate↵openescclose

Python guides

Every published Python guide, newest first, with practical examples you can copy into your own project.

3,965 guides across 14 topics

Topics

Latest Python guides

Diagram showing dropout, batch normalization, and learning rate scheduler components in a PyTorch neural network training pipeline.
Python

Combining Dropout, Batch Normalization, and Learning Rate Schedulers in PyTorch

Learn how to combine dropout, batch normalization, and learning rate schedulers in PyTorch, including layer placement, train/eval modes, scheduler timing, and common pitfalls.

PyTorchDropoutBatch NormalizationLearning Rate Schedulers
Diagram showing a PyTorch model in eval mode with gradient tracking disabled, comparing no_grad and inference_mode contexts.
Python

PyTorch Inference: model.eval() vs torch.no_grad() vs torch.inference_mode()

Learn how model.eval(), torch.no_grad(), and torch.inference_mode() affect PyTorch inference, when to combine them, and which context to choose.

PyTorchinferenceautogradmodel evaluation
Illustration of a PyTorch model state_dict being saved and loaded as a checkpoint file
Python

How to Save and Load PyTorch state_dict and Checkpoints

Learn how to save and load PyTorch models with state_dict and full checkpoints, including optimizer state, epoch, and safe device handling.

PyTorchstate_dictcheckpointsmodel persistence
Diagram of a PyTorch training and validation loop showing forward pass, backward pass, and evaluation phases on a neural network
Python

PyTorch Training and Validation Loops in Python

Learn how to structure PyTorch training and validation loops, handle device placement, manage gradients, and avoid common pitfalls.

PyTorchdeep learningmodel trainingbackpropagation
Illustration of a custom PyTorch Dataset feeding samples into a DataLoader that produces training batches.
Python

PyTorch Dataset and DataLoader for Custom Datasets

Implement custom PyTorch datasets with __getitem__ and __len__, then use DataLoader for batching, shuffling, and parallel loading.

PyTorchDatasetDataLoaderCustom Dataset
Diagram of a PyTorch training loop connecting a loss function to Adam and SGD optimizers with gradient flow
Python

PyTorch Loss Functions and Optimizers: Adam vs SGD

python pytorch loss functions optimizers adam and sgd: How to pair loss functions with optimizers in PyTorch, and how Adam and SGD differ in update behavior, memory us...

PyTorchloss functionsoptimizersAdam
A visual representation of PyTorch neural network layers and activation functions, showing connected nodes and a ReLU curve.
Python

PyTorch torch.nn Layers and Activation Functions: A Practical Guide

Build PyTorch neural networks with torch.nn layers and activation functions. Covers nn.Linear, nn.Conv2d, nn.Sequential, custom nn.Module models, initialization, and the functional API.

PyTorchNeural Networksnn.ModuleActivation Functions
Diagram of a PyTorch computational graph showing gradient flow backward from a loss node to parameter nodes.
Python

Python PyTorch Autograd: requires_grad and backward

Understand how PyTorch's autograd engine uses requires_grad and backward() to compute gradients, including graph construction, detach(), no_grad(), and common pitfalls.

PyTorchautogradrequires_gradbackward
Illustration of a tensor moving from a CPU chip to a GPU chip in PyTorch with a CUDA device indicator.
Python

Managing CUDA Devices and GPU Tensors in PyTorch

Learn how PyTorch manages CUDA devices and GPU tensors: check availability, move tensors with .to(), write device-agnostic code, handle multiple GPUs, and avoid device mismatch errors.

PyTorchCUDAGPUTensor
A visual representation of a PyTorch tensor being converted to a NumPy array with indexing and slicing operations shown as connecting blocks.
Python

PyTorch Tensor Creation, Indexing, and NumPy Conversion

Learn how to create PyTorch tensors, index and slice them effectively, and convert between tensors and NumPy arrays while avoiding common memory-sharing pitfalls.

PyTorchTensorNumPyMachine Learning