Spaces:
Running
Running
Update pages/3_WithExercises.py
Browse files- pages/3_WithExercises.py +12 -12
pages/3_WithExercises.py
CHANGED
|
@@ -81,7 +81,7 @@ print("Third column:", column)
|
|
| 81 |
# Modifying elements
|
| 82 |
# Changing the first element of the tensor to 10
|
| 83 |
tensor[0, 0] = 10
|
| 84 |
-
print("Modified tensor
|
| 85 |
'''
|
| 86 |
},
|
| 87 |
"Exercise 3: Tensor Reshaping and Transposing": {
|
|
@@ -94,16 +94,16 @@ tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
|
|
| 94 |
# Reshaping the tensor
|
| 95 |
# Changing the shape of the tensor to (3, 2)
|
| 96 |
reshaped_tensor = tensor.view(3, 2)
|
| 97 |
-
print("Reshaped tensor
|
| 98 |
|
| 99 |
# Another way to reshape using reshape()
|
| 100 |
reshaped_tensor2 = tensor.reshape(-1) # Flattening the tensor
|
| 101 |
-
print("Reshaped tensor (flattened)
|
| 102 |
|
| 103 |
# Transposing the tensor
|
| 104 |
# Swapping the dimensions of the tensor (transpose rows and columns)
|
| 105 |
transposed_tensor = tensor.t()
|
| 106 |
-
print("Transposed tensor
|
| 107 |
|
| 108 |
# Using permute for higher-dimensional tensors
|
| 109 |
tensor_3d = torch.randn(2, 3, 4) # Creating a random 3D tensor
|
|
@@ -129,7 +129,7 @@ print("Matrix multiplication result:\n", matmul_result)
|
|
| 129 |
tensor3 = torch.tensor([1, 2, 3])
|
| 130 |
tensor4 = torch.tensor([[1], [2], [3]])
|
| 131 |
broadcast_result = tensor3 + tensor4
|
| 132 |
-
print("Broadcasting result
|
| 133 |
|
| 134 |
# Element-wise functions
|
| 135 |
# Applying sine function element-wise
|
|
@@ -152,32 +152,32 @@ print("Square root of tensor3:", sqrt_result)
|
|
| 152 |
|
| 153 |
# Creating tensors filled with zeros and ones
|
| 154 |
zeros_tensor = torch.zeros(3, 3)
|
| 155 |
-
print("Zeros tensor
|
| 156 |
|
| 157 |
ones_tensor = torch.ones(3, 3)
|
| 158 |
-
print("Ones tensor
|
| 159 |
|
| 160 |
# Randomly initialized tensors
|
| 161 |
# Uniform distribution in the range [0, 1)
|
| 162 |
rand_tensor = torch.rand(3, 3)
|
| 163 |
-
print("Uniform random tensor
|
| 164 |
|
| 165 |
# Normal distribution with mean 0 and variance 1
|
| 166 |
randn_tensor = torch.randn(3, 3)
|
| 167 |
-
print("Normal random tensor
|
| 168 |
|
| 169 |
# Random integers in the range [0, 10)
|
| 170 |
randint_tensor = torch.randint(0, 10, (3, 3))
|
| 171 |
-
print("Random integer tensor
|
| 172 |
|
| 173 |
# Initializing tensors with specific distributions
|
| 174 |
# Normal distribution with custom mean and standard deviation
|
| 175 |
normal_tensor = torch.normal(mean=0, std=1, size=(3, 3))
|
| 176 |
-
print("Normal distribution tensor
|
| 177 |
|
| 178 |
# Uniform distribution in a custom range [0, 1)
|
| 179 |
uniform_tensor = torch.empty(3, 3).uniform_(0, 1)
|
| 180 |
-
print("Uniform distribution tensor
|
| 181 |
'''
|
| 182 |
},
|
| 183 |
"Exercise 6: Tensor Arithmetic Operations": {
|
|
|
|
| 81 |
# Modifying elements
|
| 82 |
# Changing the first element of the tensor to 10
|
| 83 |
tensor[0, 0] = 10
|
| 84 |
+
print("Modified tensor:", tensor)
|
| 85 |
'''
|
| 86 |
},
|
| 87 |
"Exercise 3: Tensor Reshaping and Transposing": {
|
|
|
|
| 94 |
# Reshaping the tensor
|
| 95 |
# Changing the shape of the tensor to (3, 2)
|
| 96 |
reshaped_tensor = tensor.view(3, 2)
|
| 97 |
+
print("Reshaped tensor:", reshaped_tensor)
|
| 98 |
|
| 99 |
# Another way to reshape using reshape()
|
| 100 |
reshaped_tensor2 = tensor.reshape(-1) # Flattening the tensor
|
| 101 |
+
print("Reshaped tensor (flattened):", reshaped_tensor2)
|
| 102 |
|
| 103 |
# Transposing the tensor
|
| 104 |
# Swapping the dimensions of the tensor (transpose rows and columns)
|
| 105 |
transposed_tensor = tensor.t()
|
| 106 |
+
print("Transposed tensor:", transposed_tensor)
|
| 107 |
|
| 108 |
# Using permute for higher-dimensional tensors
|
| 109 |
tensor_3d = torch.randn(2, 3, 4) # Creating a random 3D tensor
|
|
|
|
| 129 |
tensor3 = torch.tensor([1, 2, 3])
|
| 130 |
tensor4 = torch.tensor([[1], [2], [3]])
|
| 131 |
broadcast_result = tensor3 + tensor4
|
| 132 |
+
print("Broadcasting result:", broadcast_result)
|
| 133 |
|
| 134 |
# Element-wise functions
|
| 135 |
# Applying sine function element-wise
|
|
|
|
| 152 |
|
| 153 |
# Creating tensors filled with zeros and ones
|
| 154 |
zeros_tensor = torch.zeros(3, 3)
|
| 155 |
+
print("Zeros tensor:", zeros_tensor)
|
| 156 |
|
| 157 |
ones_tensor = torch.ones(3, 3)
|
| 158 |
+
print("Ones tensor:", ones_tensor)
|
| 159 |
|
| 160 |
# Randomly initialized tensors
|
| 161 |
# Uniform distribution in the range [0, 1)
|
| 162 |
rand_tensor = torch.rand(3, 3)
|
| 163 |
+
print("Uniform random tensor:", rand_tensor)
|
| 164 |
|
| 165 |
# Normal distribution with mean 0 and variance 1
|
| 166 |
randn_tensor = torch.randn(3, 3)
|
| 167 |
+
print("Normal random tensor:", randn_tensor)
|
| 168 |
|
| 169 |
# Random integers in the range [0, 10)
|
| 170 |
randint_tensor = torch.randint(0, 10, (3, 3))
|
| 171 |
+
print("Random integer tensor:", randint_tensor)
|
| 172 |
|
| 173 |
# Initializing tensors with specific distributions
|
| 174 |
# Normal distribution with custom mean and standard deviation
|
| 175 |
normal_tensor = torch.normal(mean=0, std=1, size=(3, 3))
|
| 176 |
+
print("Normal distribution tensor:", normal_tensor)
|
| 177 |
|
| 178 |
# Uniform distribution in a custom range [0, 1)
|
| 179 |
uniform_tensor = torch.empty(3, 3).uniform_(0, 1)
|
| 180 |
+
print("Uniform distribution tensor:", uniform_tensor)
|
| 181 |
'''
|
| 182 |
},
|
| 183 |
"Exercise 6: Tensor Arithmetic Operations": {
|