MathalyseResearch

Learn AI · Chapter 9 · Convolutional Networks

The Convolution Operation

Compute a convolution by sliding a kernel along an input, in one and two dimensions, see what the kernel flip does, and write the same operation as a sparse matrix.

Book §9.1, pages 331–334Equations 9.1–9.6About 35 minutes
Section 1

A smoothing filter for a noisy sensor

x input data w kernel s feature map λ decay rate σ noise
Habit 1

Click the colored symbols. Each one explains itself and lights up the same symbol in the other equations.

Habit 2

Move the controls before you read on. Each demo is built so that the sentence below it describes something you have just seen.

Picture a laser sensor that tracks a spaceship. It reports the position at every instant, but every reading carries some noise. The fix is to average several readings, giving recent readings more weight than old ones. Applying that weighted average at every moment gives a smooth estimate of the position:

(9.1)
Click any coloured symbol to see what it means.

Read the equation as a recipe. For each moment , take every past reading , multiply it by a weight that depends only on how old that reading is, and add up the results. The integral sign means the sum runs over all ages .

  • Convolution is the name of this operation. Equation 9.2 writes it with an asterisk.
  • Input: the signal , here the sensor readings.
  • Kernel: the weighting function . Its argument is the age, .
  • Feature map: the output , a smoothed version of the input.
(9.2)
Click any coloured symbol to see what it means.

A running class grade works the same way. If the last test counts double, the grade is a weighted average in which recent scores carry more weight. The laser estimate applies the same rule to a physical signal, at every moment.

The book sets two conditions for this example. The weights must form a probability density, so they add up to 1, or the output is not a weighted average. The weights must also be zero for negative ages, or the estimate would use readings from the future. Convolution itself is defined for any pair of functions for which the integral exists, and it is used for many purposes besides averaging.

Smoothing the laser track

true tracknoisy readingsestimatekernel w(a)

The estimate trails the true track. A reading that is seconds old counts with weight that falls like . Its average age is seconds, and a track moving at speed is therefore estimated units behind. This derivation is added here and is not printed in the book. The exponential shape of the weights is a choice made for this demo.

A larger gives a shorter memory. The estimate reacts faster, but it averages fewer readings, so more noise gets through. Try the decay rate at 0.2 and at 4 and compare the two estimates by eye.

Challenge +8 XP

Set the decay rate so that the average age of a reading is exactly 1 second.

Note

The demo uses a finite grid of readings to approximate the integral. The two end readings of the history count half, which is the trapezoid rule, and the history runs 60 seconds. For a noise-free track the gap matches to within about 0.002 for decay rates from 0.2 to 4 and speeds up to 1.

Section 2

Discrete convolution as a sum

A sensor that reports once per second has whole-number times, so and take integer values. The integral of equation 9.1 becomes the sum of equation 9.3. This is the form that a computer uses.

(9.3)
Click any coloured symbol to see what it means.

In a computer the arrays are finite, so the sum has finitely many non-zero terms. The kernel is zero outside its stored entries. Each output value is one sum of products, and each product is a reading times a kernel entry. The kernel index runs backwards as the reading index runs forwards.

Add up the products

Readings (six values)
Kernel (three values)

With the default numbers, the output at adds two products. Move to 3 to see three products add up. The kernel length sets how many products can appear in one sum: at most three here. Guess the output at a time you choose, press the check button, and compare with the readout. The full output has 8 values, which is 6 plus 3 minus 1.

Challenge +8 XP

Find an output time at which all three kernel entries contribute, and the output is at least 2.

Check

With = (0, 1, 3, 2, 0, −1) and = (0.5, 0.3, 0.2), the output at is .

Section 3

Two dimensions and the kernel flip

An image is a grid, so the input becomes a two-dimensional array and the kernel becomes a small grid . The book calls arrays of this kind tensors. The two-dimensional sum is equation 9.4. Its output is the feature map at row and column .

(9.4)
Click any coloured symbol to see what it means.

Look at the kernel index . As the input row grows, the kernel row shrinks. The kernel is read backwards. This reversal is the flip, and it is what makes the operation commutative: equation 9.5 gives the same result with the roles of input and kernel exchanged.

(9.5)
Click any coloured symbol to see what it means.

The book notes that equation 9.5 is the easier form to implement, because the range of valid index values varies less. The commutative property is useful in proofs. In a neural network implementation it is rarely important, and convolution is usually combined with other functions that do not commute with it. The book states that the only reason to flip the kernel is to obtain this commutative property.

Most machine learning libraries skip the flip and compute equation 9.6, called cross-correlation. This text calls both operations convolution, and states which one it means when the flip matters. A learning algorithm finds the right kernel values in any case, so a kernel learned with the flip is stored upside down relative to one learned without it.

(9.6)
Click any coloured symbol to see what it means.

Figure 9.1 shows the operation without the flip. The input is a 3 by 4 grid with letters a to l, row by row. The kernel is 2 by 2, with the entries , , and . The figure keeps only the positions where the kernel lies entirely inside the input, the valid positions, so the output is 2 by 3. The figure reuses the letters , , and for kernel entries, so here x means a kernel entry and not the signal of Section 1.

Figure 9.1, with numbers

Choose an output value to see its sum. With the flip off, the upper-left output is , the first formula of Figure 9.1. With the flip on, each input letter meets the kernel entry in the opposite corner, so the same letters appear in a different pairing.

Challenge +8 XP

Set the kernel so that a half turn leaves it unchanged: the top left equals the bottom right, and the top right equals the bottom left. Keep the kernel not all zero. Then the flip changes no output.

Note

The output size follows from the valid-position rule: the input height minus the kernel height plus 1, and the same for the width. This count is added here and is not printed in the book.

Section 4

Convolution as a sparse matrix

Discrete convolution can also be written as a matrix product. Stack the input as a column , and place the kernel in each row of a matrix at a different offset. The output column is then . The example below uses three kernel entries and six inputs, with the kernel in the order written. In that order the product is the cross-correlation of equation 9.6. Reversing the kernel gives the convolution of equation 9.4, which the demo further down can switch on. The example and this remark are added here and are not printed in the book.

Click any coloured symbol to see what it means.

The matrix has a regular pattern. Each row is the row above shifted one place to the right, so every diagonal holds one repeated value. A matrix with this property is called a Toeplitz matrix. The book states this for univariate convolution.

In two dimensions the book describes a doubly block circulant matrix. A circulant matrix wraps around at its edges, so that description fits convolution of a wrapped input. The valid convolution of Figure 9.1 does not wrap, so its matrix is only block Toeplitz. This remark is added here and is not printed in the book.

Most entries of the matrix are zero, because the kernel is much smaller than the input. Each row has non-zero entries, and there are rows. So the fraction of zeros is . This count is added here and is not printed in the book.

Any algorithm that works with matrix multiplication and does not depend on special matrix structure also works with convolution, without change. Practical convolutional networks use faster methods, but these are not required in theory.

Build the matrix

Change the input length and the kernel length. The kernel cannot be longer than the input, so its slider stops at the input length. Each row has the same number of non-zero entries, and those entries sit on a diagonal band. Turn the flip on and check that the matrix still reproduces the sliding sum.

Challenge +8 XP

Reach a matrix in which at least 70% of the entries are zero.

  • Convolution slides a kernel along an input and adds the products. In one dimension this is equation 9.3, and in two dimensions it is equation 9.4.
  • The flip reverses the kernel. Most libraries drop it and call the result convolution (eq. 9.6), and the learned kernel adapts either way.
  • Convolution is a matrix product with a Toeplitz structure. For valid convolution the fraction of zero entries is 1 minus k over N, so the matrix is sparse when the kernel is small.
Final quiz

Check yourself

Questions that mix the whole module. Each one is scored, and the score feeds your mastery of this module.

Source: Ian Goodfellow, Yoshua Bengio and Aaron Courville, Deep Learning, MIT Press, 2016, Section 9.1 (pages 331–334), free to read at deeplearningbook.org. Where the book prints equation numbers, the numbers here follow the book. The order of ideas follows the book. The explanations and all demos were written for this page.