In [1]:
from __future__ import print_function
import torch

In [2]:
x = torch.empty(5, 3)
print(x)


tensor([[-4.9628e-20,  4.5626e-41, -4.9628e-20],
        [ 4.5626e-41,  8.9817e+05,  2.2995e+08],
        [ 1.6751e-37,  1.2121e-14,  1.6751e-37],
        [ 1.2121e-14,  2.3908e+08,  2.6724e+17],
        [ 1.0690e+18,  4.2760e+18,  1.2568e-40]])

In [3]:
x = torch.rand(5,3)
print(x)


tensor([[0.6238, 0.7552, 0.8748],
        [0.7621, 0.0135, 0.4693],
        [0.9803, 0.5155, 0.5493],
        [0.0230, 0.6803, 0.8941],
        [0.7281, 0.8170, 0.8018]])

In [4]:
import torch
x = torch.ones(2, 2, requires_grad=True)
print(x)
y = x + 2
print(y)


tensor([[1., 1.],
        [1., 1.]], requires_grad=True)
tensor([[3., 3.],
        [3., 3.]], grad_fn=<AddBackward0>)

In [5]:
print(y.grad_fn)
z = y* y * 3
out = z.mean()
print(z,out)


<AddBackward0 object at 0x7f3036692160>
tensor([[27., 27.],
        [27., 27.]], grad_fn=<MulBackward0>) tensor(27., grad_fn=<MeanBackward1>)

In [6]:
out.backward()

In [7]:
print(x.grad)


tensor([[4.5000, 4.5000],
        [4.5000, 4.5000]])

In [ ]: