eaglelandsonce commited on
Commit
7d0d72e
·
verified ·
1 Parent(s): 9106f17

Update pages/3_WithExercises.py

Browse files
Files changed (1) hide show
  1. pages/3_WithExercises.py +22 -2
pages/3_WithExercises.py CHANGED
@@ -2,8 +2,6 @@ import streamlit as st
2
  import torch
3
  import io
4
  import sys
5
- import numpy as np
6
- import time
7
 
8
  # Function to execute the input code and capture print statements
9
  def execute_code(code):
@@ -29,6 +27,9 @@ exercises = {
29
  "Exercise 1: Create and Manipulate Tensors": {
30
  "description": "Tensors are the core data structure in PyTorch, similar to arrays in NumPy but with additional capabilities for GPU acceleration. This exercise introduces how to create tensors from various data sources such as lists and NumPy arrays. It also covers basic tensor operations like addition, subtraction, and element-wise multiplication, which are fundamental for manipulating data in PyTorch.",
31
  "code": '''
 
 
 
32
  # Creating tensors from Python lists
33
  # This creates a 1D tensor from the list [1, 2, 3]
34
  tensor_from_list = torch.tensor([1, 2, 3])
@@ -60,6 +61,8 @@ print("Element-wise Multiplication:", elementwise_multiplication)
60
  "Exercise 2: Tensor Indexing and Slicing": {
61
  "description": "Indexing and slicing allow you to access and manipulate specific elements and sub-tensors. This is crucial for tasks such as data preprocessing and manipulation in machine learning workflows. This exercise demonstrates how to index and slice tensors to extract and modify elements efficiently.",
62
  "code": '''
 
 
63
  # Creating a 2D tensor (matrix)
64
  tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
65
 
@@ -86,6 +89,8 @@ print("Modified tensor:\n", tensor)
86
  "Exercise 3: Tensor Reshaping and Transposing": {
87
  "description": "Reshaping and transposing tensors are common operations when preparing data for neural networks. Reshaping allows you to change the layout of tensor data without altering its data. Transposing changes the orientation of the data, which is useful for operations like matrix multiplication. This exercise covers reshaping using `view()` and `reshape()`, and transposing using `transpose()` and `permute()`.",
88
  "code": '''
 
 
89
  # Creating a 2D tensor
90
  tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
91
 
@@ -113,6 +118,8 @@ print("Permuted tensor shape:", permuted_tensor.shape)
113
  "Exercise 4: Tensor Operations and Broadcasting": {
114
  "description": "Broadcasting in PyTorch allows you to perform arithmetic operations on tensors of different shapes. This can simplify code and reduce the need for explicit reshaping. This exercise demonstrates matrix multiplication, the concept of broadcasting, and the application of element-wise functions like sine and exponential.",
115
  "code": '''
 
 
116
  # Matrix multiplication
117
  tensor1 = torch.tensor([[1, 2], [3, 4]])
118
  tensor2 = torch.tensor([[5, 6], [7, 8]])
@@ -146,6 +153,8 @@ print("Square root of tensor3:", sqrt_result)
146
  "Exercise 5: Tensor Initialization": {
147
  "description": "Proper initialization of tensors is crucial for neural network training and other machine learning tasks. This exercise explores various methods to initialize tensors, such as filling them with zeros, ones, random values, and specific distributions. Understanding these methods helps in setting up model parameters and test data.",
148
  "code": '''
 
 
149
  # Creating tensors filled with zeros and ones
150
  zeros_tensor = torch.zeros(3, 3)
151
  print("Zeros tensor:\n", zeros_tensor)
@@ -179,6 +188,8 @@ print("Uniform distribution tensor:\n", uniform_tensor)
179
  "Exercise 6: Tensor Arithmetic Operations": {
180
  "description": "Arithmetic operations on tensors are essential for numerous tasks in machine learning, including data preprocessing and neural network computations. This exercise covers basic arithmetic with tensors, in-place operations, and aggregation functions that compute summaries over tensor elements.",
181
  "code": '''
 
 
182
  # Creating a 1D tensor
183
  tensor = torch.tensor([1.0, 2.0, 3.0])
184
 
@@ -215,6 +226,8 @@ print("Maximum element:", max_all)
215
  "Exercise 7: Tensor Comparison and Logical Operations": {
216
  "description": "Comparison and logical operations enable you to make conditional selections and perform logical checks on tensors. This is crucial for tasks such as filtering data and implementing decision-based logic in machine learning workflows. This exercise demonstrates the use of comparison operators and logical functions, which are often used in neural network training and data analysis.",
217
  "code": '''
 
 
218
  # Creating two tensors for comparison
219
  tensor1 = torch.tensor([1, 2, 3])
220
  tensor2 = torch.tensor([3, 2, 1])
@@ -250,6 +263,8 @@ print("Conditional selection:", selected_tensor)
250
  "Exercise 8: Tensor Reduction Operations": {
251
  "description": "Reduction operations aggregate tensor elements along specified dimensions, providing summarized results like sums, means, and indices of maximum or minimum values. This exercise covers essential reduction operations, which are commonly used in loss functions and performance metrics in machine learning.",
252
  "code": '''
 
 
253
  # Creating a 2D tensor (matrix)
254
  tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
255
 
@@ -279,6 +294,8 @@ print("Indices of minimum elements along dimension 0:", argmin_dim0)
279
  "Exercise 9: Tensor Cloning and Detachment": {
280
  "description": "Cloning and detaching tensors are important when working with gradients in neural networks. Cloning creates a copy of a tensor, while detaching removes it from the computation graph to prevent gradient tracking. This exercise demonstrates these operations, which are crucial for managing tensor operations in complex neural networks.",
281
  "code": '''
 
 
282
  # Creating a tensor with gradient tracking enabled
283
  tensor = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
284
 
@@ -303,6 +320,9 @@ print("Detached tensor remains unchanged:", detached_tensor)
303
  "Exercise 10: GPU Operations with Tensors": {
304
  "description": "Utilizing GPUs can significantly speed up tensor operations, which is crucial for training large neural networks. This exercise covers checking for GPU availability, moving tensors to GPU, and performing operations on GPU to compare performance with CPU operations.",
305
  "code": '''
 
 
 
306
  # Check for GPU availability
307
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
308
  print("Using device:", device)
 
2
  import torch
3
  import io
4
  import sys
 
 
5
 
6
  # Function to execute the input code and capture print statements
7
  def execute_code(code):
 
27
  "Exercise 1: Create and Manipulate Tensors": {
28
  "description": "Tensors are the core data structure in PyTorch, similar to arrays in NumPy but with additional capabilities for GPU acceleration. This exercise introduces how to create tensors from various data sources such as lists and NumPy arrays. It also covers basic tensor operations like addition, subtraction, and element-wise multiplication, which are fundamental for manipulating data in PyTorch.",
29
  "code": '''
30
+ import torch
31
+ import numpy as np
32
+
33
  # Creating tensors from Python lists
34
  # This creates a 1D tensor from the list [1, 2, 3]
35
  tensor_from_list = torch.tensor([1, 2, 3])
 
61
  "Exercise 2: Tensor Indexing and Slicing": {
62
  "description": "Indexing and slicing allow you to access and manipulate specific elements and sub-tensors. This is crucial for tasks such as data preprocessing and manipulation in machine learning workflows. This exercise demonstrates how to index and slice tensors to extract and modify elements efficiently.",
63
  "code": '''
64
+ import torch
65
+
66
  # Creating a 2D tensor (matrix)
67
  tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
68
 
 
89
  "Exercise 3: Tensor Reshaping and Transposing": {
90
  "description": "Reshaping and transposing tensors are common operations when preparing data for neural networks. Reshaping allows you to change the layout of tensor data without altering its data. Transposing changes the orientation of the data, which is useful for operations like matrix multiplication. This exercise covers reshaping using `view()` and `reshape()`, and transposing using `transpose()` and `permute()`.",
91
  "code": '''
92
+ import torch
93
+
94
  # Creating a 2D tensor
95
  tensor = torch.tensor([[1, 2, 3], [4, 5, 6]])
96
 
 
118
  "Exercise 4: Tensor Operations and Broadcasting": {
119
  "description": "Broadcasting in PyTorch allows you to perform arithmetic operations on tensors of different shapes. This can simplify code and reduce the need for explicit reshaping. This exercise demonstrates matrix multiplication, the concept of broadcasting, and the application of element-wise functions like sine and exponential.",
120
  "code": '''
121
+ import torch
122
+
123
  # Matrix multiplication
124
  tensor1 = torch.tensor([[1, 2], [3, 4]])
125
  tensor2 = torch.tensor([[5, 6], [7, 8]])
 
153
  "Exercise 5: Tensor Initialization": {
154
  "description": "Proper initialization of tensors is crucial for neural network training and other machine learning tasks. This exercise explores various methods to initialize tensors, such as filling them with zeros, ones, random values, and specific distributions. Understanding these methods helps in setting up model parameters and test data.",
155
  "code": '''
156
+ import torch
157
+
158
  # Creating tensors filled with zeros and ones
159
  zeros_tensor = torch.zeros(3, 3)
160
  print("Zeros tensor:\n", zeros_tensor)
 
188
  "Exercise 6: Tensor Arithmetic Operations": {
189
  "description": "Arithmetic operations on tensors are essential for numerous tasks in machine learning, including data preprocessing and neural network computations. This exercise covers basic arithmetic with tensors, in-place operations, and aggregation functions that compute summaries over tensor elements.",
190
  "code": '''
191
+ import torch
192
+
193
  # Creating a 1D tensor
194
  tensor = torch.tensor([1.0, 2.0, 3.0])
195
 
 
226
  "Exercise 7: Tensor Comparison and Logical Operations": {
227
  "description": "Comparison and logical operations enable you to make conditional selections and perform logical checks on tensors. This is crucial for tasks such as filtering data and implementing decision-based logic in machine learning workflows. This exercise demonstrates the use of comparison operators and logical functions, which are often used in neural network training and data analysis.",
228
  "code": '''
229
+ import torch
230
+
231
  # Creating two tensors for comparison
232
  tensor1 = torch.tensor([1, 2, 3])
233
  tensor2 = torch.tensor([3, 2, 1])
 
263
  "Exercise 8: Tensor Reduction Operations": {
264
  "description": "Reduction operations aggregate tensor elements along specified dimensions, providing summarized results like sums, means, and indices of maximum or minimum values. This exercise covers essential reduction operations, which are commonly used in loss functions and performance metrics in machine learning.",
265
  "code": '''
266
+ import torch
267
+
268
  # Creating a 2D tensor (matrix)
269
  tensor = torch.tensor([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
270
 
 
294
  "Exercise 9: Tensor Cloning and Detachment": {
295
  "description": "Cloning and detaching tensors are important when working with gradients in neural networks. Cloning creates a copy of a tensor, while detaching removes it from the computation graph to prevent gradient tracking. This exercise demonstrates these operations, which are crucial for managing tensor operations in complex neural networks.",
296
  "code": '''
297
+ import torch
298
+
299
  # Creating a tensor with gradient tracking enabled
300
  tensor = torch.tensor([1.0, 2.0, 3.0], requires_grad=True)
301
 
 
320
  "Exercise 10: GPU Operations with Tensors": {
321
  "description": "Utilizing GPUs can significantly speed up tensor operations, which is crucial for training large neural networks. This exercise covers checking for GPU availability, moving tensors to GPU, and performing operations on GPU to compare performance with CPU operations.",
322
  "code": '''
323
+ import torch
324
+ import time
325
+
326
  # Check for GPU availability
327
  device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
328
  print("Using device:", device)