{ "cells": [ { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Lecture 3: Scaling to complex models by learning with optimization algorithms.\n", "\n", "## Gradient descent, convex optimization and conditioning. \n", "\n", "## CS4787 — Principles of Large-Scale Machine Learning Systems\n", "\n", "$\\newcommand{\\R}{\\mathbb{R}}$\n", "$\\newcommand{\\norm}{\\left\\|#1\\right\\|}$" ] }, { "cell_type": "code", "execution_count": 1, "metadata": { "slideshow": { "slide_type": "skip" } }, "outputs": [], "source": [ "import numpy\n", "import scipy\n", "import matplotlib\n", "import time\n", "\n", "def hide_code_in_slideshow(): \n", " from IPython import display\n", " import binascii\n", " import os\n", " uid = binascii.hexlify(os.urandom(8)).decode() \n", " html = \"\"\"
\n", " \"\"\" % (uid, uid)\n", " display.display_html(html, raw=True)" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Review: The Empirical Risk\n", "\n", "Suppose we have a dataset $\\mathcal{D} = \\{ (x_1, y_1), (x_2, y_2), \\ldots, (x_n, y_n) \\}$, where $x_i \\in \\mathcal{X}$ is an example and $y_i \\in \\mathcal{Y}$ is a label.\n", "Let $h: \\mathcal{X} \\rightarrow \\mathcal{Y}$ be a hypothesized model (mapping from examples to labels) we are trying to evaluate.\n", "Let $L: \\mathcal{Y} \\times \\mathcal{Y} \\rightarrow \\mathbb{R}$ be a _loss function_ which measures how different two labels are." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The **empirical risk** is\n", "$$\n", " R(h) = \\frac{1}{n} \\sum_{i=1}^n L(h(x_i), y_i).\n", "$$\n", "Most notions of error or accuracy in machine learning can be captured with an empirical risk.\n", "A simple example: measure the error rate on the dataset with the _0-1 Loss Function_\n", "$$\n", " L(\\hat y, y) = 0 \\text{ if } \\hat y = y \\text{ and } 1 \\text{ if } \\hat y \\ne y.\n", "$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### What are other examples you've seen of empirical risk and loss functions?\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Loss function:\n", "* Mean squared error\n", "* Hinge loss\n", "* Cross Entropy loss\n", "* Absolute error\n", "* Negative log likelihood\n", "* ROC\n", "\n", "Regularizers:\n", "* $\\ell_2$ and $\\ell_1$ regularization" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## The Cost of Computing the Empirical Risk\n", "\n", "We need to compute the empirical risk a lot during training, both during validation (and hyperparameter optimization) and testing, so it's nice if we can do it fast.\n", "\n", "**Question: how does computing the empirical risk scale?** Three things affect the cost of computation.\n", "* The number of training examples $n$. The cost will certainly be proportional to $n$.\n", "* The cost to compute the loss function $L$.\n", "* The cost to evaluate the hypothesis $h$.\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Review: Empirical Risk Minimization\n", "\n", "We don't just want to compute the empirical risk: we also want to minimize it.\n", "To do so, we _parameterize_ the hypotheses using some parameters $w \\in \\R^d$.\n", "That is, we assign each hypothesis a $d$-dimensional vector of parameters and vice versa and solve the optimization problem\n", "\\begin{align*}\n", " \\mbox{minimize: }& R(h_w) = \\frac{1}{n} \\sum_{i=1}^n L(h_w(x_i), y_i) = f(w) = \\frac{1}{n} \\sum_{i=1}^n f_i(w) \\\\ \\mbox{ over }& w \\in \\R^d\n", "\\end{align*}\n", "where $h_w$ denotes the hypothesis associated with the parameter vector $w$.\n", "This is an instance of a principle of scalable ML: **Write your learning task as an optimization problem, then solve it with an optimization algorithm.**" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Most optimization algorithms used in machine learning with with **gradients**...so let's make sure we're on the same page about gradients before we move on." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Gradients\n", "\n", "_What is a gradient?_\n", "\n", "Suppose I have a function $f$ from $\\R^d$ to $\\R$. The gradient, $\\nabla f$, is a function from $\\R^d$ to $\\R^d$ such that\n", "\n", "$$\\left(\\nabla f(w) \\right)_i = \\frac{\\partial}{\\partial w_i} f(w) = \\lim_{\\delta \\rightarrow 0} \\frac{f(w + \\delta e_i) - f(w)}{\\delta},$$\n", "\n", "that is, it is the **vector of partial derivatives of the function**.\n", "Another, perhaps cleaner (and basis-independent), definition is that $\\nabla f(w)^T$ is the linear map such that for any $u \\in \\R^d$\n", "\n", "$$\\nabla f(w)^T u = \\lim_{\\delta \\rightarrow 0} \\frac{f(w + \\delta u) - f(w)}{\\delta} = \\left. \\frac{\\partial}{\\partial \\alpha} f(w + \\alpha u) \\; \\right|_{\\alpha = 0}.$$\n", "\n", "More informally, it is the unique vector such that $f(w) \\approx f(w_0) + (w - w_0)^T \\nabla f(w_0) + \\mathcal{o}( |w - w_0| )$ for $w$ nearby $w_0$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "# Let's derive some gradients!\n", "\n", "$f(x) = x^T A x$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "$f(x) = \\sum_{i=1}^d \\sum_{j=1}^d A_{ij} x_i x_j$\n", "\n", "$\\left( \\nabla f(x) \\right)_k = \\frac{\\partial}{\\partial x_k} f(x) = \\sum_{j=1}^d A_{kj} x_j + \\sum_{i=1}^d A_{ik} x_i$\n", "\n", "$\\nabla f(x) = \\sum_{k=1}^d \\left( \\nabla f(x) \\right)_k e_k$ \n", "\n", "$\\nabla f(x) = \\sum_{k=1}^d \\left( \\sum_{j=1}^d A_{kj} x_j + \\sum_{i=1}^d A_{ik} x_i \\right) e_k$ \n", "\n", "$\\nabla f(x) = Ax + A^T x$\n", "\n", "$\\nabla^2 f(w) = A + A^T$\n", "\n", "$(\\nabla^2 f(w))^{-1} \\nabla f(x) = x$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "$$(x + y)^T z = x^T z + y^T z$$\n", "\n", "\\begin{align*}\n", "f(x + \\delta u) \n", "&=\n", "(x + \\delta u)^T A (x + \\delta u)\n", "\\\\&=\n", "x A (x + \\delta u) + \\delta u A (x + \\delta u) \n", "\\\\&=\n", "x^T A x + \\delta (u^T A x + u^T A^T x) + \\delta^2 u^T A u\n", "\\end{align*}\n", "\n", "\n", "\n", "\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "$$\\lim_{\\delta \\rightarrow 0} \\frac{x^T A x + \\delta (u^T A x + u^T A^T x) + \\delta^2 u^T A u - x^T A x}{\\delta} = u^T A x + u^T A^T x$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$f(x) = \\| x \\|_2^2 = \\sum_{i=1}^d x_i^2 = x^T x = x^T I x$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "$$\\frac{\\partial f}{\\partial x_k} = \n", "\\frac{\\partial}{\\partial x_k} x_k^2 = 2 x_k$$\n", "\n", "$$\\nabla f(x) = 2 x$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$f(x) = \\| x \\|_1 = \\sum_{i=1}^d | x_i |$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "$$\\frac{\\partial f}{\\partial x_k} = \n", "\\frac{\\partial}{\\partial x_k} |x_k|\n", "=\n", "\\operatorname{sign}(x_k)$$\n", "\n", "$$\\nabla f(x) = \\operatorname{sign}(x),$$ where sign operates elementwise" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "$f(x) = \\| x \\|_{\\infty} = \\max(|x_1|, |x_2|, \\ldots, |x_d|)$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "$$\\frac{\\partial f}{\\partial x_k} =\n", "\\begin{cases} \n", "0 & | x_k | < \\| x \\|_{\\infty} \\\\\n", "\\operatorname{sign}(x_k) & | x_k | = \\| x \\|_{\\infty} \\end{cases}$$\n", "\n", "$$\\nabla f(x) = \\operatorname{sign}(x) \\cdot 1_{|x_k| = \\|x\\|_\\infty}$$" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "If $x = \\begin{bmatrix} -2 \\\\ 1 \\end{bmatrix}$\n", "\n", "$$\\nabla f(x) = \\begin{bmatrix} \\operatorname{sign}(-2) \\\\ 0 \\end{bmatrix} = \\begin{bmatrix} -1 \\\\ 0 \\end{bmatrix}.$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Higher-order derivatives\n", "\n", "Gradients have natural generalizations to second- and higher-order derivatives.\n", "The most common one to see in ML is the **Hessian**, which is the matrix of second-partial derivatives.\n", "\n", "Suppose I have a function $f$ from $\\R^d$ to $\\R$. The Hessian, $\\nabla^2 f$, is a function from $\\R^d$ to $\\R^{d \\times d}$ such that\n", "\n", "$$\\left(\\nabla^2 f(w) \\right)_{ij} = \\frac{\\partial^2}{\\partial w_i \\partial w_j} f(w).$$\n", "\n", "More informally, it is the unique matrix such that $$f(w) \\approx f(w_0) + (w - w_0)^T \\nabla f(w_0) + \\frac{1}{2} (w-w_0)^T \\nabla^2 f(w_0) (w-w_0) + \\mathcal{o}( |w - w_0|^2 )$$ for $w$ nearby $w_0$.\n" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Gradient descent (GD).\n", "\n", "Initialize the parameters at some value $w_0 \\in \\R^d$, and decrease the value of the empirical risk iteratively by running\n", "$$\n", " w_{t+1} = w_t - \\alpha_t \\cdot \\nabla f(w_t) = w_t - \\alpha_t \\cdot \\frac{1}{n} \\sum_{i=1}^n \\nabla f_i(w)\n", "$$\n", "where $w_t$ is the value of the parameter vector at time $t$, $\\alpha_t$ is a parameter called the _learning rate_ or _step size_, and $\\nabla f$ denotes the gradient (vector of partial derivatives) of $f$." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Gradient descent is not the only classic optimization method though..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Another iterative method of optimization: Newton's method.\n", "\n", "Initialize the parameters at some value $w_0 \\in \\R^d$, and decrease the value of the empirical risk iteratively by running\n", "$$\n", " w_{t+1} = w_t - \\left( \\nabla^2 f(w_t) \\right)^{-1} \\nabla f(w_t).\n", "$$ \n", "Newton's method can converge more quickly than gradient descent, because it is a _second-order optimization method_ (it uses the second derivative of $f$) whereas gradient descent is only a _first-order method_ (it uses the first derivative of $f$)." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
Question: what is the computational cost of running a single step of gradient descent and Newton's method? Not including the training set, how much memory is required?
\n", "\n", "Suppose that computing gradients of the examples takes $\\Theta(d)$ time and computing Hessians takes $\\Theta(d^2)$ time, and express your answer in terms $n$ and $d$.\n", "\n", "Gradient descent: $\n", " w_{t+1} = w_t - \\alpha_t \\cdot \\nabla f(w_t) = w_t - \\alpha_t \\cdot \\frac{1}{n} \\sum_{i=1}^n \\nabla f_i(w)\n", "$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Time: $O(nd)$\n", "\n", "Memory: $O(d)$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Newton's method: $\n", " w_{t+1} = w_t - \\left( \\nabla^2 f(w_t) \\right)^{-1} \\nabla f(w_t).\n", "$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "Time: $O(nd^2 + d^3) = O(nd + nd^2 + d^3 + d^2 + d)$\n", "\n", "Memory: $O(d^2)$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Why does gradient descent work?\n", "\n", "**...and what does it mean for it to work?**\n", "\n", "Intuitively, GD decreases the value of the objective at each iteration, as long as the learning rate is small enough and the value of the gradient is nonzero.\n", "Eventually, this should result in gradient descent _coming close to a point where the gradient is zero_.\n", "We can prove that gradient descent works under the assumption that the second derivative of the objective is bounded (this is sometimes called an $L$-smoothness bound).\n", "* There are other assumptions we could use here also, but this is the simplest one.\n", "\n", "Suppose that for some constant $L > 0$, for all $x$ and $y$ in $\\R^d$,\n", "\n", "$$\n", " \\| \\nabla f(x) - \\nabla f(y) \\| \\le L \\| x - y \\|. \n", "$$\n", "\n", "Here, $\\| \\cdot \\|$ denotes the $\\ell_2$ norm: $\\|u\\| = \\sqrt{\\sum_{i=1}^d u_i^2}$.\n", "\n", "* This is equivalent to the condition that $\\| \\nabla^2 f(x) \\|_2 \\le L$ (where the norm here denotes the induced norm) if the function is twice-differentiable." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "Starting from this condition, let's look at how the objective changes over time as we run gradient descent with a fixed step size.\n", "\n", "\\begin{align*}\n", "f(w_{t+1})\n", "&=\n", "f(w_t - \\alpha \\nabla f(w_t)).\n", "\\end{align*}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Let's consider this expression as a function of $\\alpha$. Call this $\\rho(\\alpha) = f(w_t - \\alpha \\nabla f(w_t))$. Observe that\n", "\n", "$$\\rho'(\\alpha) = \\frac{\\partial}{\\partial \\alpha} f(w_t - \\alpha \\nabla f(w_t)) = -\\nabla f(w_t - \\alpha \\nabla f(w_t))^T \\nabla f(w_t).$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Now, by the **fundamental theorem of calculus**,\n", "\n", "$$\n", " \\rho(\\alpha) - \\rho(0) = \\int_0^\\alpha \\rho'(\\eta) \\; d \\eta.\n", "$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "If we substitute back in our definition of $\\rho$, we get\n", "\n", "$$f(w_{t+1}) = f(w_t - \\alpha \\nabla f(w_t)) = f(w_t) - \\int_0^{\\alpha} \\nabla f(w_t - \\eta \\nabla f(w_t))^T \\nabla f(w_t) \\; d \\eta.$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "We can now simplify this as follows. (Keep in mind: $\\| \\nabla f(x) - \\nabla f(y) \\| \\le L \\| x - y \\|$).\n", "\n", "\\begin{align*}\n", "f(w_{t+1}) \n", "&= \n", "f(w_t) - \\int_0^{\\alpha} \\nabla f(w_t - \\eta \\nabla f(w_t))^T \\nabla f(w_t) \\; d \\eta.\n", "\\\\&= \n", "f(w_t) \n", "- \\alpha \\nabla f(w_t)^T \\nabla f(w_t)\n", "- \\int_0^{\\alpha} \\left( \\nabla f(w_t - \\eta \\nabla f(w_t)) - \\nabla f(w_t) \\right)^T \\nabla f(w_t) \\; d \\eta.\n", "\\\\&= \n", "f(w_t) \n", "- \\alpha \\| \\nabla f(w_t) \\|^2\n", "+ \\int_0^{\\alpha} \\left( \\nabla f(w_t) - \\nabla f(w_t - \\eta \\nabla f(w_t)) \\right)^T \\nabla f(w_t) \\; d \\eta.\n", "\\\\&\\le\n", "f(w_t) \n", "- \\alpha \\| \\nabla f(w_t) \\|^2\n", "+ \\int_0^{\\alpha} \\left\\| \\nabla f(w_t) - \\nabla f(w_t - \\eta \\nabla f(w_t)) \\right\\| \\cdot \\left| \\nabla f(w_t) \\right| \\; d \\eta.\n", "\\end{align*}\n", "Where the last line follows from $x^T y \\le \\| x \\| \\cdot \\| y \\|$. Now by our smoothness bound,\n", "\\begin{align*}\n", "f(w_{t+1}) \n", "&\\le\n", "f(w_t) \n", "- \\alpha \\| \\nabla f(w_t) \\|^2\n", "+ \\int_0^{\\alpha} L \\cdot \\left\\| (w_t) - (w_t - \\eta \\nabla f(w_t)) \\right\\| \\cdot \\left| \\nabla f(w_t) \\right| \\; d \\eta\n", "\\\\&=\n", "f(w_t) \n", "- \\alpha \\| \\nabla f(w_t) \\|^2\n", "+ L \\| \\nabla f(w_t) \\|^2 \\cdot \\int_0^{\\alpha} \\eta \\; d \\eta\n", "\\\\&\\le\n", "f(w_t) \n", "- \\alpha \\| \\nabla f(w_t) \\|^2\n", "+ \\frac{1}{2} \\alpha^2 L \\| \\nabla f(w_t) \\|^2\n", "\\\\&\\le\n", "f(w_t) \n", "- \\alpha \\left(1 - \\frac{1}{2} \\alpha L \\right) \\cdot \\| \\nabla f(w_t) \\|^2.\n", "\\end{align*}\n", "\n", "Note that there is a somewhat easier way of getting to this result using Taylor's theorem rather than FTC; this easier approach is the one I use in the notes. I'm using this more-long-winded way of getting there to make sure that everyone gets the intuition." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "So, we got to\n", "\n", "$$f(w_{t+1}) \\le\n", "f(w_t) \n", "- \\alpha \\left(1 - \\frac{1}{2} \\alpha L \\right) \\cdot \\| \\nabla f(w_t) \\|^2.$$\n", "\n", "If we choose our step size $\\alpha$ to be _small enough_ that $1 \\ge \\alpha L$, then this simplifies to\n", "\n", "$$f(w_{t+1}) \\le\n", "f(w_t) \n", "- \\frac{\\alpha}{2} \\cdot \\| \\nabla f(w_t) \\|^2.$$\n", "\n", "That is, **the objective is guaranteed to decrease at each iteration**! This matches our intuition for why gradient descent should work." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Now, if we sum this up across $T$ iterations of gradient descent, we get\n", "\n", "$$\n", " \\frac{1}{2} \\alpha \\sum_{t = 0}^{T-1} \\norm{ \\nabla f(w_t) }^2\n", " \\le\n", " \\sum_{t = 0}^{T-1} \\left(\n", " f(w_t)\n", " -\n", " f(w_{t+1})\n", " \\right)\n", " =\n", " f(w_0) - f(w_T)\n", " \\le\n", " f(w_0) - f^*\n", "$$\n", "\n", "where $f^*$ is the global minimum value of the loss function $f$.\n", "From here, we can get\n", "\n", "$$\n", " \\min_{t \\in \\{0, \\ldots, T\\}} \\norm{ \\nabla f(w_t) }^2\n", " \\le\n", " \\frac{1}{T} \\sum_{t = 0}^{T-1} \\norm{ \\nabla f(w_t) }^2\n", " \\le\n", " \\frac{2 (f(w_0) - f^*)}{\\alpha T}.\n", "$$\n", "\n", "\n", "This means that the smallest gradient we observe after $T$ iterations is getting smaller proportional to $1/T$.\n", "So gradient descent converges...\n", "* as long as we look at the smallest observed gradient\n", "* and we care about finding a point where the gradient is small" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
Question: does this ensure that gradient descent converges to the global optimum (i.e. the value of $w$ that minimizes $f$ over all $w \\in \\R^d$)?
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### When can we ensure that gradient descent _does_ converge to the unique global optimum?\n", "\n", "Certainly, we can do this when there is only one global optimum.\n", "\n", "The simplest case of this is the case of **convex objectives**." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "## Convex Functions\n", "\n", "A function $f: \\R^d \\rightarrow \\R$ is convex if for all $x, y \\in \\R^d$, and all $\\eta \\in [0,1]$\n", "\n", "$$f(\\eta x + (1 - \\eta) y) \\le \\eta f(x) + (1 - \\eta) f(y).$$\n", "\n", "What this means graphically is that if we draw a line segment between any two points in the graph of the function, that line segment will lie above the function.\n", "There are a bunch of equivalent conditions that work if the function is differentiable.\n", "In terms of the gradient, for all $x, y \\in \\R^d$,\n", "\n", "$$(x - y)^T \\left( \\nabla f(x) - \\nabla f(y) \\right) \\ge 0,$$\n", "\n", "and in terms of the Hessian, for all $x \\in \\R^d$ and all $u \\in \\R^d$\n", "\n", "$$u^T \\nabla^2 f(x) u \\ge 0;$$\n", "\n", "this is equivalent to saying that the Hessian is positive semidefinite." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "
Question: What are some examples of hypotheses and loss functions that result in a convex objective??
" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "-" } }, "source": [ "..." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "An even easier case than convexity: **strong convexity**.\n", " \n", "A function is strongly convex with parameter $\\mu > 0$ if for all $x, y \\in \\R^d$,\n", "\n", "$$f(y) \\ge f(x) + \\nabla f(x)^T (y - x) + \\frac{\\mu}{2} \\norm{x - y}^2$$\n", "\n", "or equivalently, for all $x \\in \\R^d$ and all $u \\in \\R^d$\n", "\n", "$$u^T \\nabla^2 f(x) u \\ge \\mu \\norm{u}^2.$$\n", "\n", "One (somewhat weaker) condition that is implied by strong convexity is\n", "\n", "$$\\norm{\\nabla f(x)}^2 \\ge 2 \\mu \\left( f(x) - f^* \\right);$$\n", "\n", "this is sometimes called the _Polyak-Lojasiewicz condition_.\n", "* A useful note: you can make any convex objective strongly convex by adding $\\ell_2$ regularization." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "### Gradient descent on strongly convex objectives.\n", "\n", "As before, let's look at how the objective changes over time as we run gradient descent with a fixed step size.\n", "* This is a standard approach when analyzing an iterative algorithm like gradient descent.\n", "From our proof before, we had (as long as $1 \\ge \\alpha L$)\n", "\n", "$$f(w_t) \\le\n", "f(w_t) \n", "- \\frac{\\alpha}{2} \\cdot \\| \\nabla f(w_t) \\|^2.$$\n", "\n", "Now applying the Polyak-Lojasiewicz condition condition to this gives us\n", "\n", "$$f(w_{t+1})\n", " \\le\n", " f(w_t)\n", " -\n", " \\alpha \\mu \\left( f(w_t) - f^* \\right).$$\n", " \n", " \n", "Subtracting the global optimum $f^*$ from both sides produces\n", "\n", "$$\n", " f(w_{t+1})\n", " - \n", " f^*\n", " \\le\n", " f(w_t)\n", " - \n", " f^*\n", " -\n", " \\alpha \\mu \\left( f(w_t) - f^* \\right)\n", " =\n", " (1 - \\alpha \\mu) \\left( f(w_t) - f^* \\right).\n", "$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "So we got that\n", "\n", "$$f(w_{t+1}) - f^* \\le (1 - \\alpha \\mu) \\left( f(w_t) - f^* \\right).$$\n", "\n", "But this means that\n", "\n", "\\begin{align*}\n", "f(w_{t+2}) - f^* &\\le (1 - \\alpha \\mu) \\left( f(w_{t+1}) - f^* \\right)\n", "\\\\&\\le (1 - \\alpha \\mu) \\left( (1 - \\alpha \\mu) \\left( f(w_t) - f^* \\right) \\right)\n", "\\\\&\\le (1 - \\alpha \\mu)^2 \\left( f(w_t) - f^* \\right).\n", "\\end{align*}" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "Applying this inductively, after $K$ total steps\n", "\n", "$$f(w_K) - f^*\n", " \\le\n", " (1 - \\alpha \\mu)^K \\left( f(w_0) - f^* \\right)\n", " \\le\n", " \\exp(-\\alpha \\mu K) \\cdot \\left( f(w_0) - f^* \\right).$$\n", "\n", "This shows that, for strongly convex functions, **gradient descent with a constant step size converges exponentially quickly to the optimum**.\n", "This is sometimes called _convergence at a linear rate_." ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "If we use the largest step size that satisfies our earlier assumption that $1 \\ge \\alpha L$ (i.e. $\\alpha = 1/L$), then this rate becomes\n", "\n", "$$f(w_K) - f^*\n", " \\le\n", " \\exp\\left(-\\frac{\\mu K}{L} \\right) \\cdot \\left( f(w_0) - f^* \\right).$$\n", " \n", "Equivalently, in order to ensure that $f(w_K) - f^*$ for some target error $\\epsilon > 0$, it suffices to set the number of iterations $K$ large enough that\n", "\n", "$$K\n", " \\ge\n", " \\frac{L}{\\mu}\n", " \\cdot\n", " \\log\\left( \\frac{f(w_0) - f^*}{\\epsilon} \\right).$$" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "fragment" } }, "source": [ "The main thing that affects how large $K$ needs to be here is the ratio $L / \\mu$, which is a function just of the problem itself and not of how we initialize or how accurate we want our solutions to be.\n", "We call the ratio\n", "\n", "$$\\kappa = \\frac{L}{\\mu}$$\n", "\n", "the condition number of the problem.\n", "The condition number encodes _how hard a strongly convex problem is to solve_.\n", "\n", "* Observe that this ration is invariant to scaling of the objective function $f$.\n", "\n", "When the condition number is very large, the problem can be very hard to solve, and take many iterations of gradient descent to get close to the optimum.\n", "Even if the cost of running gradient descent is tractible as we scale, **if scaling a problem causes the condition number to blow up, this can cause issues**!" ] }, { "cell_type": "markdown", "metadata": { "slideshow": { "slide_type": "slide" } }, "source": [ "
What affects the condition number? What can we do to make the condition number smaller?
\n", "\n", "
DEMO.
" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "celltoolbar": "Slideshow", "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.5" }, "rise": { "scroll": true, "transition": "none" } }, "nbformat": 4, "nbformat_minor": 2 }