-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPyTorch.py
More file actions
78 lines (52 loc) · 997 Bytes
/
PyTorch.py
File metadata and controls
78 lines (52 loc) · 997 Bytes
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
import torch
import numpy as np
x = torch.empty(5,3)
print(x)
x = torch.rand(5,3)
print(x)
x = torch.zeros(5,3,dtype=torch.long)
print(x)
x = torch.tensor([5.5,3])
print(x)
x = x.new_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))
result = torch.empty(5, 3)
torch.add(x, y, out = result)
print(result)
# _ means in place
y.add_(x)
print(y)
print(x[:,1])
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)
print(x.size(), y.size(), z.size())
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)
x = torch.randn(1)
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))