Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

What the layers of a neural network do

After this lesson: I can describe what the layers of a neural network do.

50 min lesson2 commandsJupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

A network is a stack of simple sums

A neural network is a program built from layers of small units called neurons, which a deep neural network stacks several layers high. Knowing what each layer does turns the network from a black box into sums you can follow with a pencil.

A hire shop can feed in tomorrow's temperature, rain and day of the week. The network gives back one number: busy or quiet.

By the end of this lesson

Note

I can describe what the layers of a neural network do.

Key words

WordWhat it meansExample
Neurona small unit that turns several numbers into one
Layera row of neurons that work side by side
Weighthow much one input counts3
Biasa number added to shift the total-1
Activation functiona rule that bends a neuron's totalReLU
Deephaving two or more hidden layers

The cells students fill in are left blank in this preview.

A neuron adds weighted inputs, then decides

A neuron takes several numbers in and gives one number out. It multiplies each input by its weight and adds the results. Then it adds its bias.

A big positive weight means that input pushes the total up. A negative weight pushes it down. A weight of 0 means the input is ignored.

Worked example: one neuron on one day

InputValueWeightProduct
Temperature in tens of degrees236
Rain in cm1-4-4
Weekend122
Biasnonenone-1
Totalnonenone3

The cells students fill in are left blank in this preview.

Change the rain and run it

Code: python
# Inputs: temperature in tens of degrees, rain in cm, weekend (1 or 0).
inputs = [2, 1, 1]
weights = [3, -4, 2]
bias = -1

total = bias
for n in range(3):
    total = total + weights[n] * inputs[n]
print("total:", total)
# Change the rain from 1 to 0 and press Run.
# The total should rise. By how much?

Layers stack neurons into a network

Neurons sit in layers. The input layer holds the raw numbers and does no sums. A hidden layer is a row of neurons between the input and the answer. The output layer gives the answer.

Every neuron reads every number from the layer before. A network with two or more hidden layers is called deep.

Every connection has a weight to learn

Each line in a diagram is a weight. Each neuron also has one bias. These numbers are the network's parameters, and training is the job of setting them.

A layer with 3 inputs and 4 neurons has 12 weights and 4 biases. The table counts a whole network.

Worked example: counting a small network

LayerInputs inNeuronsWeightsBiases
Hidden layer 134124
Hidden layer 244164
Output layer4141
All layersnonenone329

The cells students fill in are left blank in this preview.

Count the parameters with Python

Command
docker exec jupyter python3 -c "print(sum(a * b + b for a, b in zip([3, 4, 4], [4, 4, 1])))"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

An activation function bends the total

If every neuron only added, a whole network could be squashed into one neuron. An activation function is a rule applied to each neuron's total. It is what makes extra layers worth having.

Step gives 1 for a positive total, else 0. ReLU gives the total if it is positive, else 0. A sigmoid gives a smooth S-shaped number between 0 and 1.

Worked example: three rules on five totals

TotalStepReLUHard sigmoid
-3000
-1000.25
0000.5
1110.75
3131

The cells students fill in are left blank in this preview.

Worked example: two plain layers equal one

InputLayer 1 (times 2)Layer 2 (times 3)One layer (times 6)
1266
241212
361818

The cells students fill in are left blank in this preview.

ReLU on five totals

Command
docker exec jupyter python3 -c "print([max(0, x) for x in [-3, -1, 0, 1, 3]])"

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people think a deep network understands its data more deeply. Deep only counts hidden layers. More layers let it combine simple patterns into richer ones, but it can still be wrong. Judge a network by how it does on rows it has never seen.

Going further

In a notebook cell, build MLPClassifier(hidden_layer_sizes=(4, 3), max_iter=2000) from sklearn.neural_network and fit it on any three-column table of numbers with a 0 or 1 label. Then print [w.shape for w in net.coefs_].

Predict the three shapes before you run it, using the counting table in this lesson.

About this lab and its source: JupyterLab data science lab: pandas, Matplotlib and scikit-learn in the browser

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.