Exercise: Convolutional Neural Networks - Pen & Paper

Introduction

Requirements

Knowledge

You should already be familiar with the backpropagation algorithm, as well as differential calculus. The following resources are recommend if this topic is new to you:

Theory

Definition

Given an image$ I $ with shape$ i \times j $, a kernel$ K $ with shape$ k_1 \times k_2 $ and a stride$ s $ with$ s \in \mathbb Z $ we define the multidimensional discrete convolution$ * $ as:

\begin{align} ( I K ) { i j } = \sum { m = 1 } ^ { k { 1 } } \sum { n = 1 } ^ { k { 2 } } K { m , n } \cdot I _ { s ( i - 1 ) + m , s ( j - 1 ) + n } \tag{1} \end{align*}

The following picture visualizes the whole process over all steps with an Image of size 3x3, a Kernel of size 2x2 and stride 1:

internet connection needed

Definition

The gradient with respect to a Kernel$ K $ is:

\begin{align} \frac { \partial ( I K ) } { \partial K { m ^ { \prime } , n ^ { \prime } } } = \sum { i = 1 } ^ { c { 1 } } \sum { j = 1 } ^ { c { 2 } } I { s ( i - 1 ) + m ^ { \prime } , s ( j - 1 ) + n ^ { \prime } } \tag{2} \end{align*}

Note:

Remember that the shape of an output$ O $ of a convolutional layer can be calculated by: `SINGLE c_1 = (i_1 + 2 p - k_1)/s + 1 SINGLE*SINGLE c_2 = (i_2 + 2 * p - k_2)/s + 1 SINGLE`

  • Output depth equals the number of kernels used in the convolution

Exercises

Convolution as a Sum

For the exercises, assume the following parameter: stride$ s=2 $, padding$ p=0 $

Tasks:

(a) You feed an image I with the dimensions 200x200x3 in the convolutional layer, which consists of 12323 kernels K each with the size 40x40x3. What dimesions of the output O do you expect?

(b) Calculate$ I * K $ with

\begin{align} I = \left[ \begin{matrix} 1 & 1 & -2 & 0 & 1 \ 1 & 0 & 0 & 2& 1 \ 0 & 1 & 0 & 5 & -1 \ -2 & 1 & 0 & -1& 1 \ 0 & 1 & 0 & 5 & -1 \end{matrix} \right], K = \left[ \begin{matrix} 0 & 1 & 1 \ 1 & 0 & 0 \ 0 & 1 & 0\end{matrix} \right] \end{align}

(c) Calculate the gradient$ \frac { \partial ( I * K ) }{ \partial K } $

Convolution as a Matrix Multiplication

If you do a naive implementation of the convolutional operation, you have nested loops. That is a bad idea, because it is always computationally expensive. A good solution to that problem is to vectorize the operation via matrix multiplication.

Tasks:

(a) How can you optimize the calculation of the convolutional operation to be more efficient? (Hint: Look up ’Convolution as matrix multiplication’ with a search engine of your choice.)

(b) Calculate$ I * K $ again like in (1.b) but with the method described.

(c) Calculate the gradient$ \frac { \partial ( L ) } { \partial K } $ using a matrix multiplication, where:

\begin{align} \frac { \partial ( L ) } { \partial K } = \frac { \partial ( L ) } { \partial (I K) } \frac { \partial ( I K ) } { \partial (K) } \end{align*}

Assume that the convolutional operation described is the last or only node of your computational graph. Note that the derivative of the matrix multiplication is:

\begin{align} \frac { \partial ( I \cdot \vec { k } ) } { \partial \vec { k } } = I ^ { T } \end{align}

(d) Calculate the max-pooling operation on image$ I $ with kernel-size of$ (3x3) $, stride$ s = 2 $ padding$ p = 0 $. (Hint: Pooling is the same as a convolution, but with a different kernel-function.)

Some Questions for a Better Understanding of the Whole Topic

Some questions to gain a better understanding of the whole topic.

(a) In the equations (1) and (2) we did not include a bias$ b $. How would they change if you decide to ad bias$ b $ to your convolutional layer?

(b) We always looked at 2d-examples. What does convolution look like if you write down a 3d-example (as a sum and matrix multiplication)? Create a toy example of your choice, e.g., an image with 5x5x2 and a kernel with 3x3x2 and calculate the convolution.

(c) What is the difference between cross-correlation$ \star $ and convolution$ * $.

Additional Notes

Some additional information at the end: For a modular implementation of a convolutional neural network, we need also the gradient with respect to the Image I. It can be expressed as:

Convolution as a sum \begin{align} \frac { \partial ( I K ) } { \partial I { m ^ { \prime } , n ^ { \prime } } } = \sum { i = 1 } \sum { j = 1 } K { m ^ { \prime } - s ( i - 1 ) , n ^ { \prime } - s ( j - 1 ) } \end{align*}

Convolution as matrix multiplication \begin{align} \frac { \partial ( I \cdot \vec { k } ) } { \partial I } = k ^ { T } \end{align}

Summary and Outlook

In this notebook you performed convolution operations on small example images by hand. It can be very helpful to walk through operations like im2col or vectorization by hand so you gain an intuition about how it works under the hood. Another benefit is that in the next notebook you're going to implement convolution as algorithms - You'll use the small toy examples to verify if your algorithms are working correctly.

Licenses

Notebook License (CC-BY-SA 4.0)

The following license applies to the complete notebook, including code cells. It does however not apply to any referenced external media (e.g., images).

Exercise: Convolutional Neural Networks - Pen & Paper
by Benjamin Voigt
is licensed under a Creative Commons Attribution-ShareAlike 4.0 International License.
Based on a work at https://gitlab.com/deep.TEACHING.

Code License (MIT)

The following license only applies to code cells of the notebook.

Copyright 2018 Benjamin Voigt

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.