Autograd

Autograd is a technology utilized in PyTorch which computes a graph of the gradients of a function to each of its underlying parameters.

Topics

Sample Code

The following code demonstrates a simple example of using Autograd to compute a gradient.

def loss(t1, t2): sdiffs = (t1-t2)**2 return sdiffs.mean() args = torch.tensor([1.0,1.0], requires_grad=True) loss1 = loss(model(datapoints[0][0], *args), datapoints[0][1]) loss1.backward() grad = args.grad

In this example, there is a function of 3 arguments declared.
def function(t,w,b): return w*t+b

A loss function is defined. Given an output of the function, here labeled t1, and an output which represents the correct output labeled t2, we return the mean squared error.
def loss(t1, t2): sdiffs = (t1-t2)**2 return sdiffs.mean()

Calculating the Gradient on Sample Datapoint

The following code demonstrates a simple example of using Autograd to compute a gradient.

datapoints = [ [1,3] ] args = torch.tensor([1.0,1.0], requires_grad=True) loss1 = loss(model(datapoints[0][0], *args), datapoints[0][1]) loss1.backward() grad = args.grad

Full Code

The following code demonstrates a simple example of using Autograd to compute a gradient.

def function(t,w,b): return w*t+b def loss(t1, t2): sdiffs = (t1-t2)**2 return sdiffs.mean() args = torch.tensor([1.0,1.0], requires_grad=True) loss1 = loss(model(datapoints[0][0], *args), datapoints[0][1]) loss1.backward() grad = args.grad