Models, training and Keras

Keras · Essentials · 8 min

Every Keras function talks about models, layers, training, losses and gradients; this lesson gives you those words before you need them.

Suppose you want a program that reads a photo and says which digit, 0 to 9, it shows. Writing the rules by hand is hopeless. Machine learning takes the other route: you collect many examples with the right answers attached (the training data) and let the computer find the pattern itself.

What it finds is a model: a fixed formula with many adjustable numbers inside. Those numbers are the model's weights. Feed in an input (the pixels), the model does arithmetic with its weights, and out comes a guess. With random weights the guesses are nonsense; with good weights they are right.

Training is the search for good weights. It repeats one loop:

  • Run a few examples through the model and look at its guesses.
  • Score how wrong they are with a loss function: one number that is zero for perfect guesses and grows the worse they get.
  • Work out, for every weight, which way to nudge it to make the loss smaller. That set of directions is the gradient.
  • Nudge every weight a small step that way, and repeat until the loss stops falling.

The kind of model Keras builds is a neural network: a stack of layers. Each layer takes numbers from the one before, combines them using its own weights, and passes new numbers on. Many simple layers stacked together can recognise digits, faces or spoken words.

Inside, the numbers travel as tensors: grids of numbers with any number of dimensions, like NumPy arrays. A batch (a group of examples processed together) of 32 small greyscale images is a tensor of shape (32, 28, 28).

Keras gives you the layers ready-made, works out gradients for you, and runs the whole loop with one call, fit(). It runs fast on a GPU (a graphics chip that does thousands of multiplications at once) when you have one. You do not need the maths to start, only these words: model, weights, layer, loss, gradient, training.

Example

# A toy "model" with one weight w: guess = w * x.   The truth is y = 3 * x.
w = 0.0
examples = [(1, 3), (2, 6), (3, 9)]            # (input, correct answer)

def loss(w):                                    # 0 when every guess is perfect
    return sum((w * x - y) ** 2 for x, y in examples)

for step in range(20):
    # gradient: how much the loss rises if w grows a tiny bit
    grad = (loss(w + 0.001) - loss(w)) / 0.001
    w -= 0.01 * grad                            # step the other way, lowering the loss

print(round(w, 2), round(loss(w), 4))           # 3.0 0.0003  -> it learned y = 3x
# Keras does the same for millions of weights, computing gradients exactly.

Remember

  • A model is a formula with adjustable weights; training finds good values for them
  • The loss measures how wrong the model is; the gradient says which way to nudge each weight
  • A neural network is a stack of layers; Keras provides them and runs training with fit()

Watch out for

  • Judging a model only by how well it does on its own training data: a low loss there can mean it memorised the examples rather than learned the pattern.

Check yourself

  1. What changes while a model trains?
  2. What does a loss function return?

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

Official docs: keras.io/getting_started/intro_to_keras_for_engineers/