Gradient LabSign up free

Cheat sheets › Chapter 8

Neural networks cheat sheet

Stack simple units into networks that learn almost anything. Every lesson in one page: the key idea, the pseudocode and the terms.

The perceptron

A perceptron is the simplest artificial neuron. It takes a few inputs, here three yes/no facts about a concert, and produces one decision: go or stay home.

inputs  = [weather_is_good, friend_is_going, ticket_is_cheap]
weights = [0.6, 0.9, 0.2]
total = sum(input × weight) + bias

if total > 0.5:
    output 1    # go to the concert
else:
    output 0    # stay home

Terms: perceptron, neuron, weight

Activation functions

An activation function adds a bend. The most popular, relu, is wonderfully simple: negative numbers become 0, positive numbers pass through unchanged.

relu(x)    = max(0, x)          # off below 0, straight line above
sigmoid(x) = 1 / (1 + e^(−x))   # smooth S from 0 to 1
tanh(x)    = an S from −1 to 1

layer_output = activation(W × input + bias)
# without activations, stacked layers collapse into one straight line

Terms: activation function, relu

Multilayer networks

Put neurons side by side to make a layer, then stack layers. Every neuron in one layer connects to every neuron in the next. This is a multilayer network.

hidden1 = relu(W1 × input + b1)
hidden2 = relu(W2 × hidden1 + b2)
output  = sigmoid(W3 × hidden2 + b3)

# each hidden layer builds new features out of the last one

Terms: hidden layer

The forward pass

A forward pass is how a network makes a prediction. It starts with the input: here, two numbers describing a photo.

x = [0.8, 0.2]                  # input
h = relu(W1 × x + b1)           # layer 1 → [0.9, 0.0, 0.4]
y = sigmoid(W2 × h + b2)        # layer 2 → 0.76
prediction = y                  # a 76% chance it's a cat

Terms: forward pass

Backpropagation

Run a forward pass, then compare the prediction with the truth. The network said 90% cat, but the photo was a dog. That gap is the error.

prediction = forward(x)
error = loss(prediction, truth)
for each layer, from the last to the first:
    blame = how much each weight added to the error     # chain rule
    weight = weight − learning_rate × blame
repeat for many, many examples

Terms: backpropagation

Cross-entropy loss

Classifiers output probabilities. To train them we need a loss that judges how much probability went to the right answer. The standard choice is cross-entropy.

p = the model's probability for the correct answer
loss = −log(p)

p = 0.9   → loss 0.11    # confident and right: tiny loss
p = 0.5   → loss 0.69
p = 0.01  → loss 4.6     # confident and wrong: huge loss

Terms: cross-entropy

Optimizers: SGD, momentum and Adam

With momentum, each step keeps some of the previous one, like a heavy ball rolling downhill. The side-to-side bounces cancel out and the forward motion builds up.

# plain SGD: step = −lr × gradient
w = w − lr × gradient

# momentum: keep some of the last step, like a rolling ball
velocity = 0.9 × velocity − lr × gradient
w = w + velocity

# Adam: momentum + its own step size for every weight

Terms: momentum, adam

Weight initialization

Before training, every weight needs a starting value. All zeros won’t work: every neuron would compute the same thing and learn the same thing. So we start with small random numbers. But how small?

# too small: signals shrink layer after layer → nothing learns
# too big:   signals explode → numbers overflow
# just right: scale random weights to the layer size
w = random_normal() × sqrt(2 / inputs_per_neuron)    # “He” initialization

Terms: initialization

Dropout

With dropout, every training step switches off a random handful of neurons (greyed out). The rest must cope without them.

during training, for each step:
    switch off a random 20% of the neurons
    train with the rest
when predicting:
    use every neuron (scaled to match)
# no neuron can rely on one particular partner

Terms: dropout

Batch normalization

batch normalization measures the mean and spread of each layer’s outputs for the current batch, then shifts and squeezes them to mean 0 and spread 1, just like standardizing features.

for each batch of examples, at one layer:
    mean = average of the activations
    std  = spread of the activations
    normalized = (activation − mean) / std
    output = γ × normalized + β      # learnable re-scale and shift

Terms: batch normalization

Revise with flashcards and quizzes. Free practice for every lesson in this chapter.

Open in the app