RECEPTIVE FIELD

Ch 1 ▸ Why not a dense layer? ▸ Module 2/4

  1. 1Images are numbers
  2. 2Dense layers
  3. 3Counting parameters
  4. 4Lost neighbors

Dense layers.

🔒 This module is locked

It builds on Images are numbers. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.

Go to Images are numbers

A layer is one step of a neural network: it takes numbers in and computes new numbers from them. A network is a sequence of layers, each one reading what the layer before it produced.

A dense layer

A dense layer is a layer in which every output is computed from every input. It’s also called a fully connected layer, and PyTorch calls it nn.Linear. To compute one output, it multiplies each input by a number of its own, adds up all the products, and then adds one more number.

The numbers that multiply the inputs are the weights. Each output has one weight for every input. The number added at the end is the bias, and each output has one. Together, the weights and biases are the layer’s parameters: the numbers it learns. Training is the process that adjusts them, little by little, until the network gives good answers. Chapter 7 shows how training reaches the weights of a convolution.

Here’s one output with four inputs. Each input is paired with its own weight:

InputWeightInput × weight
0.210.2
0.9−1−0.9
0.521.0
0.100

Add up the last column and add the bias, 0.5:

(0.2×1)+(0.9×(−1))+(0.5×2)+(0.1×0)+0.5=0.2−0.9+1.0+0+0.5=0.8(0.2 \times 1) + (0.9 \times (-1)) + (0.5 \times 2) + (0.1 \times 0) + 0.5 = 0.2 - 0.9 + 1.0 + 0 + 0.5 = 0.8

The multiply-then-add part has a name. A dot product pairs up two lists of the same length, multiplies each pair, and adds up the results. So each output of a dense layer is the dot product of the inputs with that output’s weights, plus its bias.

More than one output

A layer usually has many outputs. Every output reads the same inputs, but each has its own weights and its own bias, so each computes something different. The outputs don’t read each other. They’re computed side by side, each from the inputs alone.

Add a second output to the example above, with four weights and a bias of its own. Each output has one weight per input, and one bias, so

parameters=(inputs×outputs)+biases\text{parameters} = (\text{inputs} \times \text{outputs}) + \text{biases}

where the number of biases equals the number of outputs. Here that’s (4×2)+2=10(4 \times 2) + 2 = 10.

What an output means depends on the job. In a network that sorts images into categories, called a classifier, the last layer has one output per category, or class. Each output is a score for its class, and the network’s answer is the class with the highest score. The scores aren’t related by any rule inside the layer. Training sets each output’s weights from labeled examples, so that the right class tends to score highest.

A dense layer on an image

Flatten the duck, and a dense layer can read it: 256 inputs. Each output then has 256 weights, one per pixel. That means you can lay an output’s weights back out in the image’s shape, as a 16×16 picture of what that output responds to.

The weights alone already decide what an output computes. If every weight is 1/256 and the bias is 0, the output adds up all 256 pixels and divides by 256: it’s the image’s average brightness. If every weight is 0 except one, which is 1, the output copies that one pixel through.

Figure 1.2 is a dense layer working as a classifier with three classes: a duck on a pond, an empty pond, and empty sky. It has one output per class.

FIG 1.2 ▸ A DENSE LAYER AS A CLASSIFIER · 256 IN, 3 OUT (256 × 3) + 3 = 771 PARAMETERS
INPUT 16×16FLATTEN256 INPUTSOUTPUT 0: DUCK ON A POND3.38BIAS −15.86◂ HIGHESTOUTPUT 1: EMPTY POND−5.70BIAS 8.27OUTPUT 2: EMPTY SKY−3.51BIAS 1.77INPUT×WEIGHTS · DUCK ON A POND=PRODUCTS→ADD ALL 256Σ = 19.24+ BIAS −15.86= 3.38▸ ANSWER: DUCK ON A POND. CLICK AN OUTPUT TO SEE ITS WEIGHTS.
Image
Pick an image, then click an output to see its weights laid back out as a 16×16 image. Red weights are positive and blue are negative. Each product is an input pixel times its weight; the score is their sum plus the bias.

The weights here are set by hand rather than trained, so you can see why they work. Each class’s weights are its picture minus the average of the three pictures. A score adds up pixel × weight, so a bright pixel under a positive weight pushes the score up, and a bright pixel under a negative weight pushes it down.

Across the top of the image, all three pictures show the same sky, so every class’s weights there are 0. Pixels that look the same in every class can’t help tell the classes apart.

The biases even things out. The pictures differ in overall brightness, and that would shift some scores up whatever the image showed. The empty pond is the darkest picture, so its output gets the largest bias, 8.27. The duck is the brightest, and its bias is −15.86.

Pick each image in turn. The duck scores 3.38 on its own class, against −3.51 for empty sky and −5.70 for empty pond. The empty pond scores 1.59 on its own class and 0.07 on empty sky; those two pictures differ only in the water. Click an output to see its weights, the products they make with the image, and how the sum and the bias give the score.

This layer has (256×3)+3=771(256 \times 3) + 3 = 771 parameters. That’s for three classes and an image smaller than an icon. The next module counts what happens at real sizes.

Knowledge check ▸ 5 questions

Before you move on.

Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.

  1. 1An output has weights 0.5, −1 and 2, and bias 1. Its inputs are 1, 2 and 3. What is the output?

  2. 2A dense layer has 4 inputs and 2 outputs. How many parameters does it have?

  3. 3A dense layer reads a flattened 16×16 image. How many weights does one of its outputs have?

  4. 4Every weight of an output is 1/256, its bias is 0, and it reads a flattened 16×16 image. What does it compute?

  5. 5A classifier’s last layer is a dense layer with one output per class. How does it give its answer?

0 of 5 answered correctly.

🔒 Next module: Counting parameters