What deep learning is

PyTorch · Essentials · 10 min

Every PyTorch lesson talks about models, weights, losses and gradients; this one gives those words a meaning before you meet the code.

Suppose you want a program that says whether a photo shows a cat. Writing the rules by hand ("if there are pointy ears...") never works well. Machine learning takes the opposite approach: you collect many examples, each paired with its correct answer (its label), and let the computer find the pattern itself. The examples it learns from are the training data; any such collection of examples is a dataset.

What the computer builds is a model: a formula with many adjustable numbers inside. Feed in a photo and the formula outputs a guess. The adjustable numbers are the model's weights (PyTorch calls them parameters). They start out random, and everything the model "knows" ends up stored in them.

A neural network is a model built as a chain of simple steps called layers. Each layer is a small calculation with its own weights, and it passes its numbers on to the next layer. Deep learning is machine learning with neural networks that have many layers; it is behind today's image, speech and language software.

Training is how the weights go from random to useful. Take a batch, a group of examples processed together (say 32 photos). The model guesses, and a loss function scores how wrong the guesses are as a single number, the loss; lower is better. Then the computer works out, for every weight, which direction a tiny nudge would lower the loss. That set of directions is the gradient. Every weight is moved a small step that way, and the cycle repeats, often millions of times. How big each step is is the learning rate.

All of this is a huge amount of arithmetic, mostly multiplying large grids of numbers. The computer's main processor, the CPU, does one calculation at a time, very fast. A GPU (the graphics chip) does thousands at once, so training runs on a GPU whenever one is available, often 10 to 100 times faster.

Once trained, using the model to make predictions on new data is called inference. PyTorch covers the whole path: tensors (grids of numbers) to hold the data, automatic gradients, ready-made layers, and tools for training on GPUs. You need no maths beyond this picture to follow the next lessons.

Example

# Training in miniature, with no PyTorch: learn w so that w * x matches y.
xs = [1.0, 2.0, 3.0]
ys = [2.0, 4.0, 6.0]          # the "labels": the correct answers (here y = 2x)

w = 0.0                       # the model's single weight, starting at a bad guess
lr = 0.05                     # learning rate: how big a step to take

for step in range(20):
    guesses = [w * x for x in xs]
    # loss: average squared error, one number that says how wrong w is
    loss = sum((g - y) ** 2 for g, y in zip(guesses, ys)) / len(xs)
    # gradient: how the loss changes when w grows a little (worked out by calculus)
    grad = sum(2 * (g - y) * x for g, y, x in zip(guesses, ys, xs)) / len(xs)
    w = w - lr * grad         # step against the gradient to lower the loss
    if step in (0, 1, 19):
        print(step, round(loss, 3), round(w, 3))
# 0 18.667 0.933
# 1 5.31 1.431
# 19 0.0 2.0        <- w has learned the pattern y = 2x

# PyTorch does the gradient part automatically, for millions of weights, on a GPU.

Remember

  • A model is a formula with adjustable numbers (weights); training sets them from labelled examples
  • The loss scores how wrong the model is; the gradient says which way to nudge each weight
  • GPUs do the heavy arithmetic; inference is using the trained model on new data

Watch out for

  • Expecting to write the rules yourself: in machine learning you supply examples and a loss, and the weights are found by training.

Check yourself

  1. During training, what does the gradient tell you?
  2. A friend says a trained model's knowledge is stored in its weights. Is that right?

Sign in to answer, see the explanations, and get this lesson back as a quick review later.

Official docs: pytorch.org/tutorials/beginner/basics/intro.html