Bikash Santra
Indian Statistical Institute, Kolkata
It’s a Python based scientific computing package targeted at two sets of audiences:
Tensors are similar to NumPy’s ndarrays, with the addition being that Tensors can also be used on a GPU to accelerate computing.
%matplotlib inline
from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x, x.dtype)
x = torch.rand(5, 3)
print(x, x.dtype)
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
x = torch.tensor([5.5, 3])
print(x)
help(torch.transpose)
x = torch.tensor([5.5, 3])
print(x, torch.transpose(x,0,0))
These methods will reuse properties of the input tensor, e.g. dtype, unless new values are provided by user.
x = x.new_ones(5, 3, dtype=torch.double) # new_* methods take in sizes
print(x)
x = torch.randn_like(x, dtype=torch.float) # override dtype!
print(x) # result has the same size
torch.Size
is in fact a tuple, so it supports all tuple operations.
y = x.size()
print(y[0])
There are multiple syntaxes for operations. In the following example, we will take a look at various operations.
y = torch.rand(5, 3)
print(x + y)
print(torch.add(x, y))
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
# adds x to y
y.add_(x)
print(y)
Any operation that mutates a tensor in-place is post-fixed with an ``_``. For example: ``x.copy_(y)``, ``x.t_()``, will change ``x``.
print(x[:, 1])
a = torch.zeros((2,3))
print(a)
b = a.t().clone()
print(b)
b[0,0] = 5
print(b)
print(a)
torch.view
:¶x = torch.randn(4, 4, 4)
y = x.view(64)
z = x.view(-1, 2, 8) # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())
.item()
to get the value as a Python number¶x = torch.randn(5)
print(x, type(x))
print(x.data[0], type(x.data[1]))
Read later:
100+ Tensor operations, including transposing, indexing, slicing,
mathematical operations, linear algebra, random numbers, etc.,
are described here <http://pytorch.org/docs/torch>
.
Converting a Torch Tensor to a NumPy array and vice versa is a breeze.
The Torch Tensor and NumPy array will share their underlying memory locations, and changing one will change the other.
a = torch.ones(5)
print(a, type(a))
b = a.numpy()
print(b, type(b))
See how the numpy array changed in value.
a.add_(10)
print(a)
print(b)
See how changing the np array changed the Torch Tensor automatically
import numpy as np
a = np.ones(5)
print(a)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(b)
All the Tensors on the CPU except a CharTensor support converting to NumPy and back.
Tensors can be moved onto any device using the .to
method.
# let us run this cell only if CUDA is available
# We will use ``torch.device`` objects to move tensors in and out of GPU
print(torch.cuda.is_available(), torch.cuda.get_device_name(0))
torch.cuda.empty_cache()
torch.cuda.set_device(0)
device = torch.device(0)
if torch.cuda.is_available():
y = torch.ones_like(x).to(device)
x = x.to(device) # or just use strings ``.to("cuda")``
z = x + y
print(z)
x = torch.rand(3,3)
y = torch.rand(3,3)
print(x)
print(y)
x = x.cuda()
print(x)
print(x+y)
y = y.cuda()
print(x+y)
From GPU to CPU
print(y.cpu())