Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

tensors.py 3.2 KB

You have to be logged in to leave a comment. Sign In
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
  1. #%%
  2. import torch
  3. print(torch.__version__)
  4. #%%
  5. tensor_array = torch.Tensor([[1, 2], [4, 5]])
  6. tensor_array
  7. #%%
  8. tensor_unitialized = torch.Tensor(3, 3) # unitialized
  9. torch.numel(tensor_unitialized) # number of elements
  10. #%%
  11. tensor_unitialized # will throw error Overflow when unpacking as memory not yet allocated
  12. #%%
  13. torch_initialized = torch.rand(2, 3)
  14. torch_initialized # random numbers
  15. #%%
  16. tensor_int = torch.randn(5, 3).type(torch.IntTensor) # just integers
  17. tensor_int # has dtype=torch.int32
  18. #%%
  19. tensor_long = torch.LongTensor([1.0, 2.0, 3.0])
  20. tensor_long # converted to longs even though entered as floats
  21. #%%
  22. tensor_byte = torch.ByteTensor([0, 261, 1, -5])
  23. tensor_byte # has dtype=torch.uint8, so numbers past 255 loop, e.g.: 261 ends up as 5
  24. #%%
  25. tensor_ones = torch.ones(10) # init to 1. floats
  26. tensor_ones
  27. #%%
  28. tensor_zeros = torch.zeros(10) # init zero vector (floats)
  29. tensor_zeros
  30. #%%
  31. tensor_eye = torch.eye(3) # identity matrix
  32. tensor_eye
  33. #%%
  34. non_zero = torch.nonzero(tensor_eye)
  35. non_zero # finds all index positions of non-zero elements
  36. #%%
  37. tensor_ones_shape_eye = torch.ones_like(tensor_eye)
  38. tensor_ones_shape_eye
  39. #%%
  40. # inplace operation something_ the underscore means the current tensor is getting altered, no _ postfix returns a new tensor
  41. initial_tensor = torch.rand(3, 3)
  42. initial_tensor.fill_(3) # overwrites all values with 3.
  43. #%%
  44. # out of place operation
  45. initial_tensor.fill(3) # throws an error because it doesn't exist - not all in-place ops have out-of-place equivalents that make sense
  46. #%%
  47. new_tensor = initial_tensor.add(4)
  48. new_tensor # ads 4 to a tensor copy
  49. #%%
  50. initial_tensor # same as before
  51. #%%
  52. initial_tensor.add_(5) # modifies in-place
  53. initial_tensor # changed forever
  54. #%%
  55. initial_tensor # same as before, unchanged
  56. #%%
  57. import numpy as np
  58. numpy_arr = np.array([1, 2, 3])
  59. numpy_arr
  60. #%%
  61. tensor = torch.from_numpy(numpy_arr) # great integration
  62. tensor # dtype=torch.int32
  63. #%%
  64. numpy_from_tensor = tensor.numpy()
  65. numpy_from_tensor
  66. #%%
  67. numpy_arr[1] = 4
  68. numpy_arr # same memory as the tensor!
  69. #%%
  70. tensor # same memory as the numpy array!
  71. #%%
  72. numpy_from_tensor # same memory as the tensor!
  73. #%%
  74. initial_tensor = torch.rand(2, 3)
  75. initial_tensor
  76. #%%
  77. initial_tensor[0, 2]
  78. #%%
  79. initial_tensor[:, 1:] # last two columns using brackets notation
  80. #%%
  81. initial_tensor.size()
  82. #%%
  83. initial_tensor.shape # same as size()
  84. #%%
  85. resized_tensor = initial_tensor.view(6) # same memory as the original tensor
  86. resized_tensor.shape
  87. #%%
  88. resized_tensor
  89. #%%
  90. initial_tensor[0, 2] = 0.1111
  91. resized_tensor # updates the view as well
  92. #%%
  93. resized_tensor = initial_tensor.view(3, 2)
  94. resized_tensor.shape # different shape
  95. #%%
  96. resized_matrix = initial_tensor.view(-1, 2) # infers the missing shape parameter, in this case it picks up 3
  97. resized_matrix.shape # 3 by 2
  98. #%%
  99. resized_matrix = initial_tensor.view(-1, 5) # incompatible reshape ops throw errors
  100. #%%
  101. initial_tensor
  102. #%%
  103. sorted_tensor, sorted_indices = torch. sort(initial_tensor)
  104. sorted_tensor
  105. #%%
  106. sorted_indices
  107. #%%
  108. sorted_tensor, sorted_indices = torch.sort(initial_tensor, dim=0)
  109. sorted_tensor
  110. #%%
  111. sorted_indices
  112. #%%
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...