Chrysolites Tech Academy

Deep Learning with PyTorch · Course book

Chapter 1: The PyTorch Workflow

Pairs with Lesson 1: PyTorch for Deep Learning

PyTorch is the deep learning framework most widely used in research and increasingly in industry, prized for feeling like ordinary Python. This chapter covers the foundations a long beginner's course builds: tensors, the end-to-end workflow of preparing data, building a model, training it, evaluating it and saving it, then classification with non-linear networks, computer vision with convolutional networks, and finally loading a custom dataset of your own images. Rather than memorising code, the aim is to understand the pattern that every PyTorch project repeats, so that a new problem means changing the data and the model while the training loop stays recognisably the same.

Tensors: the basic building block

Everything in PyTorch is a tensor, a grid of numbers with any number of dimensions. A single number is a scalar with zero dimensions; a list is a vector with one; a table is a matrix with two; a colour image is a tensor with three, for colour channels, height and width. Every tensor has three attributes worth checking constantly: its shape, its data type, such as 32-bit floating point, and its device, the processor or graphics card where it lives. Most errors beginners meet come from a mismatch in one of the three.

Tensors support arithmetic element by element, and the most important operation in deep learning, matrix multiplication. It has two rules. The inner dimensions must match, and the result takes the outer dimensions. A tensor of shape (3, 2) times one of shape (2, 4) gives (3, 4); (3, 2) times (3, 2) fails, but transposing the second to (2, 3) makes it work and gives (3, 3). Each value of the result is a sum of products: for rows (1, 2) and columns (5, 7), the entry is 1 × 5 + 2 × 7 = 19.

OperationWhat it doesExample
Reshape or viewSame values, new shape(2, 6) to (3, 4)
StackJoins tensors along a new dimensionThree (4,) tensors to (3, 4)
SqueezeRemoves dimensions of size 1(1, 5, 1) to (5,)
UnsqueezeAdds a dimension of size 1(5,) to (1, 5)
PermuteReorders dimensions(224, 224, 3) to (3, 224, 224)
AggregateMin, max, mean or sum over valuesMean of a float tensor

Random numbers are everywhere in deep learning, from starting weights to shuffling, so reproducibility needs a fixed random seed. Setting the seed before creating a random tensor gives exactly the same values every run. Tensors also convert easily to and from NumPy arrays, and moving one to a graphics card with .to(device) lets the same code run much faster where a GPU is available. Writing device-agnostic code, which picks the GPU if present and the processor otherwise, keeps notebooks portable.

The PyTorch workflow

A complete project follows the same stages: get the data ready, build or pick a model, fit it to the training data, make predictions and evaluate them, improve through experiments, and save and reload the trained model. The simplest way to see them is to create data where we already know the answer. Take y = 0.7x + 0.3 for 50 values of x from 0 to 0.98 in steps of 0.02. An 80/20 split gives 40 training points and 10 test points, with the test points covering x from 0.8 upwards.

The model is a class that inherits from nn.Module, creates two learnable parameters, a weight and a bias, and defines a forward method that computes weight × x + bias. With the random seed set to 42, the starting values are a weight of 0.3367 and a bias of 0.1288, far from the true 0.7 and 0.3.

test region true: y = 0.7x + 0.3 start: y = 0.3367x + 0.1288 00.51 00.51 x
Figure 1.1 — Known data and an untrained model. Training should lift the dashed line onto the solid one; the blue points are kept for testing.

On the test points the starting model is badly off. The gap at any x is (0.7 − 0.3367)x + (0.3 − 0.1288) = 0.3633x + 0.1712, and the average test x is 0.89, so the mean absolute error on the test set is 0.3633 × 0.89 + 0.1712 = 0.4945. Training needs a loss function, here mean absolute error, and an optimiser, here stochastic gradient descent with a learning rate of 0.01, which will update the parameters.

for epoch in range(epochs):
    model.train()
    y_pred = model(X_train)          # 1. forward pass
    loss = loss_fn(y_pred, y_train)  # 2. compute the loss
    optimizer.zero_grad()            # 3. clear old gradients
    loss.backward()                  # 4. backpropagation
    optimizer.step()                 # 5. update the parameters

    model.eval()
    with torch.inference_mode():
        test_loss = loss_fn(model(X_test), y_test)

These five lines are the heart of every PyTorch project. Clearing gradients matters because PyTorch adds new gradients to old ones by default. The evaluation block switches the model into evaluation mode and turns off gradient tracking, which is faster and avoids accidental training on the test data. Printing both losses every few epochs shows them falling together; when they level off, the learned weight and bias are close to 0.7 and 0.3. Finally, torch.save stores the model's state dictionary, its learned parameters, and loading it into a fresh instance of the same class restores the trained model.

Classification and non-linearity

Classification predicts a category. A useful toy problem places 1,000 points in two interleaved circles, one class inside the other. A model made only of linear layers can draw only straight boundaries, so however long it trains it stays near 50 percent accuracy, no better than guessing. Adding non-linear activations, such as ReLU between the linear layers, lets the network bend its boundary and separate the circles almost perfectly. This small experiment shows why every useful deep network contains non-linearities.

The outputs of a classifier's last layer are raw scores called logits. For two classes, a sigmoid turns a logit into a probability and rounding gives the label: a logit of 0 gives 0.5, and a logit of 2 gives about 0.88. For several classes, softmax turns the logits into probabilities that sum to 1, and the largest gives the label. PyTorch's binary loss that includes the sigmoid, BCEWithLogitsLoss, and the multi-class CrossEntropyLoss both expect logits, a frequent source of confusion.

Computer vision with convolutional networks

Image work in PyTorch relies on the companion library torchvision for datasets, transforms and pre-built models. A standard first dataset contains 28 × 28 greyscale images of clothing in 10 classes, with 60,000 for training and 10,000 for testing. A DataLoader wraps a dataset and serves it in shuffled mini-batches, commonly of 32; 60,000 images then make 1,875 batches per epoch, and the weights update after each.

A fully connected model that flattens each image into 784 numbers is a reasonable baseline. A convolutional neural network does better by keeping the image's grid shape and sliding small learned filters across it. A compact design in the style of the VGG family stacks blocks of two convolutional layers with ReLU followed by max pooling, then flattens and ends in a linear classifier. Tracking the shapes is the main skill: a 28 × 28 image through a 3 × 3 convolution with padding 1 keeps its 28 × 28 size, and a 2 × 2 max pool halves it to 14 × 14; after two blocks it is 7 × 7, so with 10 filters the classifier receives 10 × 7 × 7 = 490 values. A confusion matrix at the end reveals which classes, such as shirts and T-shirts, the model mixes up.

Custom datasets

Real projects use your own data. The course's main project classifies photos of three foods, pizza, steak and sushi, organised in the standard image classification layout: a train folder and a test folder, each containing one sub-folder per class. torchvision's ImageFolder reads this layout directly and takes class names from the folder names. Writing your own Dataset class instead means implementing two methods, one returning the number of samples and one returning the image and label at a given index, which gives full control over how data is loaded.

Transforms prepare each image: resizing to a fixed size such as 64 × 64, converting to a tensor and, during training, data augmentation such as random flips or the automatic augmentation policies torchvision provides. With only a few hundred images, small models overfit quickly, and the course compares experiments by plotting loss curves. A training loss far below the test loss means overfitting, while both being high means underfitting. The last step is predicting on custom data: loading a photo you took yourself, applying exactly the same transforms as the test set, adding a batch dimension, and passing it to the model to see the predicted class and its probability.

Check your understanding

  1. What shape results from multiplying tensors of shape (4, 3) and (3, 5)? Why does (4, 3) times (4, 3) fail, and how can a transpose fix it?
  2. List the five steps of the PyTorch training loop in order and explain why the gradients must be cleared each time.
  3. Using the starting weight 0.3367 and bias 0.1288, compute the model's prediction and its error at x = 0.9.
  4. Explain why a network made only of linear layers cannot separate two circles, and what change fixes it.
  5. A 64 × 64 colour image passes through two blocks, each with a padded 3 × 3 convolution and a 2 × 2 max pool. What is its height and width at the end?
  6. Describe the folder layout that ImageFolder expects for a three-class dataset.

Install Chrysolites Tech Academy

Android / Chrome / Edge: open the browser menu and choose Install app or Add to Home screen.

iPhone / iPad (Safari): tap the Share button, then Add to Home Screen.