- Revolutionizing Healthcare With Artificial Intelligence
- Epe 201301
- 00212931
- AVOIDING DUPLICATED COMPUTATION TO IMPROVE THE PERFORMANCE OF PFSP ON CUDA GPUS
- MIT6 087IAP10 Project
- Optimal Control Problem and Power-Efficient Medical Image Processing Using Puma
- Gate
- Vertex Textures
- A Novel Architecture Using NVIDIA CUDA to Speed Up Simulation of Multi-Path Fast Fading Channels
- 00 - 3rd Workshop AO V1.2
- GPU Primer
- chap15
- DE
- Artificial Neural Networks Architecture Applications
- rev_m580_C
- ANFIS
- Continuity and Differ en Ti Ability
- Ad Maths Formula List
- Review 3
- Changing Concept of Change
- LECTURE 3 Second Week Functions of Several Vareables S2 2015-2016
- Calculus for Business
- Approach to Increase Accuracy of Multimodal Biometric System for Feature Level Fusion
- lec14
- Harris Dissertation
- Human Activity Recognition & Neural Networks by Matt Robinson
- Differentiation
- Mathematical Modeling of Kluyveromyces Marxianus Growth in Solid-state Fermentation Using a Packed-bed Bioreactor
- 4_R
- Multiple-Arc Optimization Problems
- LAB_10_WordShortcuts.pdf
- ETHZ_Lecture5
- US NEWS.xls
- 2018_DATE_SS
- ABC
- Quran.txt
- Berlin Station - 02x05 - Right of Way.tbs.English.hi.C.updated.addic7ed.com
- CS231n Convolutional Neural Networks for Visual Recognition
- Anx -A
- Internet
- Counterpart - 01x03 - The Lost Art of Diplomacy.amzn.WEBRip.sigmA.english.hi.C.updated.addic7ed.com - Copy
- Ielts Application for the Issue of Additional Trfs
- Ispd 16 Program
- LICENSE.txt
- The.snowman.2017.1080p.bluray.x264 [YTS.ag]
- DAC List of ODA Recipients 2014 final.pdf
- Eco 130 (Lec 29-30)
- Assignment 3
- US NEWS
- Cvpr2016
- Physical Design
- MIT_department_info.pdf
- October 16
- readme.txt
- IELTSInformationForCandidates.pdf
- Bad Career 7
- Lif Neuron
- ELG6163 Implementation of FIR
- Xilinx Windows 10 Fix

Convolutional Neural Networks for MATLAB

Andrea Vedaldi Karel Lenc Ankush Gupta

i

ii

Abstract

**MatConvNet is an implementation of Convolutional Neural Networks (CNNs)
**

for MATLAB. The toolbox is designed with an emphasis on simplicity and flexibility.

It exposes the building blocks of CNNs as easy-to-use MATLAB functions, providing

routines for computing linear convolutions with filter banks, feature pooling, and many

more. In this manner, MatConvNet allows fast prototyping of new CNN architec-

tures; at the same time, it supports efficient computation on CPU and GPU allowing

to train complex models on large datasets such as ImageNet ILSVRC. This document

provides an overview of CNNs and how they are implemented in MatConvNet and

gives the technical details of each computational block in the toolbox.

Contents

1 Introduction to MatConvNet 1

1.1 Getting started . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2

1.2 MatConvNet at a glance . . . . . . . . . . . . . . . . . . . . . . . . . . . 4

1.3 Documentation and examples . . . . . . . . . . . . . . . . . . . . . . . . . . 5

1.4 Speed . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6

1.5 Acknowledgments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7

**2 Neural Network Computations 9
**

2.1 Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9

2.2 Network structures . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2.1 Sequences . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10

2.2.2 Directed acyclic graphs . . . . . . . . . . . . . . . . . . . . . . . . . . 11

2.3 Computing derivatives with backpropagation . . . . . . . . . . . . . . . . . . 12

2.3.1 Derivatives of tensor functions . . . . . . . . . . . . . . . . . . . . . . 12

2.3.2 Derivatives of function compositions . . . . . . . . . . . . . . . . . . 13

2.3.3 Backpropagation networks . . . . . . . . . . . . . . . . . . . . . . . . 14

2.3.4 Backpropagation in DAGs . . . . . . . . . . . . . . . . . . . . . . . . 15

2.3.5 DAG backpropagation networks . . . . . . . . . . . . . . . . . . . . . 18

**3 Wrappers and pre-trained models 21
**

3.1 Wrappers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3.1.1 SimpleNN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3.1.2 DagNN . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21

3.2 Pre-trained models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22

3.3 Learning models . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.4 Running large scale experiments . . . . . . . . . . . . . . . . . . . . . . . . . 23

3.5 Reading images . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23

4 Computational blocks 27

4.1 Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27

4.2 Convolution transpose (deconvolution) . . . . . . . . . . . . . . . . . . . . . 29

4.3 Spatial pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31

4.4 Activation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

4.5 Spatial bilinear resampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

4.6 Region of interest pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32

iii

iv CONTENTS

4.7 Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

4.7.1 Local response normalization (LRN) . . . . . . . . . . . . . . . . . . 33

4.7.2 Batch normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . 33

4.7.3 Spatial normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

4.7.4 Softmax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34

4.8 Categorical losses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

4.8.1 Classification losses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35

4.8.2 Attribute losses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 36

4.9 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

4.9.1 p-distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 38

5 Geometry 39

5.1 Preliminaries . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39

5.2 Simple filters . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

5.2.1 Pooling in Caffe . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40

5.3 Convolution transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42

5.4 Transposing receptive fields . . . . . . . . . . . . . . . . . . . . . . . . . . . 43

5.5 Composing receptive fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

5.6 Overlaying receptive fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44

6 Implementation details 45

6.1 Convolution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45

6.2 Convolution transpose . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 46

6.3 Spatial pooling . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

6.4 Activation functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

6.4.1 ReLU . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47

6.4.2 Sigmoid . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

6.5 Spatial bilinear resampling . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

6.6 Normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 48

6.6.1 Local response normalization (LRN) . . . . . . . . . . . . . . . . . . 48

6.6.2 Batch normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . 49

6.6.3 Spatial normalization . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

6.6.4 Softmax . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50

6.7 Categorical losses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

6.7.1 Classification losses . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

6.7.2 Attribute losses . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51

6.8 Comparisons . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

6.8.1 p-distance . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

6.9 Other implementation details . . . . . . . . . . . . . . . . . . . . . . . . . . 52

6.9.1 Normal sampler . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52

6.9.2 Euclid’s algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54

Bibliography 55

Chapter 1

Introduction to MatConvNet

**MatConvNet is a MATLAB toolbox implementing Convolutional Neural Networks (CNN)
**

for computer vision applications. Since the breakthrough work of [8], CNNs have had a

major impact in computer vision, and image understanding in particular, essentially replacing

traditional image representations such as the ones implemented in our own VLFeat [13] open

source library.

While most CNNs are obtained by composing simple linear and non-linear filtering op-

erations such as convolution and rectification, their implementation is far from trivial. The

reason is that CNNs need to be learned from vast amounts of data, often millions of images,

requiring very efficient implementations. As most CNN libraries, MatConvNet achieves

this by using a variety of optimizations and, chiefly, by supporting computations on GPUs.

Numerous other machine learning, deep learning, and CNN open source libraries exist.

To cite some of the most popular ones: CudaConvNet,1 Torch,2 Theano,3 and Caffe4 . Many

of these libraries are well supported, with dozens of active contributors and large user bases.

Therefore, why creating yet another library?

The key motivation for developing MatConvNet was to provide an environment par-

ticularly friendly and efficient for researchers to use in their investigations.5 MatConvNet

achieves this by its deep integration in the MATLAB environment, which is one of the most

popular development environments in computer vision research as well as in many other areas.

In particular, MatConvNet exposes as simple MATLAB commands CNN building blocks

such as convolution, normalisation and pooling (chapter 4); these can then be combined and

extended with ease to create CNN architectures. While many of such blocks use optimised

CPU and GPU implementations written in C++ and CUDA (section section 1.4), MATLAB

native support for GPU computation means that it is often possible to write new blocks

in MATLAB directly while maintaining computational efficiency. Compared to writing new

CNN components using lower level languages, this is an important simplification that can

significantly accelerate testing new ideas. Using MATLAB also provides a bridge towards

1

https://code.google.com/p/cuda-convnet/

2

http://cilvr.nyu.edu/doku.php?id=code:start

3

http://deeplearning.net/software/theano/

4

http://caffe.berkeleyvision.org

5

While from a user perspective MatConvNet currently relies on MATLAB, the library is being devel-

oped with a clean separation between MATLAB code and the C++ and CUDA core; therefore, in the future

the library may be extended to allow processing convolutional networks independently of MATLAB.

1

downloading a pre-trained CNN model.1.png')).uk/~vgg/practicals/cnn/index. it is possible to download. Next. namely a chain of operations.org/matconvnet/ 8 http://www.pdf 9 An example laboratory experience based on MatConvNet can be downloaded from http://www.'single').github. Finally.6 MatConvNet can learn large CNN models such AlexNet [8] and the very deep networks of [11] from millions of images. and install MatConvNet using three MATLAB com- mands. Examining the code of vl_simplenn (edit vl_simplenn in MatCon- vNet) we note that the wrapper transforms the data sequentially. requiring only MATLAB and a compatible C++ compiler (using the GPU code requires the freely-available CUDA DevKit and a suitable NVIDIA GPU).html. applying a number of MATLAB functions as specified by the network configuration. what makes them non trivial is their efficiency (section 1. Then create a bank of K = 16 random filters of size 3 × 3 using f = randn(3. It can be downloaded from http://www. These function.4) as well as support for backpropagation (section 2.3) to allow learning CNNs.3.vlfeat.org/matconvnet as well as from GitHub. compiling the package. for instance. A manual with a complete technical description of the toolbox is maintained along with the toolbox. where D = 3 is the number of colour channels in the image. 1.1 Getting started MatConvNet is simple to install and use. While powerful. 1. we demonstrate how to use one of such building blocks directly. discussed in detail in chapter 4. This particular wrapper can be used to model networks that have a simple structure.8 These features make MatConvNet useful in an educational context too. convolve the 6 http://devblogs.3.1 provides a complete example that clas- sifies an image using a latest-generation deep convolutional neural network. The example includes downloading MatConvNet. 1. a wrapper that takes as input the CNN net and the pre-processed image im_ and produces as output a structure res of results. While most blocks implement simple operations. compile. and evaluating the latter on one of MATLAB’s stock images. INTRODUCTION TO MATCONVNET other areas.com/parallelforall/deep-learning-image-understanding-planetary-scien 7 http://www. Several fully-functional examples demonstrating how small and large networks can be learned are included.16. The implementation is fully self-contained. The key command in this example is vl_simplenn. say using im = single(imread('peppers. Importantly.2 CHAPTER 1.9 MatConvNet is open-source released under a BSD-like license.vlfeat.vlfeat. obtaining a H × W × D array im.com/matconvnet .ox.ac. several standard pre-trained network can be immedi- ately downloaded and used in applications. For the sake of the example.org/matconvnet/matconvnet-manual.10 . MatConvNet re- mains simple to use and install. consider convolving an image with a bank of linear filters. As demonstrated in fig.nvidia.1 and section 1. robots. are called “building blocks” and constitute the backbone of MatCon- vNet. 10 http://www. MatConvNet was recently used by the University of Arizona in planetary science. as summarised in this NVIDIA blogpost. Start by reading an image in MATLAB. Pre-trained versions of several of these powerful models can be downloaded from the MatConvNet home page7 . fig.

1. bestScore)) . Figure 1.org/matconvnet/download/' . im_ = im_ − net.imageSize(1:2)) .. 'http://www.gz']) .3f'.1: A complete example including download.. % setup MatConvNet run matlab/vl_setupnn % load the pre−trained CNN net = load('imagenet−vgg−f. best] = max(scores) .tar.classes.vlfeat.normalization. imagesc(im) . score 0..description{best}.meta. score %. [bestScore.png') .normalization. % show the classification result scores = squeeze(gather(res(end). 'imagenet−vgg−f. installing.mat') . clf . . % load and preprocess an image im = imread('peppers..0−beta12 run matlab/vl_compilenn % download a pre−trained CNN from the web (run once) urlwrite(.vlfeat.mat') . net..org/matconvnet/models/imagenet−vgg−f. % run the CNN bell pepper (946).0−beta12. best. im_) . im_ = imresize(single(im). figure(1) .meta. . cd matconvnet−1..704 res = vl_simplenn(net.x)) . net. 'matconvnet−1.1. title(sprintf('%s (%d). GETTING STARTED 3 % install and compile MatConvNet (run once) untar(['http://www... compiling and running Mat- ConvNet to classify one of MATLAB stock images using a large CNN pre-trained on ImageNet.averageImage ..mat'.

1). the library provides numerous examples (in the examples/ subdirectory). and ImageNet datasets. including code to learn a variety of models on the MNIST. which is an implementation of stochastic gradient descent (section 3.f. suitably invoked by vl_simplenn.DagNN MATLAB class. and ImageNet data.dzdy) where dzdy is the derivative of the CNN output w. either to classify images or to produce image encodings in the spirit of Caffe or DeCAF. CIFAR10. f a filter bank.dzdf. It also provides a much more flexible wrapper supporting networks with arbitrary topologies. CNN wrappers. While users are encouraged to make use of the blocks directly to create new architectures. While several real-world examples of small and large CNN architectures and training routines are provided. MatConvNet contains the following elements: CNN computational blocks. Pre-trained models. A set of optimized routines computing fundamental building blocks of a CNN. MatConvNet provides a simple wrapper. one for each of the K filters in the bank.dzdb] = vl_nnconv(x. chapter 4 de- scribes all the blocks in detail. encapsulated in the dagnn. Example applications. MatConvNet is an ideal playground for research in computer vision and CNNs.3). For example. This results in an array y with K channels. a convolution block is implemented by y=vl_nnconv(x. MatConvNet provides several examples of learning CNNs with stochastic gradient descent and CPU or GPU. MatConvNet provides several state-of-the-art pre-trained CNN models that can be used off-the-shelf. that implements a CNN with a linear topology (a chain of blocks).r. and b a vector of biases (sec- tion 4. While this training code is perfectly serviceable and quite flexible.4 CHAPTER 1. All these examples use the examples/cnn_train training code. The derivatives are computed as [dzdx. . 1.f. directly as MATLAB commands. using the efficiency of MATLAB in prototyping. Users are welcome to implement their optimisers. it exposes simple functions to compute CNN building blocks. Often no C coding is required at all to try new architectures.[]). it is always possible to go back to the basics and build your own. it remains in the examples/ subdirec- tory as it is somewhat problem-specific.f.b. As such. CIFAR. These building blocks are easy to combine into complete CNNs and can be used to implement sophisticated learning algorithms. INTRODUCTION TO MATCONVNET image with the filters by using the command y = vl_nnconv(x. Rather than wrapping CNNs around complex layers of software. Furthermore. on MNIST.b) where x is an image.1). such as linear convolution and ReLU operators. MATLAB provides wrappers such as vl_simplenn for standard CNN architectures such as AlexNet [8] or Network-in-Network [9].2 MatConvNet at a glance MatConvNet has a simple design philosophy.t y (section 4.

3 0.robots. For example.ox. These include: AlexNet [8].html.1). Due to the problem size.11 Second. Most examples are fully self-contained. For example.9 dropout top-1 val dropout top-5 val 0. 1.8 bnorm top-1 val bnorm top-5 val 0. VGG-S [1]. large CNNs trained on ImageNet ILSVRC that can be downloaded and used as a starting point for many other problems [1].1 shows how one such model can be used in a few lines of MATLAB code. it suffices to point MATLAB to the MatConvNet root directory and type addpath ←- examples followed by cnn_mnist.e. and VGG-VD-19 [12].2: Training AlexNet on ImageNet ILSVRC: dropout vs batch normalisation.sh).2 illustrates the top-1 and top-5 validation errors as a model similar to AlexNet [8] is trained using either standard dropout regularisation or the recent batch normalisation technique of [4].6 0. First. i. 1. and VGG-VD- 16. in order to run the MNIST example.3 Documentation and examples There are three main sources of information about MatConvNet. including downloading and preprocessing the images (using the bundled script utils/preprocess−imagenet. The latter is shown to converge in about one third of the epochs (passes through the training data) required by the former. Several advanced examples are included as well. VGG-S. fig.2 0 10 20 30 40 50 60 epoch Figure 1. there is a PDF manual containing a great deal of technical details about the toolbox. including detailed mathematical descriptions of the building blocks. 11 See also http://www. VGG-M.4 0. . The MatConvNet website contains also numerous pre-trained models.7 0. The example code of fig. Third. 1. DOCUMENTATION AND EXAMPLES 5 0. the ImageNet ILSVRC example requires some more preparation. the website con- tains descriptions of all the functions and several examples and tutorials.uk/~vgg/practicals/cnn/index. MatConvNet ships with several examples (section 1.ac.3.1.5 0.

MatConvNet supports using NVIDIA GPUs as it includes CUDA implementations of all algorithms (or relies on MATLAB CUDA support).1 reports the training speed as number of images per second processed by stochastic gradient descent. gpuArray(w). We train the models discussed in section 1. despite MATLAB overhead. and MATLAB R2015a. Note also that. 256 GB of RAM. Furthermore. using multiple GPUs.8 116. While there is a substantial communication overhead.1 192. table 1. which is about 40% faster than the vanilla GPU implementation (using CuBLAS) and more than 10 times faster than using the CPUs. the size of a SGD batch must be decreased (to fit in the GPU memory). training speed increases from 20 images/s to 45. In this manner. The data is preprocessed to avoid rescaling images on the fly in MATLAB and stored in a RAM disk for faster access.5 136. CuDNN v2. 1. increasing the overhead impact somewhat. These are further divided in sub-batches of 22 images each to fit in the GPU memory. Addressing this overhead is one of the medium term goals of the library. .6 CHAPTER 1.3 on ImageNet ILSVRC.4 20.7 VGG-M 128 7.4 96. In this case. The driver examples/cnn_imagenet. we note that. the latter are then distributed among one to four GPUs on the same machine. []). Experiments use MatConvNet beta12.0 VGG-VD-19 24 1. the batch size is set to 264 images. The test machine is a Dell server with two Intel Xeon CPU E5-2667 v2 clocked at 3.5 15. switching between CPU and GPU is fully transparent.1 VGG-VD-16 24 1. table 1.7 16. one simply converts the arguments to gpuArrays in MATLAB. The code uses the vl_imreadjpeg command to read large batches of JPEG images from disk in a number of separate threads.4 Speed Efficiency is very important for working with CNNs.4 211. To use the GPU (provided that suitable hardware is available and the toolbox has been compiled with GPU support). Next we evaluate the performance of MatConvNet when training large architectures on the ImageNet ILSVRC 2012 challenge data [2]. Note that MatConvNet can also make use of the NVIDIA CuDNN library with significant speed and space benefits. INTRODUCTION TO MATCONVNET model batch sz.1 VGG-F 256 21.1: ImageNet training speed (images/s).5 Table 1. AlexNet trains at about 264 images/s with CuDNN.6 VGG-S 128 7.4 289. as the model grows in size. the implementation speed is comparable to Caffe (they report 253 images/s with CuDNN and a Titan – a slightly slower GPU than the Titan Black used here).7 18.30 GHz (each CPU has eight cores). CPU GPU CuDNN AlexNet 256 22. and four NVIDIA Titan Black GPUs (only one of which is used unless otherwise noted).2 reports the speed on VGG-VD-16.4 264. as in y = vl_nnconv(gpuArray(x). a very large model.2 110.m is used in all experiments.

5 Acknowledgments MatConvNet is a community project.0 22. 1. The implementation of several CNN computations in this library are inspired by the Caffe library [6] (however. Caffe is not a dependency).1. and as such acknowledgements go to all contributors.8 Table 1.5. We kindly thank NVIDIA supporting this project by providing us with top-of-the-line GPUs and MathWorks for ongoing discussion on how to improve the library. . ACKNOWLEDGMENTS 7 num GPUs 1 2 3 4 VGG-VD-16 speed 20.2: Multiple GPU speed (images/s).18 44. Several of the example networks have been trained by Karen Simonyan as part of [1] and [12].20 38.

.

vl_nnrelu (ReLU activation). vl_nnsigmoid (sigmoid activation). vl_nnpool (max and average pooling). The number of data samples Nl in a batch is called the batch cardinality. xL be the outputs of each layer in the network. vl_nnbnorm (batch nor- malization). MatConvNet includes a variety of layers. 2. one for each spatial location. or perform other operations that do not strictly conform to the CNN assumptions. Each intermediate output xl = fl (xl−1 . x2 .1 Overview A Neural Network (NN) is a function g mapping data x. emphasizing the concepts required to un- derstand and use MatConvNet. to an output vector y. It is also possible to conceive CNNs with more than two spatial dimensions. vl_nnloss (classification log-loss). such as vl_nnconv (convolution). . Many useful NNs contain a mixture of convolutional layers together with layer that process other data types such as text strings. The network is called convolutional because the functions fl are local and translation invariant operators (i. A fourth dimension Nl in the tensor spans multiple data samples packed in a single batch for efficiency parallel processing. vl_nnconvt (convolution transpose or deconvolution). for example an image. In a Convolutional Neural Network (CNN).Chapter 2 Neural Network Computations This chapter provides a brief introduction to the computational aspects of neural networks. vl_nnsoftmax (softmax operator). there are little a-priori re- strictions on the format of data in neural networks in general. . . Let x1 . In fact. wl ) is computed from the previous output xl−1 by applying the function fl with parameters wl . and convolutional neural networks in particular.e. vl_nnspnorm (spatial normalization). for example an image label. which are called computational blocks or layers. where the additional dimensions may represent volume or time. the data has a spatial structure: each xl ∈ Hl ×Wl ×Cl R is a 3D array or tensor where the first two dimensions Hl (height) and Wl (width) are interpreted as spatial dimensions. non-linear filters) like linear convolution. Hence. . vl_nnnormalize (local response normal- 9 . and let x0 = x denote the network input. the tensor xl represents a Hl × Wl field of Cl - dimensional feature vectors. The function g = fL ◦ · · · ◦ f1 is the composition of a sequence of simpler functions fl . contained in the matlab/ directory. The third dimension Cl is instead interpreted as the number of feature channels.

one for each of a 1. which defines a composite function xL = f (x0 . layers in a NN are arranged in a sequence. 2.). In the example of fig. . This section discusses such configurations and introduces a graphical notation to visualize them. as further discussed in section 1. The CNN parameters can then be tuned or learned to minimize this loss averaged over a large dataset of labelled example images.1 Sequences Start by considering a computational block f in the network. or vl_nnpdist (p-distance). . . and in fact very useful in many cases. evaluating the network is a simple matter of evaluating all the blocks from left to right. w1 .1. however. the output ŷ = f (x) is a vector of probabilities. SGD also requires to compute the CNN derivatives. There are enough layers to implement many interesting state-of-the-art networks out of the box.2. NEURAL NETWORK COMPUTATIONS ization – LRN). .000 possible image labels (dog. . This can be represented schematically as a box receiving data x and parameters w as inputs and producing data y as output: x f y w As seen above.. as explained in the next section. 2.. 1. fL xL w1 w2 wL Given an input x0 .4. .2 Network structures In the simplest case.. more complex interconnections are possible as well. thus. efficiency is a paramount in MATLAB design. cat. If y is the true label of image x. or even import them from other tool- boxes such as Caffe. trilobite. Learning generally uses a variant of stochastic gradient descent (SGD). we can measure the CNN performance by a loss function `y (ŷ) ∈ R which assigns a penalty to classification errors..10 CHAPTER 2. networks may contain several million parameters and need to be trained on millions of images. in the simplest case blocks are chained in a sequence f1 → f2 → · · · → fL yielding the structure: x1 x2 xL−1 x0 f1 f2 . While this is an efficient method (for this type of problems). wL ). NNs are often used as classifiers or regressors.

There are a few noteworthy properties of this graph: 1. This is possible exactly when the interconnections between layers form a directed acyclic graph. Here boxes denote functions and circles denote variables (parameters are treated as a special kind of variables). The graph is bipartite. In the example.g. variables and parameters can have an arbitrary number of outputs (a parameter with more of one output is shared between different layers). In order to visualize DAGs. in the sense that arrows always go from boxes to circles and from circles to boxes. but must be set prior to evaluation.1: Example DAG. . all its input have been evaluated prior to it. variables have at most one input and parameters none.2 Directed acyclic graphs One is not limited to chaining layers one after another. 2.e. NETWORK STRUCTURES 11 f1 x1 x0 w1 f3 x3 f2 x2 f5 x7 w2 x5 w5 x4 f4 w4 x6 Figure 2.2.2. 3. although these are usually the variables with no outgoing arrows. it is useful to introduce additional nodes for the network variables. as in the example of Fig. Functions can take any number of inputs (e. x0 and x4 are the inputs of the CNN and x6 and x7 the outputs. when a layer has to be evaluated. 2. Any variable (or even param- eter) may be used as output. f4 has two). or DAG for short. Variables with no incoming arrows and parameters are not computed by the network. Functions can have any number of inputs or outputs. the only requirement for evaluating a NN is that. f3 and f5 take two) and have any number of outputs (e. i. 2. In fact.2.1. they are inputs.g.

. Derivatives are computed using an algorithm called backpropagation. x121 . . .. . . f2 . the CNN can be evaluated by sorting the functions and computing them one after another (in the example. each layer f can be seen reinterpreted as vector function vec f . .3 Computing derivatives with backpropagation Learning a NN requires computing the derivative of the loss with respect to the network parameters.... ∂x111 ∂x211 ∂xH11 ∂x121 ∂xHW C ∂y211 ∂y211 ∂y211 ∂y211 .. . ∂x∂yHW 211 ∂x111 ∂x211 ∂xH11 ∂x121 C . . . . . . .. evaluating the functions in the order f1 . .. .. The derivative of the function f contains the derivative of each output component yi0 j 0 k0 with respect to each input component xijk .. we discuss the derivatives of a single layer. . .3. . . d vec f ∂yH 0 11 ∂yH 0 11 ∂yH 0 11 ∂yH 0 11 ∂yH 0 11 = . ... .. Since the graph is acyclic. NEURAL NETWORK COMPUTATIONS 4.. a layer is a function y = f (x) where both input x ∈ RH×W ×C and output 0 0 0 y ∈ RH ×W ×C are tensors. f5 would work). f4 . ..1 Derivatives of tensor functions In a CNN. f3 . ∂xH11 ∂x121 121 C . . whose derivative is the conventional Jacobian matrix: ∂y111 ∂y111 ∂y111 ∂y111 ∂y111 . . . . .. which is a memory-efficient implementation of the chain rule for derivatives. .. ... ∂yH 0 W 0 C 0 ∂yH 0 W 0 C 0 ∂yH 0 W 0 C 0 ∂yH 0 W 0 C 0 ∂yH 0 W 0 C 0 ∂x111 ∂x211 . ∂x∂yHW ∂x 111 ∂x211 . xHW C By stacking both input and output. .. . . .. . and then of a whole network. . . it is often useful to switch to a matrix notation by stacking the input and output tensors into vectors. . .. First. This is done by the vec operator. 2.. vec x = xH11 . . . ∂xH11 ∂x121 . which visits each element of a tensor in lexicographical order and produces a vector: x111 x211 . d(vec x)> ∂x111 ∂x211 ∂xH11 ∂x121 ∂xHW C ∂y121 ∂y121 ∂y121 ∂y121 .. . . Instead of expressing derivatives as tensors. .. 2..12 CHAPTER 2. for a total of H 0 × W 0 × C 0 × H × W × C elements naturally arranged in a 6D tensor. ∂xHW C This notation for the derivatives of tensor functions is taken from [7] and is used throughout this document. .

. 2. the target derivative df /d(vec wl )> has the same number of elements of the parameter vector wl .g. which is moder- ate. since the network output xl is a scalar quantity. wl+1 ) d vec fl (xl−1 . wl+1 ) d vec fl (xl−1 . consider first a simple CNN terminating in a loss function fL = `y : x1 x2 xL−1 x0 f1 f2 . wl+1 ) d vec fl (xl−1 . these matrices are in general extremely large. In order to avoid computing these factor explicitly.. Start by multiplying the output of the last layer by a tensor pL = 1 (note that this tensor is a scalar just like the variable xL ): df d vec fL (xL−1 .. as seen above.. an unmanageable size. COMPUTING DERIVATIVES WITH BACKPROPAGATION 13 While it is easy to express the derivatives of tensor functions as matrices. The factor pL−1 can therefore be . wL ) ◦ . wl ) = (vec pL−1 )> × · · · × × d(vec xl )> d(vec wl> ) In the second line the last two factors to the left have been multiplied obtaining a new tensor pL−1 that has the same size as the variable xL−1 . Even for moderate data sizes (e. Note that. w1 )] . wL ) d vec fl+1 (xl . wl ) = × ··· × × (2. w2 ) ◦ f1 (x0 . the derivative can be written as df d vec fL (xL−1 . However. Storing that requires 68 GB of space in single precision. ◦ f2 (·.2.3. The purpose of the backpropagation algorithm is to compute the derivatives required for learning without incurring this huge memory cost. d(vec wl ) d(vec wl )> By applying the chain rule and by using the matrix notation introduced above. wl ) pL × = pL × ×··· × × d(vec wl ) > d(vec xL−1 ) > d(vec xl )> d(vec wl> ) | {z } (vec pL−1 )> d vec fl+1 (xl .2 Derivatives of function compositions In order to understand backpropagation. wL ) d vec fl+1 (xl . the intermediate Jacobian factors have. we can proceed as follows.1) d(vec wl )> d(vec xL−1 ) > d(vec xl )> d(vec wl> ) where the derivatives are computed at the working point determined by the input x0 and the current value of the parameters. there are H 0 W 0 C 0 HW C ≈ 17 × 109 elements in the Jacobian. H = H 0 = W = W 0 = 32 and C = C 0 = 128). fL xl ∈ R w1 w2 wL The goal is to compute the gradient of the loss value xL (output) with respect to each network parameter wl : df d > = [fL (·.3.

[]. The construction is then repeated by multiplying pairs of factors from left to right. which is a large Jacobian matrix. The computation of large Jacobian is encapsulated in the function call and never carried out explicitly. In the “forward” mode. Hence the derivative (2. wl+1 ) (vec pl )> = (vec pl+1 )> × . a CNN toolbox provides implementations of each layer f that provide: A forward mode. Since computing the derivative is a linear operation. w) of the layer given its input x and parameters w. computing the projected derivatives dhp. computing the output y = f (x. w. tensor pl is obtained from pl+1 as the product: d vec fl+1 (xl . w)i dhp. which results in a scalar quantity.w.2) needs not to use the vec notation. Note that. dx dw given.w. In order to implement backpropagation.3. dw] = vl_nnconv(x.14 CHAPTER 2. This is best illustrated with an example. we provide a schematic interpretation of backpropagation and show how it can be implemented by “reversing” the NN computational graph.[]) to apply the filters w to the input x and obtain the output y. w)i and . . pl until the desired derivative is obtained. This process is known as backpropagation. in doing so. Consider a layer f such as the convolution operator implemented by the MatConvNet vl_nnconv command. respectively. NEURAL NETWORK COMPUTATIONS explicitly stored. d(vec xl )> The key to implement backpropagation is to be able to compute these products without explicitly computing and storing in memory the second factor. and y. in addition to the input x and parameters w. In general. The projected derivative of eq. no large tensor is ever stored in memory. . 2. obtaining a sequence of tensors pL−2 . (2.p). f (x. f (xl . and yields a tensor pl that has the same size as xl as expected. and p have the same size as x. .2) dxl Here h·. In the “backward mode”. A backward mode. ·i denotes the inner product between tensors. wl )i pl = . f (x. As explained above.3 Backpropagation networks In this section. this product can be interpreted as the derivative of the layer projected along direction pl+1 : dhpl+1 . dx. a tensor p that the same size as y. . (2. dw.2) can be seen as the derivative of the following mini- network: . one calls [dx. one calls the function as y = vl_nnconv(x.

. . xL−1 . by computing the derivative of the mini-network. The projected derivative can also be though of as a new layer (dx.. that all variables are sorted in order of computation (x0 . the function df is linear in the argument p. w.. (2. x1 . xL−1 fL xL w1 w2 wL dx0 df1 dx1 df2 dx2 .3.3.2. xL ) according to the . it can be useful to think of the projection p as the “linearization” of the rest of the network from variable y down to the loss.4 Backpropagation in DAGs Assume that the DAG has a single output variable xL and assume. the forward and backward passes through the original network can be rewritten as evaluating an extended network which contains a BP-reverse of the original one (in blue in the diagram): x0 f1 x1 f2 x2 .. . p) that. Using this notation. . without loss of generality. . dxL−1 dfL dpL dw1 dw2 dwL 2. operates in the reverse direction: x w dx df p dw By construction (see eq. COMPUTING DERIVATIVES WITH BACKPROPAGATION 15 y x f h·. ·i z∈R w p In the context of back-propagation. dw) = df (x.2)).

dxl−1 ) to null tensors of the same size as (x0 . . hl+1 i dhpL . There is at most one such layer. since there are no intermediate variables between xL−1 and xL . . xl−1 ) = hl+1 (x0 . xL−1 ). given the derivatives of hl+1 . xl−1 . backpropagation computes the derivatives of hl by using the relation xL = hl (x0 . . in order to simplify the notation. xl−1 ). . . . . . . . . . . effectively transforming the first l variables into inputs. In practice. for all 0 ≤ t ≤ l − 1: dhp. the update operation needs to be carried out only for the variables xl that are actual inputs to fπL . In the first case. hL (x0 .16 CHAPTER 2. Here πl denotes the index of the layer fπl that computes the value of the variable xl . . or none if xl is an input or parameter of the original NN. dxt Given this information. At the beginning of backpropagation. . . Thus the projected derivatives of hL are the same as the projected derivatives of fπL . . x1 . In general. . . the rest of the DAG defines a function hl that maps these input variables to the output xL : xL = hl (x0 . . Backpropagation starts by initializing variables (dx0 . . we show that backpropagation in a DAG iteratively computes the projected derivatives of all functions h1 . assume that this list contains both data and parameter variables. . . . Next. . . dxt Here. . . . . it computes the projected derivatives of xL = hL (x0 . . xL−1 ) = fπL (x0 . for uniformity with the other iterations. the function hL is the same as the last layer fπL . L − 1 : dxt = . xl−1 )i ∀t = 0. each dxt contains the projected derivative of function hL with respect to the corresponding variable: dhpL . L − 1 : dxt ← dxt + . we use the fact that dxl are initialized to zero anaccumulate the values instead of storing them. . . hl i dhp. x1 . which is often a tiny fraction of all the variables in the DAG. so that general one has: xl = fπl (x0 . xt−1 )i ∀t = 0. . . . . . After the update. . the next iteration of backpropagation updates the variables to con- tain the projected derivatives of hL−1 instead. . . . . as the distinction is moot for the discussion in this section. the layer may depend on any of the variables prior to xl in the sequence. . . . hL with respect to all their parameters. . . . x1 . xl−1 ). . . Then. resulting in the equation dhpL . xl−1 )) Applying the chain rule to this expression. x1 . . xl−1 ). Next. We can cut the DAG at any point in the sequence by fixing x0 . . xl−1 to some arbitrary value and dropping all the DAG layers that feed into them. . . . Furthermore. fπL (x0 . . d(vec xt ) d(vec xt ) d(vec xl )> d(vec xt )> | {z } vec dxl . x1 . hl+1 i d vec fπl > = > + . . fπL (x0 . NEURAL NETWORK COMPUTATIONS DAG structure. . .

L : dxl−1 = . . (2. . . fπl (x0 . Initialize (dx0 . b) Update the variables using the formula: dhdxl . . l − 1 : dxt = .e. . . . Sort all variables by computation order (x0 . . . l − 1 : dxt ← dxt + . 4. Thus. if xl is a data input or a parameter of the original neural network. 3. . 2. . functions hl are obtained from the original network f by transforming variables x0 . . xl−1 )i ∀t = 0. In particular. a function with no arguments). backpropagation remains with: dhpL . . dxL−1 ) to null tensors with the same size as the corresponding variables. . . l − 1 : dxt ← dxt + . . the update does not do anything. . hl (x0 . . . . Perform a forward pass through the network to compute all the intermediate vari- able values. If xl−1 was already an input (data or parameter) of f . . . . use the “backward mode” of the layer fπl to compute its derivative projected onto dxl as needed. COMPUTING DERIVATIVES WITH BACKPROPAGATION 17 This yields the update equation dhpl . . . . after all L iterations are complete. . .2. then the derivative dxl−1 is applicable to f as well. 2. the values of all input variables (including the parameters). then xl does not depend on any other variables or parameters and fπl is a nullary function (i. . xl−1 )i ∀t = 0. xl−1 )i ∀t = 0. dxt Note that the derivatives for variables xt . and the value of the projection pL (usually xL is a scalar and pL = pL = 1): 1. L − 1. . x1 . . . . .3) dxt Once more. .3. If there is no such layer (because xl is an input or parameter of the network). fπl (x0 . For l = L. . After iteration L − l + 1 completes. xl−1 ) that evaluates variable xl . where pl = dxl . . . . go to the next iteration. . . . Backpropagation can be summarized as follows: Given: a DAG neural network f with a single output xL . . dxt To do so efficiently. . xl−1 )i ∀l = 1. . dxl−1 As seen above. . . backpropagation terminates with dhpL . the update needs to be explicitly carried out only for the variables xt that are actual inputs of fπl . . . . 1: a) Find the index πl of the layer xl = fπl (x0 . xl−1 into to inputs. In this case. hl (x0 . . l ≤ t ≤ L − 1 are not updated since hl does not depend on any of those. . . xL ) according to the DAG.

3. 2. backpropagation in DAGs can be implemented as a corresponding BP-reversed DAG. If a variable xt (or parameter wl ) is an input of fl . which creates a conflict. The BP network corresponding to the DAG of Fig. 4. then it is an input of dfl as well. then the variable dxt (or the parameter dwl ) is an output dfl . and variable/parameter xt and wl . Resolve these conflicts by inserting a summation layer that adds these contributions (this corresponds to the summation in the BP update equation (2. then dxt would be the output of two or more layers in the reversed network. 3. create a corresponding layer dfl and variable/parameter dxt and dwl . In the previous step.5 DAG backpropagation networks Just like for sequences.1 is given in Fig. . To construct the reversed DAG: 1. For each layer fl .2. 2. NEURAL NETWORK COMPUTATIONS 2.18 CHAPTER 2. if a variable xt (or parameter wl ) is input to two or more layers in f .3)). 2. If a variable xt (or parameter wl ) is an input of fl .

3.2: Backpropagation network for a DAG. COMPUTING DERIVATIVES WITH BACKPROPAGATION 19 f1 x1 x0 w1 f3 x3 f2 x2 f5 x7 w2 x5 w5 x4 f4 w4 x6 df1 dx1 dx0 Σ dw1 df3 dx3 df2 dx2 df5 p7 dw2 dx5 dw5 dx4 df4 Σ p6 dw4 Figure 2. .2.

.

as illustrated in section 3.1 Wrappers MatConvNet provides two wrappers: SimpleNN for basic chains of blocks (section 3.2 DagNN The DagNN wrapper is more complex than SimpleNN as it has to support arbitrary graph topologies. it is usually much more convenient to use them through a wrapper that can implement CNN architectures given a model specification.1 SimpleNN The SimpleNN wrapper is suitable for networks consisting of linear chains of computational blocks. it is in practice much more flexible and easier to extend. and makes the wrapper slightly slower for tiny CNN architectures (e. These are very simple to use. Its design is object oriented. with a few other support functions such as vl_simplenn_move (moving the CNN between CPU and GPU) and vl_simplenn_display (obtain and/or print information about the CNN).g. It is largely implemented by the vl_simplenn function (evaluation of the CNN and of its derivatives).1) and DagNN for blocks organized in more complex direct acyclic graphs (section 3. and face recognition. the implementation of vl_simplenn is a good example of how the basic neural net building blocks can be used together and can serve as a basis for more complex implementations. 3. MNIST).Chapter 3 Wrappers and pre-trained models It is easy enough to combine the computational blocks of chapter 4 “manually”. In fact. 21 . While this adds complexity.1.1.1. 3. depending on the mode of operation. with one class implementing each layer type.2.1. image segmentation. text spotting. Please refer to the inline help of the vl_simplenn function for details on the input and output formats. The available wrappers are briefly summarised in section 3. vl_simplenn takes as input a structure net representing the CNN as well as input x and potentially output derivatives dzdy.2). MatConvNet also comes with many pre-trained models for image classification (most of which are trained on the ImageNet ILSVRC challenge). 3.1. However.

. im_) . im_ = im_ − net. title(sprintf('%s (%d). images can be cropped rather than rescaled.meta.normalization. 'imagenet−vgg−f.255]. im_ = single(im) ..mat') . image intensities are normalized in the range [0. best] = max(scores) . imagesc(im) .x)) . net.meta.normalization field that describes the type of preprocessing that is expected.averageImage . 3. [bestScore. Second. usually for improved results.description{best}.imageSize(1:2)) . net. the pre-trained model contains a net. While preprocessing specifics depend on the model. The output of the last layer can be used to classify the image. First. Note in particular that this network takes images of a fixed size as input and requires removing the mean. 'http://www. multiple crops can be fed to the network and results averaged.mat') .png') .. The next step is running the CNN.3f'. clf . bestScore)) .. figure(1) .DagNN class (under the dagnn namespace). also.. The class names are contained in the net structure for convenience: % show the classification result scores = squeeze(gather(res(end).2 Pre-trained models vl_simplenn is easy to use with pre-trained models (see the homepage to download some). Third. Note that the image should be preprocessed before running the network.vlfeat. % obtain and preprocess an image im = imread('peppers. % note: 255 range im_ = imresize(im_.classes.org/matconvnet/models/imagenet−vgg−f. net = load('imagenet−vgg−f.. best.normalization. .meta. score %. This will return a res structure with the output of the network layers: % run the CNN res = vl_simplenn(net.meta. the following code downloads a model pre-trained on the ImageNet data and applies it to one of MATLAB stock images: % setup MatConvNet in MATLAB run matlab/vl_setupnn % download a pre−trained CNN from the web urlwrite(. For example.mat'. WRAPPERS AND PRE-TRAINED MODELS DagNN is implemented by the dagnn. the output of the network can be used as generic features for image encoding. Note that several extensions are possible...22 CHAPTER 3.

In this manner. This code is flexible enough to allow training on NMINST. transform them.3. The procedure is as follows: 1. Corresponding examples are provided in the examples/ directory. Given an input image of size H × W . 3. such as learning a network for ImageNet. Do not forget to point the training code to the pre-processed data. Image cropping and scaling.3. Do not forget to point the training code to this copy. Wo ) = s(H.3 Learning models As MatConvNet can compute derivatives of the CNN using backpropagation. Compile MatConvNet with GPU support. a NVIDIA GPU (at least 6GB of memory) and adequate CPU and disk speeds are highly recommended.sh script. Consider copying the dataset into a RAM disk (provided that you have enough memory) for faster access. Once your setup is ready.image-net. Several options in vl_imreadjpeg control how images are cropped and rescaled. training will not have to resize the images every time. W )).org/challenges/LSVRC.5 Reading images MatConvNet provides the tool vl_imreadjpeg to quickly read images. you should expect to be able to train with 200-300 images/sec. equal to a specified constant size. LEARNING MODELS 23 3. This can be done with the supplied utils/preprocess-imagenet. it is simple to implement learning algorithms with it. W ). Wo ) = (H. s = max{Ho /H. If all goes well. A basic implementation of stochastic gradient descent is therefore straightforward. See the homepage for instructions. 3. Wo /W }). we suggest the following: Download the ImageNet data http://www. or obtained by setting the minimum side equal to a specified constant and rescaling the other accordingly ((Ho . and probably many other datasets. The output size is either equal the input size ((Ho . CIFAR. Example code is provided in examples/cnn_train. first the size of the output image Ho × Wo is determined. In- stall it somewhere and link to it from data/imagenet12 Consider preprocessing the data to convert all images to have a height of 256 pixels. and move them to the GPU. to train on ImageNet.4 Running large scale experiments For large scale experiments. . you should be able to run examples/cnn_imagenet (edit the file and change any flag as needed to enable GPU support and image pre-fetching on multiple threads). ImageNet. For example.

9 makes the crop slightly smaller. a+ are. the image contrast is changed as follow: x(u. vl_imreadjpeg supports some basic colour postrpocessing. the shape of the crop is given by: (Hc . respectively. the crop is extracted relative to the input image either in the middle (center crop) or randomly shifted. W/(aWo )}. similarly to the vector µ. A a standard center crop of 128 pixels can be obtained by setting Ho = Wo = 128.0 allows picking differently-sized crops each time. Setting ρ− = 0.9 and ρ+ = 1.24 CHAPTER 3. v)]. v) in the image. but not both. The shift is randomly sampled from a Gaussian distribution with standard deviation B.e. 3.9 and a+ = 1. v) ← x(u. a+ ]) where a− . a− = a+ = 1 (CropAnisotropy option). the minimum and maximum anisotropy. and to also perturb randomly the saturation and contrast of the image. vl_imreadjpeg extract an image as is. i. Wc ). The brightness shift is a constant offset b added to all pixels in the image. Given the crop size (Hc . the crop size Hc × Wc is determined. v) + b − µ. The relative crop scale is determined by sampling a parameter ρ ∼ U ([ρ− . allowing it to shift in both directions. Here. Overall. B ∈ R3×3 is the square root of the covariance matrix of the Gaussian. which is however subtracted and constant for all images in the batch. starting from the crop anisotropy a = (Wo /Ho )/(Wc /Hc ). WRAPPERS AND PRE-TRAINED MODELS 2. It allows to subtract from all the pixels a constant shift µ ∈ R3 ( µ can also be a Ho × Wo image for fixed-sized crops). In the input image. which allows to squash a rectangular input into a square output. aWo ) min{aH/Ho . If the input image is rectangular. Wc ) = ρ(Ho /a. this crop is isotropically stretched to fill either its width or height. ω ∼ N (0. respectively. Next. (resize option). ρ+ ]) where ρ− . average color subtraction and brightness shift results in the transformation: x(u. without any processing. These transformations are discussed in detail next. the minimum and maximum relative crop sizes. Wc ) ∝ (Ho /a. If x(u. it is also possible to flip a crop left-to-right with a 50% probability. 1 + C]) uv . It also allows to add a random shift vector (sample independently for each image in a batch). v) ∈ R3 is an RGB triplet at location (u. aWo ). the relative change of aspect ratio from the crop to the output: (Hc . Setting ρ− = ρ+ = 0. 5. ρ+ are. Color post-processing. Another option is to sample it as a ∼ U([a− . v) ← γx(u. Setting a− = 0. One option is to choose a = (W/H)/(Wo /Ho ) such that the crop has the same aspect raio of the input image. v) + (1 − γ) avg[x(u. such a crop can either slide horizontally or vertically (CropLocation).2 allows the crops to be slightly elongated or widened. such that: b ← Bω. In the simples case. 4. The absolute maximum size is determined by the size of the input image. Finally. After this shift is applied. γ ∼ U([1 − C. and ρ− = ρ+ = 1 (CropSize option). I).

READING IMAGES 25 where the coefficient γ is uniformly sampled in the interval [1 − C. This is controlled by the saturation deviation coefficient S: 1−σ > x(u. 1 + S]) 3 Overall. v)] + Bω − µ . 3 uv For grayscale images.3. Note that.5. contrast can also be increased. v). v) ← σx(u. pixels are transformed as follows: 1−σ > x(u. σ ∼ U([1 − S. v) + (1 − γ) avg[x(u. The last transformation changes the saturation of the image. v) + 11 x(u. changing the saturation does not do anything (unless ones applies first a colored shift. since γ can be larger than one. 1 + C] where is C is the contrast deviation coefficient. . which effectively transforms a grayscale image into a color one). v) ← σI + 11 γx(u.

.

whereas w may have an arbitrary shape. See the rest of the chapter and MATLAB inline help for details on the syntax. others can take multiple inputs and parameters. This is done by passing a third optional argument dzdy representing the derivative of the output of the network with respect to y.w) that takes as input MATLAB arrays x and w representing the input data and parameters and returns an array y as output. however. 27 . as discussed above. In general. Here 0 0 ×D×D 00 00 ×W 00 ×D 00 x ∈ RH×W ×D . Different functions may use a slightly different syntax.f. the low-level functions aim at providing a straightforward and obvious interface even if this means differing slightly from block to block. dzdx. specified as property-value pairs.dzdy) with respect to the input data and parameters.w. The arrays dzdx.3).1 Convolution The convolutional block is implemented by the function vl_nnconv.g. The block is implemented as a MATLAB function y = vl_nn<block>(x. in this case. 1 Other parts of the library will wrap these functions into objects with a perfectly uniform interface.b) com- putes the convolution of the input map x with a bank of K multi-dimensional filters f and biases b. the function returns the derivatives [dzdx. with a particular focus on their analytical definition. dzdy and dzdw have the same dimensions of x. f ∈ RH ×W . as needed: many functions can take additional optional arguments. a rectified linear unit). some do not have parameters w (e. y=vl_nnconv(x. y ∈ RH . 4. The interface of a CNN computational block <block> is designed after the discussion in chapter 2. in order to compute derivatives. dzdy or dzdw. Refer instead to MATLAB inline help for further details on the syntax. y and w respectively (see section 2. The function implementing each block is capable of working in the backward direction as well.dzdw] = vl_nn<block>(x. in which case there may be more than one x. w.1 The rest of the chapter describes the blocks implemented in MatConvNet.Chapter 4 Computational blocks This chapters describes the individual computational blocks supported by MatConvNet. x and y are 4D real arrays packing N maps or images.

The figure illustrates the process of filtering a 1D signal x by a filter f to obtain a signal y.d00 .e. vl_nnconv allows to specify top-bottom-left-right paddings (Ph− . Pw− .f.d0 .= 2 x P+ = 3 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 y 1 2 1 2 3 4 1 2 3 4 1 2 3 4 Figure 4. Pw+ ) of the input array and subsampling strides (Sh . the array x is implicitly extended with zeros as needed. The samples of x involved in the calculation of a sample of y are shown with arrow. While in this case the sample is in the padded region.d0 . Filters are applied in a sliding-window manner across the input signal. vl_nnconv computes only the “valid” part of the convolution. h i0 =1 j 0 =1 d0 =1 In this expression. Formally. The size of the output is computed in section 5.j 00 +j 0 −1. Sh . Sw ) of the output array: H X0 W X D 0 X yi00 j 00 d00 = bd00 + fi0 j 0 d × xSh (i00 −1)+i0 −P − . it requires each application of a filter to be fully contained in the input support. The filter has H 0 = 4 elements and is applied with a stride of Sh = 2 samples. Output size.1 for technical details. Padding and stride.28 CHAPTER 4. square images). this can happen also without padding. i. 4.d00 .2 and is given by: H − H 0 + Ph− + Ph+ 00 H =1+ . for example. the output is given by H0 X X W0 X D yi00 j 00 d00 = bd00 + fi0 j 0 d × xi00 +i0 −1. COMPUTATIONAL BLOCKS P. Note that the function works with arbi- trarily sized inputs and filters (as opposed to. Ph+ .1 for a 1D slice.Sw (j 00 −1)+j 0 −Pw− . Note that the rightmost sample of x is never processed by any filter application due to the sampling step. i0 =1 j 0 =1 d0 =1 The call vl_nnconv(x.[]) does not use the biases. The purple areas represented padding P− = 2 and P+ = 3 which is zero- filled. See section 6. The process of convolving a signal is illustrated in fig.1: Convolution.

a dilation factor d = 2 causes the 1D kernel [f1 . the former is a special case of the latter obtained when the output map y has dimensions W 00 = H 00 = 1. Very often it is useful to geo- metrically relate the indexes of the various array to the input data (usually images) in terms of coordinate transformations and size of the receptive field (i. Another application is sum pooling.2. convolution transpose is implemented by the function vl_nnconvt. with dilation factors dh . . For instance. . MatConvNet does not distinguish between fully connected layers and convolutional blocks. vl_nnconv allows to group channels of the input array x and apply different subsets of filters to each group.e. Internally. To use this feature. . In order to understand convolution transpose.1. fully connected blocks or layers are linear functions where each output dimension depends on all the input dimensions. f2 ] to be implicitly transformed in the kernel [f1 . y ∈ RH . Thus. this is considerably slower than calling the dedicated pooling function as given in section 4. one can specify D groups of D0 = 1 dimensional filters identical filters of value 1 (however. otherwise an error is thrown. Dilation. These are treated as g = D/D0 filter groups. Filter groups. W 0 = dw (Wf − 1) + 1. With dilation.3). . Receptive field size and geometric transformations. For additional flexibility. Wf ) is equivalent to a filter of size: H 0 = dh (Hf − 1) + 1. vl_nnconv allows kernels to be spatially dilated on the fly by inserting zeros between elements. This is derived in section 5. f ∈ RH ×W . the convolution becomes: Hf Wf D X XX yi00 j 00 d00 = bd00 + fi0 j 0 d × xSh (i00 −1)+dh (i0 −1)−P − +1. let: 0 0 ×D×D 00 00 ×W 00 ×D 00 x ∈ RH×W ×D . . . a filter of size (Hf . Fully connected layers. h i0 =1 j 0 =1 d0 =1 4. 2D0 00 00 00 and so on. In MatConvNet. specify 0 0 0 00 as input a bank of D00 filters f ∈ RH ×W ×D ×D such that D0 divides the number of input dimensions D. An application of grouping is implementing the Krizhevsky and Hinton network [8] which uses two such streams. In other libraries. the first group is applied to dimensions d = 1.2. D0 of the input x. in the latter case. dw . of the image region that affects an output). . . 0.Sw (j 00 −1)+dw (j 0 −1)−Pw− +1.d0 . CONVOLUTION TRANSPOSE (DECONVOLUTION) 29 Note that the padded input must be at least as large as the filters: H + Ph− + Ph+ ≥ H 0 .4. Instead. . f2 ]. Note that the output is still an array y ∈ RH ×W ×D .2 Convolution transpose (deconvolution) The convolution transpose block (sometimes referred to as “deconvolution”) is the transpose of the convolution block described in section 4.d00 . vl_nnconv handles this case more efficiently when possible. the second group to dimensions d = D0 + 1. 0.

using the defini- tions given in section 4.Sh ).Sw ) X yi00 j 00 d00 = f1+Sh i0 +m(i00 +P − . The filter is applied as a sliding-window. The first one is the so called deconvolutional networks [14] and other networks such as convolutional decoders that use the transpose of a convolution.= 2 y C+ = 3 1 2 3 4 1 2 3 4 1 2 3 4 1 2 3 4 x 1 2 4 2 3 4 1 2 3 4 1 2 3 4 Figure 4.Sw ). as the convolution block supports input padding and output downsampling.1. The filter has H 0 = 4 samples in total. instead.30 CHAPTER 4. S) = (k − 1) mod S. there is no need to add padding to the input array. The purple areas represent crops with C− = 2 and C+ = 3 which are discarded. The arrows exemplify which samples of x are involved in the calculation of a particular sample of y. The figure illustrates the process of filtering a 1D signal x by a filter f to obtain a signal y.Sw ). d0 (4. h − 1+Sw j 0 +m(j 00 +Pw . n) = . The second one is implementing data interpolation.d0 × d0 =1 i0 =0 j 0 =0 x1−i0 +q(i00 +P − .1) h where k−1 m(k. convolution transpose computes instead vec y = M > vec x. the convolution transpose block supports input upsampling and output cropping.1 for the convolution operator.1. In fact. S . 4.2. since convolution is linear. and the latter can be reduced by cropping the output instead. q(k. it can be expressed as a matrix M such that vec x = M vec y. This process is illustrated for a 1D slice in fig. d00 . although each filter application uses two of them (blue squares) in a circulant manner.2: Convolution transpose. Imagine operating in the reverse direction by using the filter bank f to convolve the output y to obtain the input x. filters. forming a pattern which is the transpose of the one of fig. There are two important applications of convolution transpose.Sh ). Convolution transpose can be expressed in closed form in the following rather unwieldy expression (derived in section 6. 4.Sh ) q(W . and output tensors. − 1−j 0 +q(j 00 +Pw . Note that. the convolution transpose filters can be seen as being applied with maximum input padding (more would result in zero output values). differently from the forward convolution fig. 4. COMPUTATIONAL BLOCKS C.2): 0 0 D q(H X X . be the input tensor.

4. For max pooling.f2Sh +1 .3. A similar formula holds true for the width. vl_nnpool sup- ports padding the input.3 along with an expression for the receptive field of the operator.1≤j 0 ≤W 0 00 00 resulting in an output of size y ∈ RH ×W ×D . until we hit i00 + Sh . for sum pooling. .. similar to the convolution operator of sec- tion 4. the filter weights are now shifted by one place to the right: f2 . Here the cycle restarts after shifting x to the right by one place.k. assume that the crop parameters are zero. . Similar to the convolution operator of section 4. The height of the output array y is given by H 00 = Sh (H − 1) + H 0 − Ph− − Ph+ . fSh +1 . . Effectively. however.d . (Ph− . Note also that filter k is stored as a slice f:. subsampled with a step of Sh . convolution transpose works as an interpolating filter. Consider a 1D slice in the vertical direction. Consider the output sample yi00 where the index i00 is chosen such that Sh divides i00 − 1.1). 4.4. . due to the rounding in the quotient operation q(i00 .d .1.. Ph+ . Sum-pooling computes the average of the values instead: 1 X yi00 j 00 d = xi00 +i0 −1.2). this sample is obtained as a weighted summation of xi00 /Sh . These formulas are derived in section 5. and that Sh > 1. . The weights are the filter elements f1 . this is equivalent to extending the input data with −∞. Ph− . . Padding and stride. 1≤i0 ≤H 0 . the effect is different from padding in the convolutional block as pooling regions straddling the image boundaries are cropped.j 00 +j 0 −1. but the normalization factor at the boundaries is smaller to account for the smaller integration area. W 0H 0 1≤i0 ≤H 0 . this is similar to padding with zeros. Sw ) are the vertical and horizontal input upsampling factors. this output sample is obtained as a weighted combination of the same elements of the input x that were used to compute yi00 . . Ph+ ) the output crops. i00 + 3.1.3 Spatial pooling vl_nnpool implements max and sum pooling. Sh ). .: of the 4D tensor f . xi00 /Sh −1 .3.f2Sh . We now illustrate the action of convolution transpose in an example (see also fig. The max pooling operator computes the max- imum response of each feature channel in a H 0 × W 0 patch yi00 j 00 d = max xi00 +i0 −1. . SPATIAL POOLING 31 (Sh . Now consider computing the element yi00 +1 . fSh . . and x and f are zero-padded as needed in the calculation. The same is true for i00 + 2. however.1≤j 0 ≤W 0 Detailed calculation of the derivatives is provided in section 6. .:. according to (4. (note that the order is reversed).j 00 +j 0 −1. .

as follows: H X X W yi00 j 00 c = xijc max{0. correspond to performing bilinear interpolation. H 0 . W 0 . v− . . W 0 ) tiles along the vertical and horizontal directions.5 Spatial bilinear resampling vl_nnbilinearsampler uses bilinear interpolation to spatially warp the image according to an input transformation grid. the grid coordinates are expressed not in pixels. j 0 = 1. . 1 + e−xijd See section 6. 1]2×H ×W .32 CHAPTER 4.4 for implementation details. u+ . . αu = . Sigmoid.4 Activation functions MatConvNet supports the following activation functions: ReLU. A region is a rectangular region R = (u− . j 00 ). i0 = 1. vl_nnsigmoid computes the sigmoid : 1 yijd = σ(xijd ) = . g2i00 j 00 ). the output yi00 j 00 c at the location (i00 . y ∈ RH ×W . . This operator works with an input image x. . The edges of the tiles have coordinates vi0 = v− + (v+ − v− + 1)(i0 − 1). is a weighted sum of the input values xijc in the neighborhood of location (g1i00 j 00 . and an output image y as follows: 0 0 0 0 ×C x ∈ RH×W ×C . . βv = − . xijd }. The region itself is partitioned into (H 0 . 1 − |αv g1i00 j 00 + βv − i|} max{0.2). The weights. βu = − . The same transformation is applied to all the features channels in the input. . (4. but relative to a reference frame that extends from −1 to 1 for all spatial dimensions of the input image.2) i=1 j=1 where. this is given by choosing the coefficients as: H −1 H +1 W −1 W +1 αv = . 4. COMPUTATIONAL BLOCKS 4. . uj 0 = u− + (u+ − u− + 1)(j 0 − 1). v+ ).5 for implementation details. Furthermore. as given in (4. 2 2 2 2 See section 6. for each feature channel c. 1 − |αu g2i00 j 00 + βu − j|}. a grid g. vl_nnrelu computes the Rectified Linear Unit (ReLU): yijd = max{0. g ∈ [−1. .6 Region of interest pooling The region of interest (ROI) pooling block applies max or average pooling to specified sub- windows of a tensor. 4.

Each region pools one of the T images in the batch stored in x ∈ RH×W ×C×T . if there is a single tile (H 0 = W 0 = 1).2 Batch normalization vl_nnbnorm implements batch normalization [5]. . H − 1. For compatibility with [3]. 0. .7 Normalization 4. . t∈G(k) where. W } × {1. This oper- ator is applied independently at each spatial location and to groups of feature channels as follows: −β X yijk = xijk κ + α x2ijt . 0 0 The output is a y ∈ RH ×W ×C×K tensor. 2) there can be a pixel of overlap between them and 3) the discretization has a slight bias towards left-top pixels. In more details.4. then the region (0. . 4. . D} is a corresponding subset of input channels. In fact.7. these use (0. Note also that. See section 6. Regions are therefore specified as a tensor R ∈ R5×K . 1) tiles always contain at least one pixel.6. (i. Batch normalization is somewhat different from other neural network blocks in that it performs computation across images/feature . . . 4. differently from tensor indexes. 2. max can be replaced by the averaging operator. . . the H 0 × W 0 pooling tiles are given by Ωi0 j 0 = {bvi0 c + 1. Note also that the operator is applied uniformly at all spatial locations. G(k) ⊂ {1.j)∈Ωi0 j 0 Alternatively. . . . . . Note that input x and output y have the same dimensions. . The extent of each region is defined by four coordinates as specified above. . where y i0 j 0 c = max xijc . .1 Local response normalization (LRN) vl_nnnormalize implements the Local Response Normalization (LRN) operator.7. due to the discretization details. where the first coordinate is the index of the pooled image in the batch. H}. .7. the region coordinates are rounded to the nearest integer before the definitions above are used. Then the input and output tensors are as follows: 0 0 ×C x ∈ RH×W ×C . for each output channel k. dvi0 +1 e} × {bui0 c + 1. . furthermore. NORMALIZATION 33 Following the implementation of [3]. however.1 for implementation details. dui0 +1 e}. y ∈ RH ×W . 0) as the coordinate of the top-left pixel. W − 1) covers the whole input image: Ω11 = {1. . the input of the block is a sequence of K regions.

3 for implementation details. w. Let T be the batch size. w ∈ RK .3 Spatial normalization vl_nnspnorm implements spatial normalization. The output feature map is given by H W T H W T xijkt − µk 1 XXX 1 XXX yijkt = wk p 2 +bk .j 00 +j 0 −1−b W 0 −1 c.7. Then this is used to normalize the input: 1 yi00 j 00 d = xi00 j 00 d .34 CHAPTER 4.2 for implementation details. First. xijt t=1 e Note that the operator is applied across feature channels and in a convolutional manner at all spatial locations. σk2 = (xijkt −µk )2 . b ∈ RK .4 Softmax vl_nnsoftmax computes the softmax operator: exijk yijk = PD . the factor 1/W 0 H 0 is adjusted at the boundaries to account for the fact that neighbors must be cropped.1≤j 0 ≤W 0 2 2 In practice. The spatial normalization operator acts on different feature channels independently and rescales each input feature by the energy of the features in a local neighbourhood . (1 + αn2i00 j 00 d )β See section 6. 4. y ∈ RH×W ×K×T .7. Softmax can be seen as the combination of an activation function (exponential) and a normalization operator. σk + HW T i=1 j=1 t=1 HW T i=1 j=1 t=1 See section 6.6. . the energy of the features in a neighbourhood W 0 × H 0 is evaluated 1 X n2i00 j 00 d = x2i00 +i0 −1−b H 0 −1 c.4 for implementation details. Note that in this case the input and output arrays are explicitly treated as 4D tensors in order to work with a batch of feature maps.6. The tensors w and b define component-wise multiplicative and additive constants. COMPUTATIONAL BLOCKS maps in a batch (whereas most blocks process different images/feature maps individually). W 0H 0 1≤i0 ≤H 0 . then x. See section 6. 4.d . µk = xijkt .6. y = vl_nnbnorm(x. b) normalizes each channel of the feature map x averaging over spatial locations and batch instances.

x is interpreted as a vector of posterior probabilities p(k) = xk . The `instanceWeights` option can be used to specify the tensor w of weights. we drop the other indices and denote by x and c the slice xij:n and the scalar cij1n . c) = 1 c 6= argmax xc . x is usually the output of a block such as . c) is to compare a prediction x to a ground truth class label c. Log loss or negative posterior log-probability. Note also that. CATEGORICAL LOSSES 35 4.8.8. which can be used as an “ignore” label. which are otherwise set to all ones. c) = 1 [|{k : xk ≥ xc }| ≤ K] . The top-K classification error is zero if class c is within the top K ranked scores: `(x. the loss is undefined. the contribution of different samples are summed together (possibly after weighting) and the output of the loss is a scalar.8. c) = − log xc . (4. k xk = 1.7. c) = wij1n `(xij:n . In this case. (4. Classification error. (4. How- ever. The loss is the negative log-probability of the ground truth class: `(x. (4.3) ijn Here x ∈ RH×W ×C×N and c ∈ {1. Further technical details are in section 6.2 losses useful for binary attribute prediction. For these reasons. Section 4. C over the C classes. unless xc > 0. . the loss is treated as a convolutional operator. The classification error is zero if class c is assigned the largest score and zero otherwise: `(x. vl_nnloss implements the following all of these. C}H×W ×1×N . .1 Classification losses Classification losses decompose additively as follows: X `(x. . Unless otherwise noted. . k = 1.8 Categorical losses The purpose of a categorical loss function `(x.4) k Ties are broken randomly. .5) The classification error is the same as the top-1 classification error. . As in the rest of MatConvNet. vl_nnloss automatically skips all samples such that c = 0.1 losses useful for multi-class classification and the section 4. .8. .4. cij:n ). Top-K classification error. in the sense that the loss is evaluated independently at each spatial location. w has the same dimension as c.6) P Note that this makes the implicit assumption x ≥ 0. 4. such that the slice xij:n represent a vector of C class scores and and cij1n is the ground truth class label.

c) = max{0. the composition of the naive log loss and softmax is numerically unstable. c) = max 0. This loss combines the softmax block and the log-loss block into a single block: C e xc X `(x. instead. 1 − xc }. they are. This. c) = 0 ⇔ xc ≥ 1. just as for the log-loss above. The softmax block makes the score compete through the normalization factor. Softmax log-loss or multinomial logistic loss.2 Attribute losses Attribute losses are similar to classification losses. (4. k6=c Hence yc represent the confidence margin between class c and the other classes k 6= c. 1 − xc + max xk . Multi-class hinge loss. Note that. the score xc should be somehow in competition with the other scores xk . (4. Just like softmax log-loss combines softmax and loss. This loss is implemented also in the deprecated function vl_softmaxloss.6) can trivially be achieved by maxing all xk large.10) ijkn .9) k6=c 4.36 CHAPTER 4.7) k=1 exk k=1 Combining the two blocks explicitly is required for numerical stability.8) Note that `(x. c) = − log PC = −xc + log exk . whereas the intended effect is that xc should be large com- pared to the xk . binary attributes. Generally. but in this case classes are not mutually exclusive. by combin- P with softmax. c) ≈ 0 ing the log-loss when xc k6=c xk . k 6= c. (4. Thus this is implemented as a special case below. also know as Crammer-Singer loss. this loss automatically makes the score compete: `(bx. c) = wijkn `(xijkn . k 6= c. The structured multi-class logistic loss. Attribute losses decompose additively as follows: X `(x. Structured multi-class hinge loss. However. COMPUTATIONAL BLOCKS softmax that can guarantee these conditions. this loss does not automatically make the score competes. The multi-class logistic loss is given by `(x. (4. the loss is usually preceded by the block: yc = xc − max xk .8. In order to do that. minimizing (4. cijkn ). If this is not the case. for such a loss to make sense. combines the multi-class hinge loss with a block computing the score margin: `(x. the next loss combines margin computation and hinge loss.

15) There is a relationship between the hinge loss and the structured multi-class hinge loss which is analogous to the relationship between binary logistic loss and multi-class logistic loss. (4. Unless otherwise noted. This is the same as the multi-class logistic loss. which are oth- erwise set to all ones. +1}H×W ×C×N . Namely. we drop the other indices and denote by x and c the scalars xijkn and cijkn . This is the same as the multi-class log-loss but for binary attributes. 1] is interpreted as the probability that attribute k is on: ( − log x. 1] must be enforced by the block computing x. often set to zero. . c) = max 0.4. Binary log-loss. (4. such that the scalar xijkn represent a confidence that attribute k is on and cij1n is the ground truth attribute label. 1 1 = − log c x − + .11) Here τ is a configurable threshold. `(x. 1 − + max 2 k6=c 2 Hence the hinge loss is the same as the structure multi-class hinge loss for C = 2 classes.13) 2 2 Similarly to the multi-class log loss. but this time x/2 represents the confidence that the attribute is on and −x/2 that it is off. c) = − log σ(cx) = − log = − log cx cx . samples with c = 0 are skipped. the hinge loss can be rewritten as: cx kx `(x.14) 1 + e−cx e 2 + e− 2 Binary hinge loss. c|τ ) = 1[sign(x − τ ) 6= c]. Binary logistic loss.8. w has the same dimension as c. (4. This loss is zero only if the sign of x − τ agrees with the ground truth label c: `(x. c = +1. c = −1. CATEGORICAL LOSSES 37 Here x ∈ RH×W ×C×N and c ∈ {−1. c) = max{0. This is the same as the structured multi-class hinge loss but for binary attributes: `(x. The `instanceWeights` option can be used to specify the tensor w of weights. This is obtained by using the logistic function σ(x) cx 1 e2 `(x. As before. (4. the assumption x ∈ [0. c) = (4. 1 − cx}. Namely. this time xk ∈ [0. where x/2 is the score associated to class c = 1 and −x/2 the score associated to class c = −1. Binary error.12) − log(1 − x).

1 for implementation details. . true it is possible to compute a variant omitting the root: X yij = |xijd − x̄ijd |p .9 Comparisons 4. at each spatial location ij one extracts and compares vectors xij: .38 CHAPTER 4. i. p > 0.9. By specifying the option 'noRoot'. d See section 6.8. COMPUTATIONAL BLOCKS 4.1 p-distance The vl_nnpdist function computes the p-distance between the vectors in the input data x and a target x̄: ! p1 X yij = |xijd − x̄ijd |p d Note that this operator is applied convolutionally.e.

The set Ω(i00 . (5. j 00 ) (note that feature channels are implicitly coalesced in this discussion).j where (i.1 Preliminaries In this section we are interested in understanding how components in a CNN depend on components in the layers before it. called receptive field.g. Therefore. the analysis will be necessarily limited in scope. 5.j 00 depends only on the input components xi. We also assume that all the inputs x and outputs y are in the usual form of spatial maps. We also assume that this relation can be expressed in terms of a sliding rectangular window field.1) 2 2 00 ∆v − 1 ∆v − 1 j ∈ αv (j − 1) + βv + − . Furthermore.2) 2 2 where (αh .k where i. such as for example cropping their inputs based on data-dependent terms (e.j. this information is generally available only at “run time” and cannot be uniquely determined given only the structure of the network. blocks can implement complex operations that are difficult to characterise in simple terms. (βh .d.Chapter 5 Geometry This chapter looks at the geometry of the CNN input-output mapping. j) ∈ Ω(i00 . ∆v ) the receptive field size. βv ) the offset. Consider a layer y = f (x). j are spatial coordinates. Fast R-CNN). We are interested in establishing which components of x influence which components of y. and therefore indexed as xi. We consider blocks such as convolutions for which one can deterministically establish dependency chains between network components. (5. and (∆h . αv ) is the stride. 39 . j 00 ) is a rectangle defined as follows: 00 ∆h − 1 ∆h − 1 i ∈ αh (i − 1) + βh + − . and in particular on components of the input. Since CNNs can incorporate blocks that perform complex operations. This means that the output component yi00 .

In the library Caffe. stride.2. ∆h . 2 A similar relation holds for the horizontal direction. most loss functions etc. In addition to computing the receptive field geometry. and padding. such that H 0 = Sh = 1 and Ph− = Ph+ = 0. αv . 2 2 2 Hence H0 + 1 αh = Sh . . the last application of the pooling window has index i00 = H 00 such that H − H0 00 0. H 00 where H 00 is the largest value of i00 before the receptive fields falls outside x (including padding). Here the value yi00 depends on the samples: H0 − 1 H0 − 1 H0 + 1 − 0 00 xi : i ∈ [1. ReLU. More formally.1 Pooling in Caffe MatConvNet treats pooling operators like filters. Note that in this case αh = 1. ∆h = H 0 . the pooling window is allowed to shift enough such that the last application always includes the last pixel of the input. this means that the last application of the pooling window can be partially outside the input boundaries even if padding is “officially” zero. LNR. and once more reasoning for a 1D slice. ReLU can be considered a 1 × 1 filter. GEOMETRY 5. In the case of filters. 2. H ] + Sh (i − 1) − Ph = − . Sh the subampling stride. we get the condition H 0 + Sh (H 00 − 1) − Ph− ≤ H + Ph+ . we are often interested in determin- ing the sizes of the arrays x and y throughout the architecture. . If H is the height of the input array x. βh = 1 and ∆h = 1. It suffices to reason in 1D.40 CHAPTER 5.g.) have a filter-like receptive field geometry. if H 0 is the pool size and H the size of the signal. Note that many blocks (e. + Sh (i00 − 1) − Ph− + . Let H 0 bet the vertical filter dimension. creating some incompatibilities. this is done slightly differently. we notice that yi00 can be obtained for i00 = 1. . namely filters. For example. ∆v ) for the most common operators. We consider in particular simple filters that are characterised by an integer size. In their case. Hence H − H 0 + Ph− + Ph+ 00 H = + 1. βh . using the rules above. βh = − Ph− . βv . If the stride is greater than one. and Ph− and Ph+ the amount of zero padding applied to the top and the bottom of the input x.3) Sh 5. max pooling.2 Simple filters We now compute the receptive field geometry (αh . (5. .

00 .

Sh If there is padding. Sh . Sh (i − 1) + H i00 =H 00 ≥ H ⇔ H = + 1. such that the output has height: H − H 0 + Ph− + Ph+ 00 H = + 1. the same logic applies after padding the input image.

Using the formula above. but with the ceil instead of floor operator. Unfortunately. Note that in practice Ph− = Ph+ = Ph since Caffe does not support asymmetric padding.2. it gets more complicated. it can happen that the last padding application is completely outside the input image and Caffe tries to avoid it. This requires H − 1 + Ph− Sh (i00 − 1) − Ph− + 1.5. SIMPLE FILTERS 41 This is the same formula as for above filters.

i00 =H 00 ≤ H H 00 ≤ .

Ph− ≤ H 0 − 1. ⇔ + 1. Caffe has a special provision for that and lowers H 00 by one when needed. Sh Sh Sh Hence if Ph+ + Sh ≤ H 0 then the second term is less than zero and (5.4) is satisfied. we see that if Ph+ = 0 and Sh ≤ H 0 (which is often the case and may be assumed by Caffe). Next. Caffe assumes that Ph+ . we find MatConvNet equivalents for these parameters. Furthermore.4) Sh Using the fact that for integers a. one has da/be = b(a + b − 1)/bc. then the equation is also satisfied and Caffe skips the check. we can rewrite the expression for H 00 as follows H − H 0 + Ph− + Ph+ H − 1 + Ph− Ph+ + Sh − H 0 00 H = +1= + + 1. for any choices of Ph+ and Sh allowed by Caffe. b. the last sample of the last filter application has to be on or to the right of the last Caffe-padded pixel: H − H0 + P − + P + Sh h h + 1 −1 + H 0 ≥ H + 2P − . Assume that Caffe applies a symmetric padding Ph . (5. Then in MatConvNet Ph− = Ph to align the top part of the output signal.4) by at most one unit. the formula above may violate constraint (5. To match Caffe. Hence. Sh | {z h} | {z } Caffe rightmost input sample with padding MatConvNet rightmost pooling index | {z } MatConvNet rightmost pooled input sample Rearranging H − H 0 + Ph− + Ph+ H − H 0 + 2Ph− ≥ Sh Sh Using ba/bc = d(a − b + 1)/be we get the equivalent condition: H − H 0 + 2Ph− Ph+ − Ph− − Sh + 1 H − H 0 + 2Ph− + ≥ Sh Sh Sh . In practice. Sh Sh We conclude that. we can upper bound the second term: Ph+ + Sh − H 0 Sh − 1 ≤ ≤ 1. as otherwise the first filter application falls entirely in the padded region.

so that Ph+ = min{Ph− + Sh − 1. it is not possible to recover H 00 uniquely from this expression. We can however relax these two relations (hence obtaining a slightly larger receptive field) and conclude that 1 2Ch− − H 0 + 1 H0 − 1 αh = . ∆h = + 1. would result in a right MatConvNet padding of Ph+ = 1. βh = + 1. Uh Uh Note that.5) where cropping becomes padding and upsampling becomes downsampling. top and bottom crops. this may still be too much padding. Uh 2Uh Uh Next. Hence there is an interaction between xi and yi00 only if 1 + Uh (i − 1) − Ch− ≤ i00 ≤ H 0 + Uh (i − 1) − Ch− (5. and filter height. let xi be the input to the convolution transpose block and yi00 its output. Furthermore let Uh . instead. Turning this relation around. studied in section 5. Uh If H is now given as input. . Recall that convolution transpose (section 6. Swapping input and output in (5. causing the last pool window application to be entirely in the rightmost padded area. GEOMETRY Removing the ceil operator lower bounds the left-hand side of the equation and produces the sufficient condition Ph+ ≥ Ph− + Sh − 1. the data dependencies are the same as for the convolution operator. For example. but somewhat more complex. 4. we find that i + Ch− − H 0 i + Ch− − 1 00 00 +1≤i≤ + 1. H 0 − 1}. which in turn is a filter. MatConvNet places the restriction Ph+ ≤ H 0 − 1. Reasoning for a 1D slice. Ch+ and H 0 be the upsampling factor.2). due to rounding. it is not possible to express this set tightly in the form outlined above.2) is the transpose of the convolution operator.3 Convolution transpose The convolution transpose block is similar to a simple filter. As before.2.3) results in the constraint: H − H 0 + Ch− + Ch+ 00 H =1+ . respectively. from the output to the input (see also fig. If we look at the convolution transpose backward. a pooling region of width H 0 = 3 samples with a stride of Sh = 1 samples and null Caffe padding Ph− = 0. Ch− . we want to determine the height H 00 of the output y of convolution transpose as a function of the heigh H of the input x and the other parameters. 5.42 CHAPTER 5. all the following values are possible Uh (H − 1) + H 0 − Ch− − Ch+ ≤ H 00 < Uh H + H 0 − Ch− − Ch+ .

Sh Sh Hence we find again the formulas obtained in section 5. we choose the tighter definition and set H 00 = Uh (H − 1) + H 0 − Ch− − Ch+ . by definition of receptive fields. βh . in the reverse direction. 4. 5. then xi depends on yi00 due to g.3. ∆h = H 0 . By this. if yi00 depends on xi due to f . and since the extra samples would be ignored by the computation and stay zero. TRANSPOSING RECEPTIVE FIELDS 43 This is due to the fact that Uh acts as a downsampling factor in the standard convolution direction and some of the samples to the right of the convolution input y may be ignored by the filter (see also fig. Now suppose we are given a block x = g(y) which is the “transpose” of f . 2 αh αh 2αh Hence.2). β̂h = . ∆ αh αh αh Example 1.4 Transposing receptive fields Suppose we have determined that a later y = f (x) has a receptive field transformation (αh . we have found the parameters: H0 + 1 αh = S h . 4. For convolution. just like the convolution transpose layer is the transpose of the convolution layer. . this corresponds to a RF transformation 1 1 + αh − βh ˆ h = ∆h + αh − 1 . we can obtain the RF transformation for convolution transpose: 1 1 α̂h = = . which can be rewritten as ∆h − 1 ∆h − 1 − ≤ i − αh (i00 − 1) − βh ≤ . i00 ) given by (5. ∆h ) (along one spatial slice).4. Note that.5.1 and fig. Since the height of y is then determined up to Sh samples. 2 Using the formulas just found.1). βh = − Ph− . α̂h = . αh Sh 1 + Sh − (H 0 + 1)/2 + Ph− P − − H 0 /2 + 1/2 2P − − H 0 + 1 β̂h = = h +1= h + 1. we mean that. 2 2 A simple manipulation of this expression results in the equivalent expression: (∆h + αh − 1)/αh − 1 1 1 + αh − βh (∆h + αh − 1)/αh − 1 − ≤ i00 − (i − 1) − ≤ . f relates the inputs and outputs index pairs (i. Sh Sh Sh ˆ H 0 + Sh − 1 H0 − 1 ∆h = = + 1.

44 CHAPTER 5. we see that the first and last sample if in the domain of g ◦ f to affect ih are: ∆g − 1 ∆f − 1 if = αf αg (ih − 1) + βg ± − 1 + βf ± 2 2 αf (∆g − 1) + ∆f − 1 = αf αg (ih − 1) + αf (βg − 1) + βf ± . The goal is to determine the combined receptive field. the first and last sample ig to affect a given output sample ih are given by ∆g − 1 ig = αg (ih − 1) + βg ± . βg . Suppose that this gives receptive fields (αhf .e. The first and last sample if in the domain of f to affect ig are given by: ∆f − 1 if = αf (ig − 1) + βf ± . βhf . βh = αf (βg − 1) + βf . βhg + . ∆f ) and (αg . ∆hg ) respectively. 2 2 ∆hf − 1 ∆hg − 1 imin = α(ih − 1) + b. Only in this case. a = min βhf − . in fact. the horizontal one being the same). βf . b = max βhf + . βhg − . 5. Given the rule above. ∆h = αf (∆g − 1) + ∆f . To do so. 2 2 We conclude that the combined receptive field is a+b α = αhg = αhf . ∆g ) (once again we consider only a 1D slice in the vertical direction. x = x1 = x2 . We say that these two receptive fields are compatible. The goal is to compute the receptive field of h. g(x2 )) where the domains of f and g are the same. it is possible to compute how each output sample ih depends on each input sample if through f and on each input sample ig through g. The range of input samples i = if = ig that affect any output sample ih is then given by ∆hf − 1 ∆hg − 1 imax = α(ih − 1) + a.5 Composing receptive fields Consider now the composition of two layers h = g ◦ f with receptive fields (αf . ∆ = b − a + 1. 2 Likewise. GEOMETRY 5. i. and only if. α = αhg = αhf .1). ∆hf ) and (αhg . Now assume that the domain of f and g coincide. βhg . 2 .6 Overlaying receptive fields Consider now the combination h(f (x1 ). pick a sample ig in the domain of g. 2 We conclude that αh = αf αg . This is only possible if. 2 Substituting one relation into the other. it is possible to find a sliding window receptive field that tightly encloses the receptive field due to g and f at all points according to formulas (5. β= .

vec(φ∗ (M )) = H > vec(M ). 0 0 where F ∈ R(H W D)×K is the matrix obtained by reshaping the array f and I is an identity matrix of suitable dimensions.d) Note that φ and φ∗ are linear operators. In practice. d) and (p. j. It is also useful to define the “transposed” operator row2im: X [φ∗ (M )]ijd = Mpq . equivalently. d) = t(p.d)=t(p. this map is slightly modified to account for the padding. this operator is given by: [φ(x)]pq = xijd (i. extracting all W 0 × H 0 patches from the map x and storing them as rows of a (H 00 W 00 ) × (H 0 W 0 D) matrix.j. and dilation factors. j.1 Convolution It is often convenient to express the convolution operation in matrix form. Hence we obtain the following expression for the vectorized output (see [7]): ( (I ⊗ φ(x)) vec F. To this end. q) where: i = i00 + i0 − 1.j. let φ(x) be the im2row operator. q) is given by the map (i. This allows obtaining the following formulas for the deriva- tives: > dz dz > dz = (I ⊗ φ(x)) = vec φ(x) d(vec F )> d(vec y)> dY 45 .q)∈t−1 (i. p = i00 + H 00 (j 00 − 1). Both can be expressed by a matrix H ∈ 00 00 0 0 R(H W H W D)×(HW D) such that vec(φ(x)) = H vec(x). j = j 00 + j 0 − 1.Chapter 6 Implementation details This chapter contains calculations and details. Formally. vec y = vec (φ(x)F ) = > (F ⊗ I) vec φ(x). (p.q) where the correspondence between indexes (i. q = i0 + H 0 (j 0 − 1) + H 0 W 0 (d − 1). stride. or. 6.

let y to be obtained from x by the convolution operator as defined in section 4. i0 =1 h S where S is the downsampling factor. Notably. While this is simple to describe in term of matrices. H]. it can be rewritten as vec y = M vec x for a suitable matrix M .46 CHAPTER 6. In order to derive an expression of the convolution transpose. H the length of the input signal x and H 0 the length of the filter f . +2−q h k=−∞ q=−∞ S . convolution transpose computes instead vec x = M > vec y. the index of the input data x may exceed the range [1. start from standard convolution (for a 1D signal): H 0 H − H 0 + Ph− + Ph+ X 00 yi00 = fi0 xS(i00 −1)+i0 −P − . Likewise: > dz dz > d vec φ(x) dz > = (F ⊗ I) = vec F H d(vec x)> d(vec y)> d(vec x)> dY In summary. = φ(x)> . it is instead a fast approach when the number of filters is large and it allows leveraging fast BLAS and GPU BLAS implementations. after reshaping these terms we obtain the formulas: dz dz dz dz > vec y = vec (φ(x)F ) .1 (including padding and downsampling). we implicitly assume that the signal is zero padded outside this range.2 Convolution transpose In order to understand the definition of convolution transpose. = φ∗ F dF dY dX dY where X ∈ R(HW )×D is the matrix obtained by reshaping x. In order to derive a formula for the convolution transpose. we make use of the identity vec y> (M vec x) = (vec y> M ) vec x = vec x> (M > vec y). what happens in term of indexes is tricky. IMPLEMENTATION DETAILS 00 W 00 )×K where Y ∈ R(H is the matrix obtained by reshaping the array y. Ph− and Ph+ the padding. these expressions are used to implement the convolutional operator. while this may seem inefficient. Expanding this in formulas: b W 0 +∞ +∞ X X X X yi00 fi0 xS(i00 −1)+i0 −P − = yi00 fi0 xS(i00 −1)+i0 −P − h h i00 =1 i0 =1 i00 =−∞ i0 =−∞ +∞ X +∞ X = yi00 fk−S(i00 −1)+P − xk h i00 =−∞ k=−∞ +∞ X +∞ X = yi00 f k−1+P − xk (k−1+Ph− ) mod S+S 1−i00 + S h +1 i00 =−∞ k=−∞ +∞ X +∞ X = xk y k−1+Ph− f(k−1+P − ) mod S+S(q−1)+1 . Due to padding. Since this is a linear operation. 6. 1≤i ≤1+ .

there could be an element less). . S S The size H 00 of the output of convolution transpose is obtained in section 5. depending on k. likewise. (6.3 Spatial pooling Since max pooling simply selects for each output element an input element. . where the operator is not differentiable (this usually does not pose problems in optimization by stochastic gradient).1) +2−q h q=1 S Note that the summation extrema in (6.1 ReLU The ReLU operator can be expressed in matrix notation as dz dz vec y = diag s vec x.4 Activation functions 6. In summary.2) d vec x d vec y 6. similar relations exists with two differences: S does not depend on the input x and it is not binary. SPATIAL POOLING 47 Summation ranges have been extended to infinity by assuming that all signals are zero padded as needed. For max-pooling.1) can be refined slightly to account for the finite size of y and w: k − 1 + Ph− 00 max 1. 1}(H W D)×(HW D) . 1}HW D is an indicator vector. = S(x)> . the relation can be expressed in matrix form as vec y = S(x) vec x for a suitable selector matrix S(x) ∈ 00 00 {0. +2−H ≤q S H − 1 − (k − 1 + Ph− ) mod S k − 1 + Ph− 0 ≤ 1 + min . we have the expressions: dz dz vec y = S(x) vec x. . (6. 6. . = diag s d vec x d vec y where s = [vec x > 0] ∈ {0. q ≥ 1 is the minimum value of q for which the filter f is non zero. q ≤ b(H 0 − 1)/Sc + 1 is a fairly tight upper bound on the maximum value (although. The derivatives can be written as: d(vecdzx)> = d(vecdzy)> S(x).3. . .3. k = 1. H] (since this is the range of elements of x involved in the original convolution).6. In order to recover such ranges.4. note that k ∈ [1. Hence 0 1+b H S−1 c X xk = y k−1+Ph− f(k−1+P − ) mod S+S(q−1)+1 . Furthermore. for all but a null set of points. H. in order to account for the normalization factors.

48 CHAPTER 6. IMPLEMENTATION DETAILS 6. dyijd In matrix notation: dz dz = .2 Sigmoid The derivative of the sigmoid function is given by dz dz dyijd dz −1 = = (−e−xijd ) dxijd dyijd dxijd dyijd (1 + e−xijd )2 dz = yijd (1 − yijd ).4.

y .

4) A similar expression holds for ∂g2i0 j 0 6. 1 − |αv g1i00 j 00 + βv − i|} max{0. 4. φ(x. with the difference that summation is on i00 rather than i.6 Normalization 6. 1 − |αu g2i00 j 00 + βu − j|} ∂g1i0 j 0 i00 j 00 c i=1 j=1 X H X X W =− p i0 j 0 c αv xijc max{0. 1 − |αv g1i00 j 00 + βv − i|} max{0. dx dy 6. (11> − y). g)i/dx of the spatial bilinaer resampler operator with respect to the input image x can be found as follows: " H X W # ∂ X X pi00 k00 c00 xi0 j 0 c00 max{0. 1−|αv g2i0 j 0 +βv −j|} sign(αv g1i0 j 0 +βv −j)1{−1<αu g2i0 j0 +βu <1} . 1 − |αu g2i00 j 00 + βu − j 0 |} ∂xijc i00 j 00 c00 i0 =1 j 0 =1 X = pi00 k00 c max{0. 1 − |αv g1i00 j 00 + βv − i0 |} max{0. c i=1 j=1 (6. 1 − |αu g2i00 j 00 + βu − j|}.5 Spatial bilinear resampling The projected derivative dhp. j. φ(x. (6. j. d|x)−β − 2αβxijd L(i. k|x) = κ + α x2ijt .3) i00 j 00 Note that the formula is similar to Eq. t∈G(k) . The projected derivative dhp.2.1 Local response normalization (LRN) The derivative is easily computed as: dz dz X dz = L(i. g)i/dg with respect to the grid is similar: " H X W # ∂ X X pi00 k00 c xijc max{0.6. j. k|x)−β−1 xijk dxijd dyijd dyijk k:d∈G(k) where X L(i.

all terms with k 00 6= k are zero. dxijkt i00 j 00 k00 t00 dyi00 j 00 k00 t00 dxijkt Since feature channels are processed independently.6.6.t=t0 − = (xi0 j 0 kt0 − µk ) . ! dz wk dz 1 X dz =p 2 − dxijkt σk + dyijkt HW T i00 j 00 kt00 dyi00 j 00 kt00 wk xijkt − µk 1 X dz xi00 j 00 kt00 − µk −p 2 p p .j=j 00 .t=t00 − p − (x i 00 j 00 kt00 − µk ) σ + k . dxijkt i00 j 00 t00 dyi00 j 00 kt00 dxijkt where − 32 dσk2 dyi00 j 00 kt00 dµk 1 wk 2 = wk δi=i00 . dwk i00 j 00 k00 t00 dyi00 j 00 k00 t00 dwk 00 00 i j t 00 dyi00 j 00 kt00 σk 2 + dz X dz dyi00 j 00 k00 t00 X dz = = .j=j 0 . dbk i00 j 00 k00 t00 dyi00 j 00 k00 t00 dwk 00 00 i j t 00 dyi00 j 00 kt00 The derivative of the network output z with respect to the block input x is computed as follows: dz X dz dyi00 j 00 k00 t00 = .6.e. σk + σk2 + HW T i00 j 00 kt00 dyi00 j 00 kt00 σk2 + . NORMALIZATION 49 6. Hence ! dz wk dz 1 X dz =p 2 − dxijkt σk + dyijkt HW T i00 j 00 kt00 dyi00 j 00 kt00 wk X dz 2 − 3 (xi00 j 00 kt00 − µk ) (xijkt − µk ) 2(σk + ) 2 i00 j 00 kt00 dyi00 j 00 kt00 2 HW T i. dxi0 j 0 kt0 HW T ijt HW T HW T and δE is the indicator function of the event E.2 Batch normalization The derivative of the network output z with respect to the multipliers wk and biases bk is given by dz X dz dyi00 j 00 k00 t00 X dz xi00 j 00 kt00 − µk = = p . Hence dz X dz dyi00 j 00 kt00 = . dxijkt dxijkt σk2 + 2 dxijkt the derivatives with respect to the mean and variance are computed as follows: dµk 1 = . dxijkt HW T dσk2 2 X 1 2 = (xijkt − µk ) δi=i0 .

3 Spatial normalization The neighbourhood norm n2i00 j 00 d can be computed by applying average pooling to x2ijd using 0 vl_nnpool with a W 0 ×H 0 pooling region. bottom padding H 0 −b H−1 2 c−1. The derivative of spatial normalization can be obtained as follows: dz X dz dyi00 j 00 d = dxijd i00 j 00 d dyi00 j 00 d dxijd X dz dxi00 j 00 d dz dn2i00 j 00 d dx2ijd = (1 + αn2i00 j 00 d )−β − αβ (1 + αn2i00 j 00 d )−β−1 xi00 j 00 d i00 j 00 d dyi00 j 00 d dxijd dyi00 j 00 d d(x2ijd ) dxijd " # 2 dz X dz dn 00 00 i j d = (1 + αn2ijd )−β − 2αβxijd (1 + αn2i00 j 00 d )−β−1 xi00 j 00 d dyijd i00 j 00 d dyi j d 00 00 d(x2ijd ) " # 2 dz X dn i 00 j 00 d dz = (1 + αn2ijd )−β − 2αβxijd ηi00 j 00 d 2 .6. and similarly for the horizontal padding. dxijd dyijd k=1 dyijk In matrix form: dz dz dz > =Y .50 CHAPTER 6. dxijkt σk + dyijkt HW T dbk σk + HW T dwk 6. top padding b H 2−1 c. IMPLEMENTATION DETAILS We can identify some of these terms with the ones computed as derivatives of bnorm with respect to wk and µk : ! dz wk dz 1 dz xijkt − µk 1 dz =p 2 − − p 2 . e xijt −maxd xijd t=1 The derivative is given by: D dz X dz X exijd L(x)−1 δ{k=d} − exijd exijk L(x)−2 . exijt . 6.4 Softmax Care must be taken in evaluating the exponential in order to avoid underflow or overflow. = L(x) = dxijd k dyijk t=1 Simplifying: ! K dz dz X dz = yijd − yijk . The simplest way to do so is to divide the numerator and denominator by the exponential of the maximum value: exijk −maxd xijd yijk = PD . ηi00 j 00 d = (1 + αn2i00 j 00 d )−β−1 xi00 j 00 d dyijd i00 j 00 d d(x ijd ) dy 00 i j d00 Note that the summation can be computed as the derivative of the vl_nnpool block.6.

− .

Y 11 dX dY dY .

. The projected derivative is: ∂p`(x.7. c) ∂ log(xc ) = −p = −pxc δk=c . Log-loss.7. so the projection tensor p is trivial (a scalar).e. Recall that all losses give a scalar output. ∂xk Multi-class hinge loss. c) = PC . 6. ∂xk ∂xk Softmax log-loss.6. Note that the numerical implementation of this expression is straightforward once the output Y has been computed with the caveats above. The projected derivative is: ∂p`(x.2. we can recognize the output of the loss itself: exc y = `(x. ∂xk t6=c t=1.. The projected derivative is: ∂p`(x.. The derivative is zero a. ∂xk Structured multi-class hinge loss.7. The derivative of the binary error is 0 a.C 6. ∂xk ∂xk t=1 t=1 e xt In brackets. Y ∈ RHW ×D are the matrices obtained by reshaping the arrays x and y. 6. c) = −p (δk=c − y) .8. c) ∂ X e = −p xc − log ext = −p δk=c − PC . c) = −p 1[xc < 1] δk=c .. xt t=1 e Hence the loss derivatives rewrites: ∂p`(x.2 Attribute losses Binary error.e.1 Classification losses Top-K classification error. t∗ = argmax xt .7 Categorical losses This section obtains the projected derivatives of the categorical losses in section 4. The projected derivative is given by: C ! ! xc ∂p`(x. CATEGORICAL LOSSES 51 where X. c) = −p 1[xc < 1 + max xt ] (δk=c − δk=t∗ )..

1 p-distance The derivative of the operator without root is given by: dz dz = p|xijd − x̄ijd |p−1 sign(xijd − x̄ijd ). K − 1 : Rk = [f (xk ). ∂x 6. dyij yijp−1 dz dz =− .1 Normal sampler The function vl::randn() uses the Ziggurah method [10] to sample from a Normally- distributed random variable. 2 which are therefore implemented as special cases. c) ∂ 1 ce−cx c = −p log −cx = −p −cx = −p cx = −pc σ(−cx). IMPLEMENTATION DETAILS Binary log-loss. f (xk+1 )] × [0. . c x − 12 + 12 ∂x Binary logistic loss. The projected derivative is: ∂p`(x.8 Comparisons 6.9. . These are defined by points x1 > x2 > x3 > · · · > xK = 0 such that (for the right half of f (x)) the layers of the Ziggurat are given by ∀k = 1. xk ]. c) = −pc 1[cx < 1]. 6. .52 CHAPTER 6. The projected derivative is: ∂p`(x.9 Other implementation details 6.8. dxijd dyij The derivative of the operator with root is given by: ! p1 −1 dz dz 1 X = |xijd0 − x̄ijd0 |p p|xijd − x̄ijd |p−1 sign(xijd − x̄ijd ) dxijd dyij p d0 dz |xijd − x̄ijd |p−1 sign(xijd − x̄ijd ) = . dx̄ijd dxijd The formulas simplify a little for p = 1. ∂x ∂x 1+e 1+e e +1 Binary hinge loss. Let f (x) = √12π exp − 21 x2 the standard Normal distribution. c) c = −p . The projected derivative is ∂p`(x. . . The sampler encloses f (x) in a simple shape made of K − 1 horizontal rectangles and a base composed of a rectangle tapering off in an exponential distribution.

As before. we can implement this by sampling u uniformly in (0. we can express this as sampling x = ux0 and accepting the latter as a sample from the rectangle component if ux0 ≤ x1 . (half of) the distribution is enclosed by the Ziggurat. Given K. here the pseudo-point x0 is defined such that x1 /x0 = f (x1 )x1 /A. ∀x ≥ 0 : (x. the test y ≤ f (xk+1 ) < f (x) is always successful. f (x1 ) exp(−x1 (x − x1 ))]. x ≥ x1 . when x ≤ xk+1 . 1] and setting x = x1 − (1/x1 ) ln u. since the last point xK = 0. x1 ]) ∪ {(x.6. Given x1 . for k ≥ 1. The other points are defined recursively such that the area is the same for all rectangles: A = |Rk | = (f (xk+1 ) − f (xk ))xk ⇒ xk+1 = f −1 (A/xk + f (xk )). . . we sample the corresponding y uniformly at random in the interval [0. let z = x1 exp(−x1 (x − x1 )). A and all other points in the sequence can be derived easily using the formulas above. enclosing tightly f (x). y > f (x). otherwise. Note that. y) uniformly from the Ziggurat. this requires sampling a pair (x. v in the interval [0. set x = uxk and y = vf (xk+1 ) + (1 − v)f (xk ) (for k = 0 see below). Next. Since all regions Rk have the same area and (x. K − 1} and two scalars u. sample again. we complete the procedure for k = 0. then x = x1 − (1/x1 ) ln z/x1 and dx = | − (x1 /z)|dz. f (x1 )] × [0.e. Since x1 exp(−x1 (x − x1 ))dx = (1/x1 )dz is uniform.9.655420419026953. Sample a point (x. y) under the exponential distribution graph. one first samples a point (x. To sample from R0 uniformly. Then. and the variable y and test can be skipped in the step above. f (x)) ∈ ∪K k=0 Rk . we either choose the rectangle or the exponential distribution with a probability proportional to their area. not the exponential. The Ziggurath can be used to quickly sample from the Normal distribution. the goal is to choose x1 such that the K-th points xK = 0 lands on zero.e. x0 = A/f (x1 ). There are two degrees of freedom: the number of subdivisions K and the point x1 . y) are then drawn uniformly form the selected rectangle. . y) : x ≥ x1 . where z ∈ (0. recall that the goal is to sample from the Normal distribution. this samples a point (x. To do so. i. y) that do not belong to the graph of f (x). i. Finally. so the latter sample must be refined by rejection sampling. To do so. when R0 is not just a rectangle but rather the union of a rectangle and an exponential distribution. y ≤ f (x1 ) exp(−x1 (x − x1 ))} Note that. OTHER IMPLEMENTATION DETAILS 53 and such that its basis is given by R0 = ([0. The required value of x1 is easily found using bisection and. The first point x1 in the sequence determines the area of the Ziggurat base: A = |R0 | = f (x1 )x1 + f (x1 )/x1 . 1. Specifically: 1. we sample instead from the exponential distribution x ∼ x1 exp(−x1 (x − x1 )). for K = 256. x1 ]. 1). In order to do so. Given x sampled from the exponential distribution. . 2. i.e. y) uniformly at random from the Ziggurat ∪K k=0 Rk and then rejects pairs (x. . If the test fails. accept x as a sample. is x1 = 3. If y ≤ f (x). sample uniformly at random an index k ∈ {0. y) from the Ziggurat uniformly at random. Reusing the notation (and corresponding code) above.

we can show that. The latter is then accepted provided that y is below the Normal distribution graph f (x). A short calculation yields the test: −2 ln v ≥ x21 + x2 − 2x1 x = (x1 − x)2 = ((1/x1 ) ln u)2 . b) = max{d ∈ N : d|a ∧ d|b}. b). if d divides a − qb as well as b. where v is uniform in [0. we replace a = b(a/b) + r as above: a ru + bv 0 = gcd(a.2 Euclid’s algorithm Euclid’s algorithm finds the greatest common divisor (GCD) of two non-negative integers a and b. because gcd(a. . Let a. b) = b ⇒ v 0 = b. b The recursion terminates when r = 0. To do so. The recursion terminates when a zero reminder is generated. b). b ∈ N and let q ∈ Z such that a − qb ≥ 0. the GCD of a and b is the same as the GCD of the remainder r = a − qb = (a mod b) and b: gcd(a. the Bézout’s coefficients. in which case bv 0 = gcd(0.e. d d |{z} d |{z} d ∈Z ∈Z Hence gcd(a. v 0 = u + v. b) = gcd(a. b). now GCD is called with smaller arguments.54 CHAPTER 6. Let d be a divisor of both a and b. 0) = a. Lemma 1 (Euclid’s algorithm). Then gcd(a. such that: au + bv = gcd(a. v. then it must divide a too. 6. hence gcd(a − qb. b) = gcd(a − qb. Then d divides a − qb as well because: a − qb a b a − qb = −q ⇒ ∈ Z. b) ≤ gcd(a. In the same way. b) = gcd(r. Since the remainder (a mod b) < b is strictly smaller than b. IMPLEMENTATION DETAILS and write the latter as y = vf (x1 ) exp(−x1 (x − x1 )). We can modify the algorithms to also find two integers u. Due to the lemma above. b) ≤ gcd(q − qb. vf (x1 ) exp(−x1 (x − x1 )) ≤ f (x). b). i. 1]. b). Proof. Euclid’s algorithm starts with a > b ≥ 1 and sets q to the quotient of the integer division a/b. Recall that the GCD is the largest integer that divides both a and b: gcd(a.9. a mod b).

2015. Li. Ioffe and C. CVPR. ACM Int. K. on Multimedia. and Shuicheng Yan. A. A. The ziggurat method for generating random variables. Tsang. Very deep convolutional networks for large-scale image recognition. CoRR. [12] K. E. [5] S. Zisserman. [14] M. 2014. D. Caffe: An open source convolutional architecture for fast feature embed- ding.Bibliography [1] K. [6] Yangqing Jia. Fulkerson. number arXiv:1504. abs/1312.4400. Simonyan. Sutskever. 2015. Hinton. Szegedy. Fei-Fei. 55 . 2013. 2010. In Proc. Conf. NIPS. Zeiler and R. ECCV. [2] J. Deng. http://caffe. 2012. [4] S. Fast R-CNN. R. Ioffe and C. Journal of statistical software. 2015. 5(8):1–7. ImageNet: A Large-Scale Hierarchical Image Database. Zisserman. Vedaldi. International Journal of Quantum Chemestry. Vedaldi. Li. ArXiv e-prints. and A. B. [8] A.org/. Simonyan. In Proc. Imagenet classification with deep convo- lutional neural networks. Deep inside convolutional networks: Visu- alising image classification models and saliency maps. and G. Visualizing and understanding convolutional networks. Szegedy. Network in network.berkeleyvision. 1996. 2014. and L. In Proc. CoRR. Girshick. Integrals and derivatives for correlated gaussian fuctions using matrix differential calculus. Socher. I. [3] R. In Proc. 57:141–155. W. VLFeat – An open and portable library of computer vision algorithms. Batch normalization: Accelerating deep network training by reducing internal covariate shift. 2009. W. Marsaglia and W. 2000. Simonyan and A. 2014. In Proc. [11] K. Qiang Chen. Fergus. [9] Min Lin. [7] D. Chatfield. ICLR. Krizhevsky.08083. Vedaldi and B. In Proc. Return of the devil in the details: Delving deep into convolutional nets. Zisserman. [13] A. In arXiv.-J. K. and A. [10] G. Dong. Kinghorn. BMVC. Batch Normalization: Accelerating Deep Network Training by Reducing Internal Covariate Shift. 2013. L. 2015.

- Revolutionizing Healthcare With Artificial IntelligenceUploaded byfoliox
- Epe 201301Uploaded bychaouachitaoufik
- 00212931Uploaded bysmishra2222
- AVOIDING DUPLICATED COMPUTATION TO IMPROVE THE PERFORMANCE OF PFSP ON CUDA GPUSUploaded byCS & IT
- MIT6 087IAP10 ProjectUploaded byTrần Vinh
- Optimal Control Problem and Power-Efficient Medical Image Processing Using PumaUploaded byIJMER
- GateUploaded byKarthick Ram
- Vertex TexturesUploaded byfelipebla
- A Novel Architecture Using NVIDIA CUDA to Speed Up Simulation of Multi-Path Fast Fading ChannelsUploaded byDiego Travassos Morais
- 00 - 3rd Workshop AO V1.2Uploaded byPepeLeTvor
- GPU PrimerUploaded bymadcraft9832
- chap15Uploaded byHum Nath Baral
- DEUploaded byGolden Dubey
- Artificial Neural Networks Architecture ApplicationsUploaded byRaamses Díaz
- rev_m580_CUploaded byChris Odden
- ANFISUploaded byAnnisa Fitri
- Continuity and Differ en Ti AbilityUploaded bySesha Sai Kumar
- Ad Maths Formula ListUploaded bysamhayer
- Review 3Uploaded bySuganyadevi Subramaniam
- Changing Concept of ChangeUploaded bysafariore
- LECTURE 3 Second Week Functions of Several Vareables S2 2015-2016Uploaded byFaIz Fauzi
- Calculus for BusinessUploaded byTessa Mendioro De Claro
- Approach to Increase Accuracy of Multimodal Biometric System for Feature Level FusionUploaded byEditor IJRITCC
- lec14Uploaded byTyler Roberts
- Harris DissertationUploaded bySteven Shaw
- Human Activity Recognition & Neural Networks by Matt RobinsonUploaded bym4ttrob
- DifferentiationUploaded byrodwellhead
- Mathematical Modeling of Kluyveromyces Marxianus Growth in Solid-state Fermentation Using a Packed-bed BioreactorUploaded byJuan Oviedo Ingenieria Agroindustrial
- 4_RUploaded byMradul Yadav
- Multiple-Arc Optimization ProblemsUploaded bymykingboody2156

- LAB_10_WordShortcuts.pdfUploaded byUsama Javed
- ETHZ_Lecture5Uploaded byUsama Javed
- US NEWS.xlsUploaded byUsama Javed
- 2018_DATE_SSUploaded byUsama Javed
- ABCUploaded byUsama Javed
- Quran.txtUploaded byUsama Javed
- Berlin Station - 02x05 - Right of Way.tbs.English.hi.C.updated.addic7ed.comUploaded byUsama Javed
- CS231n Convolutional Neural Networks for Visual RecognitionUploaded byUsama Javed
- Anx -AUploaded byUsama Javed
- InternetUploaded byUsama Javed
- Counterpart - 01x03 - The Lost Art of Diplomacy.amzn.WEBRip.sigmA.english.hi.C.updated.addic7ed.com - CopyUploaded byUsama Javed
- Ielts Application for the Issue of Additional TrfsUploaded byUsama Javed
- Ispd 16 ProgramUploaded byUsama Javed
- LICENSE.txtUploaded byUsama Javed
- The.snowman.2017.1080p.bluray.x264 [YTS.ag]Uploaded byUsama Javed
- DAC List of ODA Recipients 2014 final.pdfUploaded byUsama Javed
- Eco 130 (Lec 29-30)Uploaded byUsama Javed
- Assignment 3Uploaded byUsama Javed
- US NEWSUploaded byUsama Javed
- Cvpr2016Uploaded byUsama Javed
- Physical DesignUploaded byUsama Javed
- MIT_department_info.pdfUploaded byUsama Javed
- October 16Uploaded byUsama Javed
- readme.txtUploaded byUsama Javed
- IELTSInformationForCandidates.pdfUploaded byUsama Javed
- Bad Career 7Uploaded byUsama Javed
- Lif NeuronUploaded byUsama Javed
- ELG6163 Implementation of FIRUploaded byUsama Javed
- Xilinx Windows 10 FixUploaded byUsama Javed