Week 8 &9 Flashcards
(4 cards)
Question 1: Tensor Creation
Create a PyTorch tensor from the following list of lists and determine its properties:
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
- Create a tensor from data.
- What is the dimension (ndim) of the tensor?
- What is the shape (size()) of the tensor?
- tensor = torch.tensor(data)
- Dimension: 2
- Shape: torch.Size([3, 3])
Question 2: Random Tensor Generation
Generate a random tensor and explain its properties:
Write PyTorch code to create a 4x2 tensor filled with random numbers from a uniform distribution between 0 and 1. Then, state:
The shape of the tensor.
The data type of the tensor.
Answer:
tensor = torch.rand(4, 2)
Shape: torch.Size([4, 2])
Data type: torch.float32
Question 3: Tensor Operations
Perform the following tensor operation and compute the result:
Given two tensors:
a = torch.tensor([[1, 2], [3, 4]])
b = torch.tensor([[0, 1], [2, 3]])
Compute a + b and store the result in a new tensor result. What is the value of result?
Answer:
result = torch.add(a, b)
Value of result:
tensor([[1, 3],
[5, 7]])