Getting Started

Tensors


In [1]:
from __future__ import print_function
import torch

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


tensor([[ 0.0000e+00, -4.6566e-10,  0.0000e+00],
        [-4.6566e-10,  1.4569e-19,  2.7517e+12],
        [ 7.5338e+28,  3.0313e+32,  6.3828e+28],
        [ 1.4603e-19,  1.0899e+27,  6.8943e+34],
        [ 1.1835e+22,  7.0976e+22,  1.8515e+28]])

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


tensor([[0.1440, 0.7908, 0.0389],
        [0.3627, 0.9841, 0.7017],
        [0.3116, 0.3235, 0.9572],
        [0.5752, 0.0709, 0.6739],
        [0.3352, 0.4004, 0.3672]])

In [4]:
x = torch.zeros(5, 3, dtype=torch.long)
print(x)


tensor([[0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0],
        [0, 0, 0]])

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


tensor([5.5000, 3.0000])

In [6]:
x = x.new_ones(5, 3, dtype=torch.double)
print(x)

x = torch.randn_like(x, dtype=torch.float)
print(x)


tensor([[1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.],
        [1., 1., 1.]], dtype=torch.float64)
tensor([[-0.5481,  0.3311,  0.7185],
        [ 0.1143, -1.4274, -0.5826],
        [-1.5435,  0.0898,  0.0007],
        [ 1.0425,  2.3402,  0.2768],
        [ 0.0089, -0.6538, -0.2950]])

In [7]:
print(x.size())


torch.Size([5, 3])

Operations


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


tensor([[-0.1936,  0.5664,  0.8753],
        [ 0.8227, -0.5682, -0.0247],
        [-0.9666,  0.7663,  0.3745],
        [ 1.6932,  2.7058,  0.4882],
        [ 0.5138, -0.3284, -0.2289]])

In [9]:
print(torch.add(x, y))


tensor([[-0.1936,  0.5664,  0.8753],
        [ 0.8227, -0.5682, -0.0247],
        [-0.9666,  0.7663,  0.3745],
        [ 1.6932,  2.7058,  0.4882],
        [ 0.5138, -0.3284, -0.2289]])

In [10]:
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)


tensor([[-0.1936,  0.5664,  0.8753],
        [ 0.8227, -0.5682, -0.0247],
        [-0.9666,  0.7663,  0.3745],
        [ 1.6932,  2.7058,  0.4882],
        [ 0.5138, -0.3284, -0.2289]])

In [11]:
y.add_(x)
print(y)


tensor([[-0.1936,  0.5664,  0.8753],
        [ 0.8227, -0.5682, -0.0247],
        [-0.9666,  0.7663,  0.3745],
        [ 1.6932,  2.7058,  0.4882],
        [ 0.5138, -0.3284, -0.2289]])

In [12]:
print(x[:, 1])


tensor([ 0.3311, -1.4274,  0.0898,  2.3402, -0.6538])

In [13]:
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())


torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

In [14]:
x = torch.randn(1)
print(x)
print(x.item())


tensor([1.6489])
1.6489466428756714

NumPy Bridge

Converting a Torch Tensor to a NumPy Array


In [15]:
a = torch.ones(5)
print(a)


tensor([1., 1., 1., 1., 1.])

In [16]:
b = a.numpy()
print(b)


[1. 1. 1. 1. 1.]

In [17]:
a.add_(1)
print(a)
print(b)


tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]

Converting NumPy Array to Torch Tensor


In [18]:
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)


[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)

CUDA Tensors


In [19]:
if torch.cuda.is_available():
    device = torch.device("cuda")
    y = torch.ones_like(x, device=device)
    x = x.to(device)
    z = x + y
    print(z)
    print(z.to("cpu", torch.double))

In [ ]: