Avisek Gupta

Indian Statistical Institute, Kolkata







Tutorial 2: Tensor Gradients and Optimization







1. Computing Tensor Gradients :

(i) Every Tensor has a flag ''requires_grad'' that allows for fine grained exclusion of subgraphs from gradient computation

(ii) By default, the gradients of user-created tensors are not calculated.







(iii) The backward() function calculates the gradients.







(iv) We cannot print the gradients of intermediate tensors y or z, even though they are calculated.

(v) Only if ALL inputs tensors DO NOT require gradients, the gradient of the output tensor will NOT be tracked.







2. Stop automatic computation of gradients on Tensors with "requires_grad=True" either by -

(i) Wrapping the code block in with torch.no_grad(), or

(ii) By using .detach() to get a new Tensor with the same content but that does not require gradients







3. A simple gradient descent code using only backward() to compute the gradients







Recap:

1. Computing Tensor Gradients :

(i) Every Tensor has a flag ''requires_grad'' that allows for fine grained exclusion of subgraphs from gradient computation

(ii) By default, the gradients of user-created tensors are not calculated.

(iii) The backward() function calculates the gradients.

(iv) We cannot print the gradients of intermediate tensors y or z, even though they are calculated.

(v) Only if ALL inputs tensors DO NOT require gradients, the gradient of the output tensor will NOT be tracked.

2. Stop automatic computation of gradients on Tensors with "requires_grad=True" either by -

(i) Wrapping the code block in with torch.no_grad(), or

(ii) By using .detach() to get a new Tensor with the same content but that does not require gradients

3. A simple gradient descent code using only backward() to compute the gradients