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 [1]:
import torch
import numpy as np
In [2]:
def f(x):
return x**2 + 2 * x
In [3]:
y = f(x)
In [4]:
y.backward()
In [5]:
x.grad.data   # 2x + 2  for x = 4
Out[5]:
10
[torch.DoubleTensor of size 1]
In [6]: