## 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
import numpy as np
In [6]:
def f(x):
return x**2 + 2 * x
In [10]:
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]: