In [1]:
from __future__ import print_function
import torch
In [2]:
x = torch.empty(5, 3)
print(x)
In [3]:
x = torch.rand(5, 3)
print(x)
In [4]:
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
In [5]:
x = torch.tensor([5.5, 3])
print(x)
In [6]:
x = x.new_ones(5, 3, dtype=torch.double)
print(x)
x = torch.randn_like(x, dtype=torch.float)
print(x)
In [7]:
print(x.size())
In [8]:
y = torch.rand(5, 3)
print(x + y)
In [9]:
print(torch.add(x, y))
In [10]:
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
In [11]:
y.add_(x)
print(y)
In [12]:
print(x[:, 1])
In [13]:
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
In [14]:
x = torch.randn(1)
print(x)
print(x.item())
In [15]:
a = torch.ones(5)
print(a)
In [16]:
b = a.numpy()
print(b)
In [17]:
a.add_(1)
print(a)
print(b)
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)
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 [ ]: