In [ ]:
%matplotlib inline
It’s a Python based scientific computing package targeted at two sets of audiences:
Tensors ^^^^^^^
Tensors are similar to numpy’s ndarrays, with the addition being that Tensors can also be used on a GPU to accelerate computing.
In [ ]:
from __future__ import print_function
import torch
Construct a 5x3 matrix, uninitialized:
In [ ]:
x = torch.Tensor(5, 3)
print(x)
Construct a randomly initialized matrix
In [ ]:
x = torch.rand(5, 3)
print(x)
Get its size
In [ ]:
print(x.size())
``torch.Size`` is in fact a tuple, so it supports the same operations
Operations ^^^^^^^^^^ There are multiple syntaxes for operations. Let's see addition as an example
Addition: syntax 1
In [ ]:
y = torch.rand(5, 3)
print(x + y)
Addition: syntax 2
In [ ]:
print(torch.add(x, y))
Addition: giving an output tensor
In [ ]:
result = torch.Tensor(5, 3)
torch.add(x, y, out=result)
print(result)
Addition: in-place
In [ ]:
# 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``.
You can use standard numpy-like indexing with all bells and whistles!
In [ ]:
print(x[:, 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.
Converting torch Tensor to numpy Array ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
In [ ]:
a = torch.ones(5)
print(a)
In [ ]:
b = a.numpy()
print(b)
See how the numpy array changed in value.
In [ ]:
a.add_(1)
print(a)
print(b)
Converting numpy Array to torch Tensor ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ See how changing the np array changed the torch Tensor automatically
In [ ]:
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
In [ ]:
# let us run this cell only if CUDA is available
if torch.cuda.is_available():
x = x.cuda()
y = y.cuda()
x + y