`library(torch)`

So far, all we’ve been using from torch is *tensors*, but we’ve been performing all calculations ourselves – the computing the predictions, the loss, the gradients (and thus, the necessary updates to the weights), and the new weight values. In this chapter, we’ll make a significant change: Namely, we spare ourselves the cumbersome calculation of gradients, and have torch do it for us.

Before we see that in action, let’s get some more background.

Torch uses a module called *autograd* to record operations performed on tensors, and store what has to be done to obtain the respective gradients. These actions are stored as functions, and those functions are applied in order when the gradient of the output (normally, the loss) with respect to those tensors is calculated: starting from the output node and *propagating* gradients *back* through the network. This is a form of *reverse mode automatic differentiation*.

As users, we can see a bit of this implementation. As a prerequisite for this “recording” to happen, tensors have to be created with `requires_grad = TRUE`

. E.g.

`<- torch_ones(2,2, requires_grad = TRUE) x `

To be clear, this is a tensor *with respect to which* gradients have to be calculated – normally, a tensor representing a weight or a bias, not the input data ^{1}. If we now perform some operation on that tensor, assigning the result to `y`

`<- x$mean() y `

we find that `y`

now has a non-empty `grad_fn`

that tells torch how to compute the gradient of `y`

with respect to `x`

:

`$grad_fn y`

Actual computation of gradients is triggered by calling `backward()`

on the output tensor.

`$backward() y`

That executed, `x`

now has a non-empty field `grad`

that stores the gradient of `y`

with respect to `x`

:

`$grad x`

With a longer chain of computations, we can peek at how torch builds up a graph of backward operations.

Here is a slightly more complex example. We call `retain_grad()`

on `y`

and `z`

just for demonstration purposes; by default, intermediate gradients – while of course they have to be computed – aren’t stored, in order to save memory.

```
<- torch_ones(2,2, requires_grad = TRUE)
x1 <- torch_tensor(1.1, requires_grad = TRUE)
x2 <- x1 * (x2 + 2)
y $retain_grad()
y<- y$pow(2) * 3
z $retain_grad()
z<- z$mean() out
```

Starting from `out$grad_fn`

, we can follow the graph all back to the leaf nodes:

```
# how to compute the gradient for mean, the last operation executed
$grad_fn
out# how to compute the gradient for the multiplication by 3 in z = y$pow(2) * 3
$grad_fn$next_functions
out# how to compute the gradient for pow in z = y.pow(2) * 3
$grad_fn$next_functions[[1]]$next_functions
out# how to compute the gradient for the multiplication in y = x * (x + 2)
$grad_fn$next_functions[[1]]$next_functions[[1]]$next_functions
out# how to compute the gradient for the two branches of y = x * (x + 2),
# where the left branch is a leaf node (AccumulateGrad for x1)
$grad_fn$next_functions[[1]]$next_functions[[1]]$next_functions[[1]]$next_functions
out# here we arrive at the other leaf node (AccumulateGrad for x2)
$grad_fn$next_functions[[1]]$next_functions[[1]]$next_functions[[1]]$next_functions[[2]]$next_functions out
```

After calling `out$backward()`

, all tensors in the graph will have their respective gradients created. Without our calls to `retain_grad`

above, `z$grad`

and `y$grad`

would be empty:

```
$backward()
out$grad
z$grad
y$grad
x2$grad x1
```

Thus acquainted with autograd, we’re ready to modify our example.

For a single new line calling `loss$backward()`

, now a number of lines (that did manual backprop) are gone:

```
### generate training data -----------------------------------------------------
# input dimensionality (number of input features)
<- 3
d_in # output dimensionality (number of predicted features)
<- 1
d_out # number of observations in training set
<- 100
n # create random data
<- torch_randn(n, d_in)
x <- x[,1]*0.2 - x[..,2]*1.3 - x[..,3]*0.5 + torch_randn(n)
y <- y$unsqueeze(dim = 1)
y ### initialize weights ---------------------------------------------------------
# dimensionality of hidden layer
<- 32
d_hidden # weights connecting input to hidden layer
<- torch_randn(d_in, d_hidden, requires_grad = TRUE)
w1 # weights connecting hidden to output layer
<- torch_randn(d_hidden, d_out, requires_grad = TRUE)
w2 # hidden layer bias
<- torch_zeros(1, d_hidden, requires_grad = TRUE)
b1 # output layer bias
<- torch_zeros(1, d_out,requires_grad = TRUE)
b2 ### network parameters ---------------------------------------------------------
<- 1e-4
learning_rate ### training loop --------------------------------------------------------------
for (t in 1:200) {
### -------- Forward pass --------
<- x$mm(w1)$add(b1)$clamp(min = 0)$mm(w2)$add(b2)
y_pred ### -------- compute loss --------
<- (y_pred - y)$pow(2)$mean()
loss if (t %% 10 == 0) cat(t, as_array(loss), "\n")
### -------- Backpropagation --------
# compute the gradient of loss with respect to all tensors with requires_grad = True.
$backward()
loss
### -------- Update weights --------
# Wrap in torch.no_grad() because this is a part we DON'T want to record for automatic gradient computation
with_no_grad({
$sub_(learning_rate * w1$grad)
w1$sub_(learning_rate * w2$grad)
w2$sub_(learning_rate * b1$grad)
b1$sub_(learning_rate * b2$grad)
b2
# Zero the gradients after every pass, because they'd accumulate otherwise
$grad$zero_()
w1$grad$zero_()
w2$grad$zero_()
b1$grad$zero_()
b2
})
}
```

We still manually compute the forward pass, and we still manually update the weights. In the last two chapters of this section, we’ll see how these parts of the logic can be made more modular and reusable, as well.

Unless we

*want*to change the data, as in adversarial example generation↩︎