02_Variable_Autograd


In [3]:
import torch
from torch.autograd import Variable

Tensor vs Variable


In [37]:
tensor = torch.FloatTensor([[1,2],[3,4]])            # build a tensor
x = Variable(tensor, requires_grad=True)      # build a variable, usually for compute gradients

print(tensor)       # [torch.FloatTensor of size 2x2]
print(x)     # [torch.FloatTensor of size 2x2]


 1  2
 3  4
[torch.FloatTensor of size 2x2]

Variable containing:
 1  2
 3  4
[torch.FloatTensor of size 2x2]


In [42]:
x.sum(), x.sqrt(), x**2


Out[42]:
(Variable containing:
  10
 [torch.FloatTensor of size 1], Variable containing:
  1.0000  1.4142
  1.7321  2.0000
 [torch.FloatTensor of size 2x2], Variable containing:
   1   4
   9  16
 [torch.FloatTensor of size 2x2])

Variables of a Variable


In [32]:
x.data


Out[32]:
 1  2
 3  4
[torch.FloatTensor of size 2x2]

In [34]:
x.requires_grad, x.grad, x.grad_fn, x.volatile


Out[34]:
(True, None, None, False)

Auto Graph & Gradient


In [123]:
# create graph

x = Variable(torch.FloatTensor([3]),requires_grad=True)
y = 2*x +3

x.requires_grad, y.requires_grad


Out[123]:
(True, True)

In [77]:
y.grad_fn.next_functions[0][0]


Out[77]:
<torch.autograd.function.MulConstantBackward at 0x7f97386e28b8>

In [124]:
y.backward()  # calculate dy / dx   == y.backward(torch.Tensor([1.0]))

In [125]:
x.grad # dy / dx is stored and accmulated


Out[125]:
Variable containing:
 2
[torch.FloatTensor of size 1]

In [126]:
x.grad.data.zero_() # empty gradient storage


Out[126]:
 0
[torch.FloatTensor of size 1]

In [127]:
y.backward(torch.Tensor([3.0])) # calculate dy / dx

In [128]:
x.grad


Out[128]:
Variable containing:
 6
[torch.FloatTensor of size 1]

multivariate ouput and gradient


In [167]:
x1 = Variable(torch.ones(2), requires_grad=True)
x2 = Variable(torch.ones(2))

In [168]:
y = x1*2 +  x2
y


Out[168]:
Variable containing:
 3
 3
[torch.FloatTensor of size 2]

In [169]:
y.backward(torch.Tensor([1,1]))

In [170]:
x1.grad, x2.grad


Out[170]:
(Variable containing:
  2
  2
 [torch.FloatTensor of size 2], None)

In [171]:
x1.grad.data.numpy()


Out[171]:
array([ 2.,  2.], dtype=float32)

In [ ]: