Note: This is an R port of the official tutorial available here. All credits goes to Soumith Chintala.

Central to all neural networks in torch is the autograd functionality. Let’s first briefly visit this, and we will then go to training our first neural network.

Autograd provides automatic differentiation for all operations on Tensors. It is a define-by-run framework, which means that your backprop is defined by how your code is run, and that every single iteration can be different.

Let us see this in more simple terms with some examples.

Tensor

torch_tensor is the central class of the package. If you set its attribute $requires_grad as TRUE, it starts to track all operations on it. When you finish your computation you can call $backward() and have all the gradients computed automatically. The gradient for this tensor will be accumulated into $grad attribute.

To stop a tensor from tracking history, you can call $detach() to detach it from the computation history, and to prevent future computation from being tracked.

To prevent tracking history (and using memory), you can also wrap the code block in with_no_grad({<code>}). This can be particularly helpful when evaluating a model because the model may have trainable parameters with requires_grad=TRUE, but for which we don’t need the gradients.

There’s one more class which is very important for autograd implementation - a autograd_function.

Tensor and Function are interconnected and build up an acyclic graph, that encodes a complete history of computation. Each tensor has a $grad_fn attribute that references an autograd_function that has created the Tensor (except for Tensors created by the user - their grad_fn is NULL).

If you want to compute the derivatives, you can call $backward() on a Tensor. If Tensor is a scalar (i.e. it holds a one element data), you don’t need to specify any arguments to backward(), however if it has more elements, you need to specify a gradient argument that is a tensor of matching shape.

Create a tensor and set requires_grad=TRUE to track computation with it:

x <- torch_ones(2, 2, requires_grad = TRUE)
x
#> torch_tensor
#>  1  1
#>  1  1
#> [ CPUFloatType{2,2} ]

Do a tensor operation:

y <- x + 2
y
#> torch_tensor
#>  3  3
#>  3  3
#> [ CPUFloatType{2,2} ]

y was created as a result of an operation, so it has a grad_fn.

y$grad_fn
#> AddBackward1

Do more operations on y

z <- y * y * 3
z
#> torch_tensor
#>  27  27
#>  27  27
#> [ CPUFloatType{2,2} ]
out <- z$mean()
out
#> torch_tensor
#> 27
#> [ CPUFloatType{} ]

$requires_grad_( ... ) changes an existing Tensor’s requires_grad flag in-place. The input flag defaults to FALSE if not given.

a <- torch_randn(2, 2)
a <- (a * 3) / (a - 1)
a$requires_grad
#> [1] FALSE
a$requires_grad_(TRUE)
#> torch_tensor
#>  0.2653  1.6269
#>  1.1373  1.1909
#> [ CPUFloatType{2,2} ]
a$requires_grad
#> [1] TRUE
b <- (a * a)$sum()
b$grad_fn
#> SumBackward0

Gradients

Let’s backprop now. Because out contains a single scalar, out$backward() is equivalent to out$backward(torch.tensor(1.)).

out$backward()

Print gradients d(out)/dx

x$grad
#> torch_tensor
#>  4.5000  4.5000
#>  4.5000  4.5000
#> [ CPUFloatType{2,2} ]

You should have got a matrix of 4.5. Let’s call the out Tensor \(o\).

We have that \(o = \frac{1}{4}\sum_i z_i\), \(z_i = 3(x_i+2)^2\) and \(z_i\bigr\rvert_{x_i=1} = 27\). Therefore, \(\frac{\partial o}{\partial x_i} = \frac{3}{2}(x_i+2)\), hence \(\frac{\partial o}{\partial x_i}\bigr\rvert_{x_i=1} = \frac{9}{2} = 4.5\).

Mathematically, if you have a vector valued function \(\vec{y}=f(\vec{x})\), then the gradient of \(\vec{y}\) with respect to \(\vec{x}\) is a Jacobian matrix:

\[ J=\left(\begin{array}{ccc} \frac{\partial y_{1}}{\partial x_{1}} & \cdots & \frac{\partial y_{1}}{\partial x_{n}}\\ \vdots & \ddots & \vdots\\ \frac{\partial y_{m}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}} \end{array}\right) \]

Generally speaking, autograd is an engine for computing vector-Jacobian product. That is, given any vector \(v=\left(\begin{array}{cccc} v_{1} & v_{2} & \cdots & v_{m}\end{array}\right)^{T}\), compute the product \(v^{T}\cdot J\). If \(v\) happens to be the gradient of a scalar function \(l=g\left(\vec{y}\right)\), that is, \(v=\left(\begin{array}{ccc}\frac{\partial l}{\partial y_{1}} & \cdots & \frac{\partial l}{\partial y_{m}}\end{array}\right)^{T}\), then by the chain rule, the vector-Jacobian product would be the gradient of \(l\) with respect to \(\vec{x}\):

\[ J^{T}\cdot v=\left(\begin{array}{ccc} \frac{\partial y_{1}}{\partial x_{1}} & \cdots & \frac{\partial y_{m}}{\partial x_{1}}\\ \vdots & \ddots & \vdots\\ \frac{\partial y_{1}}{\partial x_{n}} & \cdots & \frac{\partial y_{m}}{\partial x_{n}} \end{array}\right)\left(\begin{array}{c} \frac{\partial l}{\partial y_{1}}\\ \vdots\\ \frac{\partial l}{\partial y_{m}} \end{array}\right)=\left(\begin{array}{c} \frac{\partial l}{\partial x_{1}}\\ \vdots\\ \frac{\partial l}{\partial x_{n}} \end{array}\right) \]

(Note that \(v^{T}\cdot J\) gives a row vector which can be treated as a column vector by taking \(J^{T}\cdot v\).)

This characteristic of vector-Jacobian product makes it very convenient to feed external gradients into a model that has non-scalar output.

Now let’s take a look at an example of vector-Jacobian product:

x <- torch_randn(3, requires_grad=TRUE)
y <- 100 * x
y
#> torch_tensor
#> -35.0288
#> -209.1356
#>  64.9385
#> [ CPUFloatType{3} ]

Now in this case y is no longer a scalar. autograd could not compute the full Jacobian directly, but if we just want the vector-Jacobian product, simply pass the vector to backward as argument:

v <- torch_tensor(c(0.1, 1.0, 0.0001))
y$backward(v)

x$grad
#> torch_tensor
#>  1.0000e+01
#>  1.0000e+02
#>  1.0000e-02
#> [ CPUFloatType{3} ]

You can also stop autograd from tracking history on Tensors with $requires_grad=TRUE either by wrapping the code block in with with_no_grad():

x$requires_grad
#> [1] TRUE
(x ** 2)$requires_grad
#> [1] TRUE

with_no_grad({
  print((x ** 2)$requires_grad)
})
#> [1] FALSE
x$requires_grad
#> [1] TRUE
y <- x$detach()
y$requires_grad
#> [1] FALSE
x$eq(y)$all()
#> torch_tensor
#> 1
#> [ CPUBoolType{} ]

Read Later:

Document about help(autograd_function), vignette("using-autograd"), vignette("extending-autograd").