Het_Patel_HW_1[1]

.pdf

School

University of Texas, Dallas *

*We aren’t endorsed by this school

Course

6337

Subject

Mathematics

Date

May 1, 2024

Type

pdf

Pages

10

Uploaded by ProfAtom13452 on coursehero.com

1/28/24, 6:00 PM 1_Tensors_instructions.ipynb - Colaboratory https://colab.research.google.com/drive/1ELnxRfj_ngoJ78kNZscBDbTV50lRNgFP?authuser=1#scrollTo=TtYsJM4mJNdE&printMode=true 1/10 You have to submit two ±les for this part of the HW (1) ipynb (colab notebook) and (2) pdf file (pdf version of the colab file).** Files should be named as follows : FirstName_LastName_HW_1** HW1 - 15 Points import torch import time Create a torch Tensor of shape (5, 3) which is filled with zeros. Modify the tensor to set element (0, 2) to 10 and element (2, 0) to 100. Q1 : Create Tensor (1/2 Point) my_tensor = torch.zeros(5, 3) my_tensor.shape torch.Size([5, 3]) my_tensor tensor([[0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.], [0., 0., 0.]]) my_tensor[0,2] = 10 my_tensor[2,0] = 100 my_tensor tensor([[ 0., 0., 10.], [ 0., 0., 0.], [100., 0., 0.], [ 0., 0., 0.], [ 0., 0., 0.]]) You have following tensor as input: x=torch.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) Using only reshaping functions (like view, reshape, transpose, permute), you need to get at the following tensor as output: tensor([[ 0, 4, 8, 12, 16, 20], [ 1, 5, 9, 13, 17, 21], [ 2, 6, 10, 14, 18, 22], [ 3, 7, 11, 15, 19, 23]]) Q2: Reshape tensor (1/2 Point) x=torch.tensor([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23]) reshaped_tensor = x.view(6 ,4) output_tensor = reshaped_tensor.transpose(0,1) print(output_tensor)
1/28/24, 6:00 PM 1_Tensors_instructions.ipynb - Colaboratory https://colab.research.google.com/drive/1ELnxRfj_ngoJ78kNZscBDbTV50lRNgFP?authuser=1#scrollTo=TtYsJM4mJNdE&printMode=true 2/10 tensor([[ 0, 4, 8, 12, 16, 20], [ 1, 5, 9, 13, 17, 21], [ 2, 6, 10, 14, 18, 22], [ 3, 7, 11, 15, 19, 23]]) Slice the tensor x to get the following last row of x fourth column of x first three rows and first two columns - the shape of subtensor should be (3,2) odd valued rows and columns Q3: Slice tensor (1Point) x = torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 8, 10], [11, 12, 13, 14, 15]]) x tensor([[ 1, 2, 3, 4, 5], [ 6, 7, 8, 8, 10], [11, 12, 13, 14, 15]]) x.shape torch.Size([3, 5]) # Student Task: Retrieve the last row of the tensor 'x' # Hint: Negative indexing can help you select rows or columns counting from the end of the tensor. # Think about how you can select all columns for the desired row. last_row = x[-1] last_row tensor([11, 12, 13, 14, 15]) # Student Task: Retrieve the fourth column of the tensor 'x' # Hint: Pay attention to the indexing for both rows and columns. # Remember that indexing in Python starts from zero. fourth_column = x[:, 3] fourth_column tensor([ 4, 8, 14]) # Student Task: Retrieve the first 3 rows and first 2 columns from the tensor 'x'. # Hint: Use slicing to extract the required subset of rows and columns. first_3_rows_2_columns = x[:3 ,:2] first_3_rows_2_columns tensor([[ 1, 2], [ 6, 7], [11, 12]]) # Student Task: Retrieve the rows and columns with odd-indexed positions from the tensor 'x'. # Hint: Use stride slicing to extract the required subset of rows and columns with odd indices. odd_valued_rows_columns = x[::2, ::2] odd_valued_rows_columns tensor([[ 1, 3, 5], [11, 13, 15]]) Write the function that normalizes the columns of a matrix. You have to compute the mean and standard deviation of each column. Then for each element of the column, you subtract the mean and divide by the standard deviation. Q4 -Normalize Function (1/2 Points)
1/28/24, 6:00 PM 1_Tensors_instructions.ipynb - Colaboratory https://colab.research.google.com/drive/1ELnxRfj_ngoJ78kNZscBDbTV50lRNgFP?authuser=1#scrollTo=TtYsJM4mJNdE&printMode=true 3/10 # Given Data x = [[ 3, 60, 100, -100], [ 2, 20, 600, -600], [-5, 50, 900, -900]] # Convert to PyTorch Tensor and set to float X = torch.tensor(x) X= X.float() # Print shape and data type for verification print(X.shape) print(X.dtype) torch.Size([3, 4]) torch.float32 # Compute and display the mean and standard deviation of each column for reference X.mean(axis = 0) X.std(axis = 0) tensor([ 4.3589, 20.8167, 404.1452, 404.1452]) X.std(axis = 0) tensor([ 4.3589, 20.8167, 404.1452, 404.1452]) Your task starts here Your normalize_matrix function should take a PyTorch tensor x as input. It should return a tensor where the columns are normalized. After implementing your function, use the code provided to verify if the mean for each column in Z is close to zero and the standard deviation is 1. def normalize_matrix(x): # Calculate the mean along each column (think carefully , you will take mean along axis = 0 or 1) mean = torch.mean(x, dim=0) # Calculate the standard deviation along each column std = torch.std(x, dim=0) # Normalize each element in the columns by subtracting the mean and dividing by the standard deviation y = (x - mean) / std return y # Return the normalized matrix Z = normalize_matrix(X) Z tensor([[ 0.6882, 0.8006, -1.0722, 1.0722], [ 0.4588, -1.1209, 0.1650, -0.1650], [-1.1471, 0.3203, 0.9073, -0.9073]]) Z.mean(axis = 0) tensor([ 0.0000e+00, 4.9671e-08, 3.9736e-08, -3.9736e-08]) 1. Create a tensor A with values [1, 2, 3] . 2. Perform an in-place addition (use add_ method) of 5 to tensor A . 3. Then, create another tensor B with values [4, 5, 6] and perform an out-of-place addition of 5 . Print the memory addresses of A and B before and after the operations to demonstrate the difference in memory usage. Provide explanation Q5: In-place vs. Out-of-place Operations (1 Point)
1/28/24, 6:00 PM 1_Tensors_instructions.ipynb - Colaboratory https://colab.research.google.com/drive/1ELnxRfj_ngoJ78kNZscBDbTV50lRNgFP?authuser=1#scrollTo=TtYsJM4mJNdE&printMode=true 4/10 A = torch.tensor([1, 2, 3]) print('Original memory address of A:', id(A)) A.add_(5) print('Memory address of A after in-place addition:', id(A)) print('A after in-place addition:', A) B = torch.tensor([4, 5, 6]) print('Original memory address of B:', id(B)) B = B + 5 print('Memory address of B after out-of-place addition:', id(B)) print('B after out-of-place addition:', B) Original memory address of A: 140592758940000 Memory address of A after in-place addition: 140592758940000 A after in-place addition: tensor([6, 7, 8]) Original memory address of B: 140590330455648 Memory address of B after out-of-place addition: 140592748489408 B after out-of-place addition: tensor([ 9, 10, 11]) Provide Explanation for above question here : Justification When using the add_ method (in-place addition), the operation changes the tensor (A) that is already in place. Thus, both before and after the in- place addition, A's memory address stays the same. When addition occurs out of place (using B = B + 5), the result of the addition is used to generate a new tensor, to which variable B is subsequently transferred. As a result, following the out-of-place addition, B's memory address changes. Because in-place operations alter the current tensor rather than creating a new one, they can be memory-efficient. It's crucial to remember that in-place procedures might not be appropriate in all situations and may have unintended consequences. 1. Create two tensors X with shape (3, 1) and Y with shape (1, 3) . Perform an addition operation on X and Y . 2. Explain how broadcasting is applied in this operation by describing the shape changes that occur internally. Q6: Tensor Broadcasting (1 Point) X = torch.tensor([[2], [4], [6]]) Y = torch.tensor([[3, 6, 9]]) print('Original shapes:', X.shape, Y.shape) result = X + Y print('Result:', result) print('Result shape:', result.shape) Original shapes: torch.Size([3, 1]) torch.Size([1, 3]) Result: tensor([[ 5, 8, 11], [ 7, 10, 13], [ 9, 12, 15]]) Result shape: torch.Size([3, 3]) Provide Explanation for above question here : Broadcasting is used in the addition operation X + Y to make the operation consistent with the forms of X and Y. Broadcasting is a method that implicitly replicates values throughout dimensions to make disparate tensors compatible, hence enabling operations between them. This is the way that broadcasting is used in this particular instance: Extend Dimensions: X: (3, 1) and Y: (1, 3) are the initial forms. The first step in broadcasting is to enlarge the smaller tensor's dimensions to match the larger tensor's. Y is the smaller tensor; by duplicating its rows, it can expand from (1, 3) to (3, 3). Apply Element-wise Addition: After both tensors have expanded to the form (3, 3), element-wise addition is applied. Outcome: The greatest size along each of the X and Y dimensions is represented by the shape of (3, 3) in the result tensor. The appropriate elements from X and Y are added to create each element in the result tensor. To summarise, broadcasting makes tensors X and Y compatible for element-wise addition by automatically expanding and replicating dimensions, enabling a smooth addition operation between them. Q7: Linear Algebra Operations (1 Point)
Your preview ends here
Eager to read complete document? Join bartleby learn and gain access to the full version
  • Access to all documents
  • Unlimited textbook solutions
  • 24/7 expert homework help