{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Automatic Differentiation\n",
"\n",
"In machine learning, we *train* models, updating them successively so that they get better and better as they see more and more data. Usually, *getting better* means minimizing a *loss function*, a score that answers the question \"how *bad* is our model?\" With neural networks, we typically choose loss functions that are differentiable with respect to our parameters. \n",
"Put simply, this means that for each of the model's parameters, we can determine how much *increasing* or *decreasing* it might affect the loss. While the calculations for taking these derivatives are straightforward, requiring only some basic calculus, for complex models, working out the updates by hand can be a pain (and often error-prone).\n",
"\n",
"The autograd package expedites this work by automatically calculating derivatives. And while many other libraries require that we compile a symbolic graph to take automatic derivatives, `autograd` allows us to take derivatives while writing ordinary imperative code. Every time we pass data through our model, `autograd` builds a graph on the fly, tracking which data combined through which operations to produce the output. This graph enables `autograd` to subsequently backpropagate gradients on command. Here *backpropagate* simply means to trace through the compute graph, filling in the partial derivatives with respect to each parameter. If you are unfamiliar with some of the math, e.g. gradients, please refer to the [“Mathematical Basics”](../chapter_appendix/math.md) section in the appendix."
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "1"
}
},
"outputs": [],
"source": [
"from mxnet import autograd, nd"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## A Simple Example\n",
"\n",
"As a toy example, say that we are interested in differentiating the mapping $y = 2\\mathbf{x}^{\\top}\\mathbf{x}$ with respect to the column vector $\\mathbf{x}$. To start, let's create the variable `x` and assign it an initial value."
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "2"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"[[0.]\n",
" [1.]\n",
" [2.]\n",
" [3.]]\n",
"\n"
]
}
],
"source": [
"x = nd.arange(4).reshape((4, 1))\n",
"print(x)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Once we compute the gradient of ``y`` with respect to ``x``, we will need a place to store it. We can tell an NDArray that we plan to store a gradient by invoking its ``attach_grad()`` method."
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "3"
}
},
"outputs": [],
"source": [
"x.attach_grad()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now we are going to compute ``y`` and MXNet will generate a computation graph on the fly. It is as if MXNet turned on a recording device and captured the exact path by which each variable was generated.\n",
"\n",
"Note that building the computation graph requires a nontrivial amount of computation. So MXNet will *only* build the graph when explicitly told to do so. This happens by placing code inside a ``with autograd.record():`` block."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "4"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"[[28.]]\n",
"\n"
]
}
],
"source": [
"with autograd.record():\n",
" y = 2 * nd.dot(x.T, x)\n",
"print(y)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Since the shape of `x` is (4, 1), `y` is a scalar. Next, we can automatically find the gradient by calling the `backward` function. It should be noted that if `y` is not a scalar, MXNet will first sum the elements in `y` to get the new variable by default, and then find the gradient of the variable with respect to `x`."
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "5"
}
},
"outputs": [],
"source": [
"y.backward()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The gradient of the function $y = 2\\mathbf{x}^{\\top}\\mathbf{x}$ with respect to $\\mathbf{x}$ should be $4\\mathbf{x}$. Now let's verify that the gradient produced is correct."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "6"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"True\n",
"\n",
"[[ 0.]\n",
" [ 4.]\n",
" [ 8.]\n",
" [12.]]\n",
"\n"
]
}
],
"source": [
"print((x.grad - 4 * x).norm().asscalar() == 0)\n",
"print(x.grad)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Training Mode and Prediction Mode\n",
"\n",
"As you can see from the above, after calling the `record` function, MXNet will record and calculate the gradient. In addition, `autograd` will also change the running mode from the prediction mode to the training mode by default. This can be viewed by calling the `is_training` function."
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "7"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"False\n",
"True\n"
]
}
],
"source": [
"print(autograd.is_training())\n",
"with autograd.record():\n",
" print(autograd.is_training())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In some cases, the same model behaves differently in the training and prediction modes (e.g. when using neural techniques such as dropout and batch normalization). In other cases, some models may store more auxiliary variables to make computing gradients easier. We will cover these differences in detail in later chapters. For now, you do not need to worry about them.\n",
"\n",
"## Computing the Gradient of Python Control Flow\n",
"\n",
"One benefit of using automatic differentiation is that even if the computational graph of the function contains Python's control flow (such as conditional and loop control), we may still be able to find the gradient of a variable. Consider the following program: It should be emphasized that the number of iterations of the loop (while loop) and the execution of the conditional judgment (if statement) depend on the value of the input `b`."
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "8"
}
},
"outputs": [],
"source": [
"def f(a):\n",
" b = a * 2\n",
" while b.norm().asscalar() < 1000:\n",
" b = b * 2\n",
" if b.sum().asscalar() > 0:\n",
" c = b\n",
" else:\n",
" c = 100 * b\n",
" return c"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that the number of iterations of the while loop and the execution of the conditional statement (if then else) depend on the value of `a`. To compute gradients, we need to `record` the calculation, and then call the `backward` function to calculate the gradient."
]
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "9"
}
},
"outputs": [],
"source": [
"a = nd.random.normal(shape=1)\n",
"a.attach_grad()\n",
"with autograd.record():\n",
" d = f(a)\n",
"d.backward()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's analyze the `f` function defined above. As you can see, it is piecewise linear in its input `a`. In other words, for any `a` there exists some constant such that for a given range `f(a) = g * a`. Consequently `d / a` allows us to verify that the gradient is correct:"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "10"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"[1.]\n",
"\n"
]
}
],
"source": [
"print(a.grad == (d / a))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Head gradients and the chain rule\n",
"\n",
"*Caution: This part is tricky and not necessary to understanding subsequent sections. That said, it is needed if you want to build new layers from scratch. You can skip this on a first read.*\n",
"\n",
"Sometimes when we call the backward method, e.g. `y.backward()`, where\n",
"`y` is a function of `x` we are just interested in the derivative of\n",
"`y` with respect to `x`. Mathematicians write this as\n",
"$\\frac{dy(x)}{dx}$. At other times, we may be interested in the\n",
"gradient of `z` with respect to `x`, where `z` is a function of `y`,\n",
"which in turn, is a function of `x`. That is, we are interested in\n",
"$\\frac{d}{dx} z(y(x))$. Recall that by the chain rule\n",
"\n",
"$$\\frac{d}{dx} z(y(x)) = \\frac{dz(y)}{dy} \\frac{dy(x)}{dx}.$$\n",
"\n",
"So, when ``y`` is part of a larger function ``z`` and we want ``x.grad`` to store $\\frac{dz}{dx}$, we can pass in the *head gradient* $\\frac{dz}{dy}$ as an input to ``backward()``. The default argument is ``nd.ones_like(y)``. See [Wikipedia](https://en.wikipedia.org/wiki/Chain_rule) for more details."
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"attributes": {
"classes": [],
"id": "",
"n": "11"
}
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"[[0. ]\n",
" [4. ]\n",
" [0.8 ]\n",
" [0.12]]\n",
"\n"
]
}
],
"source": [
"with autograd.record():\n",
" y = x * 2\n",
" z = y * x\n",
"\n",
"head_gradient = nd.array([10, 1., .1, .01])\n",
"z.backward(head_gradient)\n",
"print(x.grad)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Summary\n",
"\n",
"* MXNet provides an `autograd` package to automate the derivation process.\n",
"* MXNet's `autograd` package can be used to derive general imperative programs.\n",
"* The running modes of MXNet include the training mode and the prediction mode. We can determine the running mode by `autograd.is_training()`.\n",
"\n",
"## Exercises\n",
"\n",
"1. In the control flow example where we calculate the derivative of `d` with respect to `a`, what would happen if we changed the variable `a` to a random vector or matrix. At this point, the result of the calculation `f(a)` is no longer a scalar. What happens to the result? How do we analyze this?\n",
"1. Redesign an example of finding the gradient of the control flow. Run and analyze the result.\n",
"1. In a second-price auction (such as in eBay or in computational advertising), the winning bidder pays the second-highest price. Compute the gradient of the final price with respect to the winning bidder's bid using `autograd`. What does the result tell you about the mechanism? If you are curious to learn more about second-price auctions, check out this paper by [Edelman, Ostrovski and Schwartz, 2005](https://www.benedelman.org/publications/gsp-060801.pdf).\n",
"1. Why is the second derivative much more expensive to compute than the first derivative?\n",
"1. Derive the head gradient relationship for the chain rule. If you get stuck, use the [\"Chain rule\" article on Wikipedia](https://en.wikipedia.org/wiki/Chain_rule).\n",
"1. Assume $f(x) = \\sin(x)$. Plot $f(x)$ and $\\frac{df(x)}{dx}$ on a graph, where you computed the latter without any symbolic calculations, i.e. without exploiting that $f'(x) = \\cos(x)$.\n",
"\n",
"## Scan the QR Code to [Discuss](https://discuss.mxnet.io/t/2318)\n",
"\n",
"![](../img/qr_autograd.svg)"
]
}
],
"metadata": {
"language_info": {
"name": "python"
}
},
"nbformat": 4,
"nbformat_minor": 2
}