-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasic_pytorch.py
72 lines (51 loc) · 1.11 KB
/
basic_pytorch.py
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
# pytorch is a replace ment of numpy
# DL research platform which provides maximum flexibility and speed
#tensors are similar to numpy ndarrays
#advantage than numpy is thesde can be used on a GPU to accelerate computing
from __future__ import print_function
import torch
import numpy
x = torch.empty(5,3)
print(x)
y = torch.rand(5,3)
print(y)
z = torch.zeros(5,3, dtype=torch.long)
print(z)
n = torch.tensor([5.5,3])
print(n)
x= x.new_ones(4,3,dtype=torch.double)
print(x)
x = torch.randn_like(x, dtype =torch.float64)
print(x)
print(x.size())
print(y+z)
print(torch.add(y,z))
result = torch.empty(5,3)
torch.add(y,z, out=result)
print(result)
y.add_(z)
print(y)
print(x[:,1]) #indexing like in numpy
x = torch.randn(1)
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())
#converting tensor to numpy
# we called this numpy bridging
a = torch.ones(5)
b = a.numpy()
print(b)
a.add_(1)
print(a)
print(b)
#converting numpy to tensor
a = np.ones(5)
b= torch.from_numpy(a)
np.add(a,1,out=a)
print(a)
print(b)
#CUDA tensors
#tensors can be moved onto any device