PyTorch: The Basics

PyTorch allows you to dynamically define computational graphs. This is done by operating on Variables, which wrap PyTorch's Tensor objects.

Here is a simple example:

In [5]:
import torch
from torch.autograd import Variable
import numpy as np
In [6]:
def f(x):
    return x**2 + 2 * x
In [10]:
x = Variable(torch.from_numpy(np.array([4.0])), requires_grad=True)
y = f(x)
In [11]:
y.backward()
In [12]:
x.grad.data   # 2x + 2  for x = 4
Out[12]:
 10
[torch.DoubleTensor of size 1]
In [13]:
x = Variable(torch.from_numpy(np.array([5.0])), requires_grad=True)
y = f(x)
In [15]:
y.backward()
In [17]:
x.grad.data   # 2x + 2 for x = 5
Out[17]:
 12
[torch.DoubleTensor of size 1]

Note that unlike in TensorFlow, we defined the graph on the fly. That is why it was more convenient to define a function in Python: we call the function as part of constructing the graph.