Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use autograd.gradcheck in PyTorch?

The documentation does not include any example use case of gradcheck, where would it be useful?

like image 247
apostofes Avatar asked Aug 23 '19 13:08

apostofes


People also ask

How does Autograd work in PyTorch?

Autograd is reverse automatic differentiation system. Conceptually, autograd records a graph recording all of the operations that created the data as you execute operations, giving you a directed acyclic graph whose leaves are the input tensors and roots are the output tensors.

What is Autograd Variable in PyTorch?

Autograd is a PyTorch package for the differentiation for all operations on Tensors. It performs the backpropagation starting from a variable. In deep learning, this variable often holds the value of the cost function. Backward executes the backward pass and computes all the backpropagation gradients automatically.

What is Gradcheck?

A Graduation Check is the best way to make sure of your course progress as you approach the end of your course. These are carried out by the Graduation Team, often in consultation with Academic Chairs.

What does Loss backward () do?

Loss Function MSELoss which computes the mean-squared error between the input and the target. So, when we call loss. backward() , the whole graph is differentiated w.r.t. the loss, and all Variables in the graph will have their . grad Variable accumulated with the gradient.


1 Answers

There's an example use case provided in the documentation here:

https://pytorch.org/docs/master/notes/extending.html

You probably want to check if the backward method you implemented actually computes the derivatives of your function. It is possible by comparing with numerical approximations using small finite differences:

from torch.autograd import gradcheck

# gradcheck takes a tuple of tensors as input, check if your gradient
# evaluated with these tensors are close enough to numerical
# approximations and returns True if they all verify this condition.
input = (torch.randn(20,20,dtype=torch.double,requires_grad=True), torch.randn(30,20,dtype=torch.double,requires_grad=True))
test = gradcheck(linear, input, eps=1e-6, atol=1e-4)
print(test)

As the quote above suggests, the purpose of the gradcheck function is to verify that a custom backward function agrees with a numerical approximation of the gradient. The primary use case is when you're implementing a custom backward operation. In very few cases should you be implementing your own backward function in PyTorch. This is because PyTorch's autograd functionality takes care of computing gradients for the vast majority of operations.

The most obvious exceptions are

  1. You have a function which can't be expressed as a finite combination of other differentiable functions (for example, if you needed the incomplete gamma function, you might want to write your own forward and backward which used numpy and/or lookup tables).

  2. You're looking to speed up the computation of a particularly complicated expression for which the gradient could be drastically simplified after applying the chain rule.

like image 168
jodag Avatar answered Sep 22 '22 05:09

jodag