1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81
|
from __future__ import print_function import torch import numpy as np
x = torch.empty(5 * 3) print(x)
x = torch.randn(5, 3) print(x)
x = torch.zeros(5, 3, dtype=torch.long) print(x)
x = torch.tensor([5.5, 3]) print(x)
x = torch.ones(5, 3, dtype=torch.double) print(x) x = torch.randn_like(x, dtype=torch.float) print(x)
print(x.size())
y = torch.rand(5, 3) print(x + y) print(torch.add(x, y)) y.add_(x) print(y)
result = torch.add(x, y) print(result)
result = torch.empty(5, 3) torch.add(x, y, out=result) print(result)
x = torch.randn(4, 4) y = x.view(16) z = x.view(-1, 8) print(x.size(), y.size(), z.size())
x = torch.randn(4, 4) print(x) print(x[0][0]) x = torch.randn(1) print(x) print(x.item())
a = torch.ones(5) print(a) b = a.numpy() print(b) a.add_(1)
print(a) print(b)
a = np.ones(5) b = torch.from_numpy(a) np.add(a, 1, out=a) print(a) print(b)
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))
|