PyTorch · start here

Build your first image model.

Start with the three things that make PyTorch click: tensor shapes, one complete training loop, and how a CNN reads an image. This page is the short version; the detailed reference shows the why and the runnable code.

tensor.inspect
shape [32, 3, 224, 224]
dtype torch.float32
device cuda:0
requires_grad false
01 / START HERE

The five steps every model repeats.

A model learns by seeing a batch, making logits, measuring error, calculating gradients, and updating its weights. Select a stage to see its job in plain language.

02 / CORE CONCEPTS

What to understand before changing code.

You do not need to memorize every PyTorch function. You do need to recognise what enters a model, what leaves it, where learning happens, and how to check whether it worked.

01 · INPUT

Tensors and shapes

For images, read [batch, channels, height, width]. Shape, dtype, and device are part of the program.

Start with tensors ↗
02 · DATA

Samples become batches

Dataset returns one prepared example. DataLoader groups examples into batches and shuffles training order.

See the data path ↗
03 · MODEL

Layers create logits

nn.Module turns a batch into raw class scores. Activations make a network capable of learning non-linear patterns.

Models, logits, and loss ↗
04 · LEARNING

Loss changes weights

Clear gradients, predict, measure the loss, calculate gradients, then update the parameters.

Read the training loop ↗
05 · CHECK

Evaluation is different

Use model.eval() and torch.no_grad() before measuring accuracy or inspecting errors.

Evaluate correctly ↗
06 · IMAGES

CNNs learn local patterns

Convolutions create feature maps. Pooling reduces spatial size. Validation curves reveal whether those features generalize.

See CNNs visually ↗
Conceptual diagram from image file through transform and dataset to a DataLoader batch

Image data path. The model receives a batch tensor, not an image file.

Conceptual convolution showing a vertical-edge filter and its feature map

Convolution. A small filter creates a feature map; a CNN learns useful filters from data.

Conceptual CNN path showing image batch shape after convolution, pooling, and classification

CNN shapes. Channels can grow while pooling reduces height and width.

Measured regression comparison where a linear model misses a curved pattern and a nonlinear network follows it

Why activations matter. This retained result shows a line cannot fit every pattern.

training_loop.py
for images, labels in train_loader:
    images, labels = images.to(device), labels.to(device)

    optimizer.zero_grad()       # clear old gradients
    logits = model(images)         # forward pass
    loss = loss_fn(logits, labels) # measure error
    loss.backward()              # calculate gradients
    optimizer.step()            # update parameters
03 / VISUAL TOOLS

Change an input and see what changes.

These small tools make shapes, batches, broadcasting, and generalization visible without running a model or storing anything in your browser.

Batch calculator

CNN shape tracer

Broadcasting checker

Generalization curves

04 / RUN EXAMPLES

See each idea in a complete script.

Each example downloads public data when needed, trains a real model, and writes its own predictions, curves, metrics, and checkpoint. Start small on CPU; use the longer GPU mode when you want it.

Regression

Linear vs nonlinear features

A deterministic curved function exposes what a linear model cannot represent and why activation functions change capacity.

TensorMSEAutogradAdam
Open project ↗
Handwriting

EMNIST letter classifier

Downloads real letters, trains a CNN, and saves predictions, a confusion matrix, curves, metrics, and a checkpoint—small on CPU, longer on GPU.

EMNISTDownloadTrainArtifacts
Open project ↗
Data quality

Robust image pipeline

Downloads public images, builds a checked folder dataset, records a corrupt file, then trains only on valid samples and saves diagnostics.

FoldersValidationTrainReport
Open project ↗
Computer vision

Nature CNN and overfitting

Downloads selected CIFAR-100 classes, trains a regularized CNN, and saves predictions, curves, a confusion matrix, metrics, and a checkpoint.

CIFAR-100DownloadCNNArtifacts
Open project ↗

Detailed reference

Need the next explanation?

Continue through two connected guides, jump to a specific section, or copy a complete maintained project script.

Open the detailed Fundamentals guide ↗