{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "
\n", "\n", "UFF logo\n", "\n", "\n", "IC logo\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "align_type": "Left", "slide_type": "-" } }, "source": [ "### Machine Learning\n", "# Deep Learning Neural Networks" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### [Luis Martí](http://lmarti.com)\n", "#### [Instituto de Computação](http://www.ic.uff)\n", "#### [Universidade Federal Fluminense](http://www.uff.br)\n", "$\\newcommand{\\vec}[1]{\\boldsymbol{#1}}$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import random\n", "\n", "import numpy as np\n", "import pandas as pd\n", "\n", "import matplotlib as mpl\n", "import matplotlib.pyplot as plt\n", "import matplotlib.cm as cm\n", "from mpl_toolkits.mplot3d import Axes3D" ] }, { "cell_type": "code", "execution_count": 2, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import seaborn\n", "seaborn.set(style='whitegrid')\n", "seaborn.set_context('talk')\n", "\n", "%matplotlib inline\n", "%config InlineBackend.figure_format = 'retina'" ] }, { "cell_type": "code", "execution_count": 3, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "from ipywidgets import interact, interactive, fixed\n", "import ipywidgets as widgets" ] }, { "cell_type": "code", "execution_count": 6, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# tikzmagic extesion for figures - https://github.com/mkrphys/ipython-tikzmagic\n", "%load_ext tikzmagic" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "# fixing a seed for reproducibility, do not do this in real life. \n", "random.seed(a=42) " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "### About the notebook/slides\n", "\n", "* The slides are _programmed_ as a [Jupyter](http://jupyter.org)/[IPython](https://ipython.org/) notebook.\n", "* **Feel free to try them and experiment on your own by launching the notebooks.**\n", "\n", "* You can run the notebook online: [![Binder](http://mybinder.org/badge.svg)](http://mybinder.org/repo/lmarti/machine-learning)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "If you are using [nbviewer](http://nbviewer.jupyter.org) you can change to slides mode by clicking on the icon:\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## Big data\n", "\n", "* *Buzzword* implying that you are able to handle large amounts of data.\n", "* Algorithmical and technical challenges.\n", "* Dimensions:\n", " * Number of records or entries (this is mostly a technical challenge).\n", " * Number of variables to take into account.\n", " * Complex data representation i.e. images." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Handling images\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "# Dealing with this complex and high-dimensional data\n", "\n", "* Intuitively we'd expect networks with many more hidden layers to be more powerful.\n", "* Such networks could use the intermediate layers to build up multiple levels of abstraction.\n", "* Extract progressively more abstract features.\n", "* A deep network could have a better performance than a shallow one with the same number of neurons?\n", "\n", "Deep neural networks at last!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "For example, doing visual pattern recognition:\n", "* neurons in the first hidden layer might learn to recognize edges,\n", "* neurons in the second layer could learn to recognize more complex shapes, say triangle or rectangles, built up from edges. \n", "* The third layer would then recognize still more complex shapes. \n", "* And so on." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " From http://rinuboney.github.io/2015/10/18/theoretical-motivations-deep-learning.html" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "# Learning deep MLP\n", "\n", "* Intuitively, nothing stop us from training deep MLPs with the backpropagation algorithm.\n", "* However, results are worst than *shallow* architectures.\n", "\n", "### Why?\n", "\n", "* This contradicts our intuition.\n", "* In the worst case we should have layers \"doing nothing\" but not worsening the results." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## Investigating the issue\n", "\n", "* We need a measure of of progress of learning -> our gradients.\n", "* I have already told you that is important to check the gradients.\n", "* We have the vectors $\\vec{\\delta}^1,\\ldots,\\vec{\\delta}^l,\\ldots$ of the deltas corresponding to each layer.\n", "* We can use the norm of the vector $\\left|\\vec{\\delta}^l\\right|$ as an indicator of how much learning is taking place in each layer. " ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "Gradient descent with just 1,000 training images, trained over 500 epochs on the MNIST dataset.\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " more examples on http://neuralnetworksanddeeplearning.com/chap5.html." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "* The two layers start out learning at very different speeds\n", "* The speed in both layers then drops very quickly, before rebounding. \n", "* *The first hidden layer learns much more slowly than the second hidden layer*." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## What about a three hidden layers network?\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "## And with four?\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "### The phenomenon is known as the \n", "# Vanishing/Exploding Gradient Problem\n", "\n", "Was reported by:\n", "* Gradient flow in recurrent nets: the difficulty of learning long-term dependencies, by Sepp Hochreiter, Yoshua Bengio, Paolo Frasconi, and Jürgen Schmidhuber (2001).\n", "* Sepp Hochreiter's earlier Diploma Thesis, Untersuchungen zu dynamischen neuronalen Netzen (1991, in German).\n", "\n", "But, probably every body that worked in neural networks had eventually made an experiment like the previous one." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "* It turns out that the gradient in deep neural networks is **unstable**, \n", "* tending to either **explode** or **vanish** in earlier layers. \n", "* This instability is a fundamental problem for gradient-based learning in deep neural networks. \n", "* It's something we need to understand and address." ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "# What's causing the vanishing gradient problem?\n", "\n", "Lets picture a simple deep network:\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "Here, $w_1, w_2,\\ldots$, are the weights, $b_1,b_2,\\ldots$ are the biases, and $C$ is some cost function. \n", "$$\n", "\\frac{\\partial C}{\\partial b_1} = \n", "f'(\\text{net}_1)\\times w_2 \\times f'(\\text{net}_2) \\times w_3 \\times f'(\\text{net}_3) \\times w_4 \\times f'(\\text{net}_5) \\times\n", "\\frac{\\partial C}{\\partial \\hat{y}}.\n", "$$" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "\"What makes deep networks hard to train?\" is a complex question:\n", "* instabilities associated to gradient-based learning in deep networks.\n", "* Evidence suggest that there is also a role played by the choice of activation function, \n", "* the way weights are initialized, and \n", "* how learning by gradient descent is implemented. \n", "\n", "Many factors play a role in making deep networks hard to train, and understanding all those factors is still a subject of ongoing research." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# What of we train each layer separately?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ " ## Autoencoders\n", "\n", "* An autoencoder is typically a MLP neural network which aims to learn a compressed, distributed representation (encoding) of a dataset.\n", "* They learn to predict their inputs." ] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", "" ] }, "metadata": { "isolated": "true" }, "output_type": "display_data" } ], "source": [ "%tikz -s 600,300 -sc 1.0 -l positioning -f svg \\input{imgs/05/autoencoder.tikz}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Stacked autoencoders" ] }, { "cell_type": "code", "execution_count": 7, "metadata": { "scrolled": true, "slideshow": { "slide_type": "-" } }, "outputs": [ { "data": { "image/svg+xml": [ "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", " \n", "\n", "\n", " \n", " \n", "\n", "\n", "" ] }, "metadata": { "isolated": "true" }, "output_type": "display_data" } ], "source": [ "%tikz -s 800,300 -sc 1.0 -l positioning -f svg \\input{imgs/05/stacked-autoencoder.tikz}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "\n", "* The hidden layer of autoencoder $t$ acts as an input layer to autoencoder $t+1$.\n", "* The input layer of the first autoencoder is the input layer for the whole network.\n", "\n", "The greedy layer-wise training procedure works like this:\n", "* Train the hidden layers as autoencoders in succession.\n", "* Train final (output layer) to predict targets." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Can we look back (again) to nature for inspiration?" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Convolutional Neural Networks (CNNs / ConvNets)\n", "\n", "Convolutional Neural Networks are very similar to ordinary neural networks:\n", "\n", "* They are made up of neurons that have learnable weights and biases. \n", "* Each neuron receives some inputs, performs a dot product and optionally follows it with a non-linear activation. \n", "* The whole network still expresses a single differentiable score function: from the raw image pixels on one end to class scores at the other.\n", "* They still have a loss function on the last (fully-connected) layer and all the tips/tricks we debated for learning regular NN still apply.\n", "\n", "Difference: \n", "* ConvNets make the explicit assumption that the inputs are images, this allows to encode certain properties into the architecture. \n", "* Forward function more efficient to implement and vastly reduce the amount of parameters in the network.\n", "* Main application: image classification." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Yet another problem with regular NNs\n", "\n", "* Regular Neural Nets don’t scale well to full images.\n", "* Images only of size $32\\times32\\times3$ (32 wide, 32 high, 3 color channels), imply that the first hidden layer will have $32\\times 32\\times3 = 3072$ weights.\n", "* This fully-connected structure does not scale to larger images. \n", "* For example, an image of more \"normal\" size, e.g. $200\\times 200\\times3$, would lead to neurons that have $200\\times 200\\times3$ = $120,000$ weights!\n", "* Moreover, we would almost certainly want to have several such neurons, so the parameters would add up quickly! \n", "* Clearly, this full connectivity is wasteful.\n", "* The huge number of parameters would quickly lead to overfitting." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Convolutional Neural Networks take advantage of the fact that the input consists of images\n", "\n", "* constrain the architecture in a more sensible way. \n", "* unlike a regular Neural Network, the layers of a ConvNet have neurons arranged in 3 dimensions: width, height, depth.\n", "* neurons in a layer will only be connected to a small region of the layer before it, instead of all of the neurons in a fully-connected manner.\n", "* The ConvNet architecture we will reduce the full image into a single vector of class scores, arranged along the depth dimension." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
\n", "
\n", "
\n", "
\n", "
\n", "
Regular network\n", " \n", "
\n", "
\n", "
\n", "
ConvNet\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Key concepts\n", "\n", "* **Local receptive fields**: We won't connect every input pixel to every hidden neuron. Instead, we only make connections in small, localized regions of the input image.\n", "* **Shared weights and biases**.\n", "* **Pooling**: usually used immediately after convolutional layers. They simplify the information in the output from the convolutional layer." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## ConvNets layer types\n", "\n", "* As we described above, a ConvNet is a sequence of layers, and\n", "* every layer of a ConvNet transforms one volume of activations to another through a differentiable function. \n", "* Three main types of layers to build ConvNet architectures: \n", " * Convolutional Layer, \n", " * Rectified linear units (RELU) Layer,\n", " * Pooling Layer, and \n", " * Fully-Connected Layer (exactly as seen in regular Neural Networks). \n", "\n", "We will stack these layers to form a full ConvNet architecture." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Convolutional Layer\n", "\n", "The convolutional layer is the core building block of a convolutional network that does most of the computational heavy lifting.\n", "* consist of a set of learnable filters. \n", "* Every filter is small spatially (along width and height), but extends through the full depth of the input volume. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "For example,\n", "* a typical filter on a first layer of a ConvNet might have size 5x5x3 (i.e. 5 pixels width and height, and 3 color channels).\n", "* During the forward pass, we slide (*convolve*) each filter across the width and height of the input volume and compute dot products between the entries of the filter and the input at any position. \n", "* We will produce a 2-dimensional activation map that gives the responses of that filter at every spatial position.\n", "\n", "![Convolution example](http://ufldl.stanford.edu/tutorial/images/Convolution_schematic.gif)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* The network will learn filters that activate when they see some type of visual feature:\n", " * an edge of some orientation or a blotch of some color on the first layer, or \n", " * entire honeycomb or wheel-like patterns on higher layers of the network.\n", "* We will have an entire set of filters in each CONV layer (e.g. 12 filters), and each of them will produce a separate 2-dimensional activation map. We will stack these activation maps along the depth dimension and produce the output volume." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Spatial arrangement\n", "\n", "How many neurons there are in the output volume and how they are arranged?\n", "Three hyperparameters control the size of the output volume: \n", "* depth, \n", "* stride and \n", "* zero-padding." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Depth of the output volume \n", "\n", "Corresponds to the number of filters we would like to use, \n", "* each learning to look for something different in the input. \n", "\n", "For example, if the first Convolutional Layer takes as input the raw image, then different neurons along the depth dimension may activate in presence of various oriented edged, or blobs of color." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Stride\n", "\n", "* We must specify the stride with which we slide the filter. \n", "* When the stride is 1 then we move the filters one pixel at a time.\n", "* When the stride is 2 (or uncommonly 3 or more, though this is rare in practice) then the filters jump 2 pixels at a time as we slide them around. \n", "* This will produce smaller output volumes spatially." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Zero padding\n", "\n", "* Sometimes it is convenient to pad the input volume with zeros around the border.\n", "* The nice feature of zero padding is that it will allow us to control the spatial size of the output volumes." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Shared weight and biases\n", "\n", "* All neurons in the layer detect the same *feature*.\n", "* We need to add layers to encode more features.\n", "\n", "
\n", "\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", "Krizhevsky et al. Each of the 96 filters shown here is of size $[11\\times11\\times3]$, and each one is shared by the $55\\times55$ neurons in one depth slice" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Summarizing the Convolutional layer\n", "* Accepts a volume of size $W_1\\times H_1\\times D_1$\n", "* Requires four hyperparameters:\n", " * Number of filters $K$,\n", " * their spatial extent $F$,\n", " * the stride $S$,\n", " * the amount of zero padding $P$.\n", "Produces a volume of size $W_2\\times H_2\\times D_2$ where:\n", "$$\n", "W_2 = \\frac{W_1−F+2P}{S+1},\\ H_2 = \\frac{H_1−F+2P}{S+1},\\ D_2=K\n", "$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Pooling\n", "\n", "* Subsampling layers reduce the size of the input. \n", "\n", "* There are multiple ways to subsample, but the most popular are:\n", " - max pooling (most popular), \n", " - average pooling, and\n", " - stochastic pooling." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "In max-pooling, a pooling unit simply outputs the maximum activation in the 2×22×2 input region:\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can see convolution as the application of a filter or a dimensionality reduction.\n", "\n", "* Convolutional layers apply a number of filters to the input. \n", "* The result of one filter applied across the image is called feature map.\n", "* If the previous layer is also convolutional, the filters are applied across all of it’s FMs with different weights, so each input FM is connected to each output FM. \n", "> The intuition behind the shared weights across the image is that the features will be detected regardless of their location, while the multiplicity of filters allows each of them to detect different set of features." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* The convolutional architecture is quite different to the architecture of traditional neural network.\n", "* But the overall picture is similar: \n", " * a network made of many simple units, \n", " * whose behaviors are determined by their weights and biases. \n", "The overall goal is still the same: to use training data to train the network's weights and biases so that the network does a good job classifying input." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Restricted Boltzmann machines\n", "\n", "Restricted Boltzmann machines (RBM) are generative stochastic neural network that can learn a probability distribution over its set of inputs.\n", "\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Training RBMs: Contrastive Divergence\n", "\n", "* **Positive phase:**\n", " - An input sample $\\vec{x}$ is presented to the input layer. \n", " - $\\vec{x}$ is propagated to the hidden layer in a similar manner to the feedforward networks. \n", " - The result is the hidden layer activations, $\\vec{h}$.\n", "* **Negative phase:**\n", " - Propagate $\\vec{h}$ back to the visible layer with result resulting in a $\\vec{x}'$.\n", " - $\\vec{x}'$ back to the hidden layer.\n", "* **Weight update**:\n", "$$ \\vec{w}(t+1) = \\vec{w}(t) + \\alpha (\\vec{x}\\vec{h}^{\\intercal} -\\vec{x'}\\vec{h'}^{\\intercal}).$$\n", "\n", "* The positive phase reflects the network internal representation of the data.\n", "* The negative phase represents an attempt to recreate the data based on this internal representation. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "* The goal is that the \"generated\" data to be as close as possible to the \"real\" one.\n", "* This is reflected in the weight update formula.\n", "\n", "> In other words, the net has a perception of how the input data must be represented, so it tries to reproduce the data based on this perception. If its reproduction isn’t close enough to reality, it makes an adjustment and tries again." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Deep Belief Networks\n", "\n", "Restricted Boltzmann machines can be stacked to create a class of neural networks known as deep belief networks (DBNs).\n", "
\n", "
\n", "
\n", "
\n", "
\n", "
\n", " \n", "
\n", "
\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Programming Deep Learning\n", "\n", "* Torch - An open source software library for machine learning based on the Lua programming language.\n", "* Caffe - A deep learning framework.\n", "* Apache SINGA - A deep learning platform developed for scalability, usability and extensibility.\n", "\n", "My current preference:\n", "* Tensorflow" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# General Principles\n", "\n", "* Supervise the learning process (did I mentioned that you should check your gradients?).\n", "* Use a correct experimental methodology.\n", "* Contrast your results with a baseline method." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# [Applications of Deep Learning](https://en.wikipedia.org/wiki/Deep_learning#Applications)\n", "\n", "There are many successfull applications, for example:\n", "\n", "* Computer vision and image recognition;\n", "* Speech recognition;\n", "* Natural language processing $\\rightarrow$ probabilistic context free grammars;\n", "* Anomaly detection on many variables;\n", "* ... and many more. " ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ "
\n", "
\n", "
\n", "
\n", "
\n", " \"Creative\n", "
\n", "
\n", " This work is licensed under a [Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License](http://creativecommons.org/licenses/by-nc-sa/4.0/).\n", "
\n", "
\n", "
\n", "
" ] }, { "cell_type": "markdown", "metadata": { "collapsed": true, "slideshow": { "slide_type": "slide" } }, "source": [ "# Final remarks\n", "\n", "* Deep learning as a step towards realising *strong AI*;\n", "* thus many organizations have become interested in its use for particular applications.\n", " * see https://en.wikipedia.org/wiki/Deep_learning#Commercial_activities\n", "* Better understanding of mental processes.\n", "* Deep learning $\\iff$ big data." ] }, { "cell_type": "code", "execution_count": 8, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [ { "data": { "application/json": { "Software versions": [ { "module": "Python", "version": "3.6.1 64bit [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]" }, { "module": "IPython", "version": "6.0.0" }, { "module": "OS", "version": "Darwin 16.6.0 x86_64 i386 64bit" }, { "module": "scipy", "version": "0.19.0" }, { "module": "numpy", "version": "1.12.1" }, { "module": "matplotlib", "version": "2.0.2" }, { "module": "sklearn", "version": "0.18.1" } ] }, "text/html": [ "
SoftwareVersion
Python3.6.1 64bit [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]
IPython6.0.0
OSDarwin 16.6.0 x86_64 i386 64bit
scipy0.19.0
numpy1.12.1
matplotlib2.0.2
sklearn0.18.1
Tue May 23 12:07:12 2017 -03
" ], "text/latex": [ "\\begin{tabular}{|l|l|}\\hline\n", "{\\bf Software} & {\\bf Version} \\\\ \\hline\\hline\n", "Python & 3.6.1 64bit [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] \\\\ \\hline\n", "IPython & 6.0.0 \\\\ \\hline\n", "OS & Darwin 16.6.0 x86\\_64 i386 64bit \\\\ \\hline\n", "scipy & 0.19.0 \\\\ \\hline\n", "numpy & 1.12.1 \\\\ \\hline\n", "matplotlib & 2.0.2 \\\\ \\hline\n", "sklearn & 0.18.1 \\\\ \\hline\n", "\\hline \\multicolumn{2}{|l|}{Tue May 23 12:07:12 2017 -03} \\\\ \\hline\n", "\\end{tabular}\n" ], "text/plain": [ "Software versions\n", "Python 3.6.1 64bit [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)]\n", "IPython 6.0.0\n", "OS Darwin 16.6.0 x86_64 i386 64bit\n", "scipy 0.19.0\n", "numpy 1.12.1\n", "matplotlib 2.0.2\n", "sklearn 0.18.1\n", "Tue May 23 12:07:12 2017 -03" ] }, "execution_count": 8, "metadata": {}, "output_type": "execute_result" } ], "source": [ "%load_ext version_information\n", "%version_information scipy, numpy, matplotlib, sklearn" ] }, { "cell_type": "code", "execution_count": 9, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [ { "data": { "text/html": [ "\n", "\n", "\n", "\n" ], "text/plain": [ "" ] }, "execution_count": 9, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# this code is here for cosmetic reasons\n", "from IPython.core.display import HTML\n", "from urllib.request import urlopen\n", "HTML(urlopen('https://raw.githubusercontent.com/lmarti/jupyter_custom/master/custom.include').read().decode('utf-8'))" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "skip" } }, "source": [ " " ] } ], "metadata": { "anaconda-cloud": {}, "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.6.3" } }, "nbformat": 4, "nbformat_minor": 1 }