Complete the code to create a tensor from a list of numbers.
import torch my_tensor = torch.[1]([1, 2, 3, 4]) print(my_tensor)
The torch.tensor function creates a tensor from a Python list or array.
Complete the code to create a 3x3 tensor filled with zeros.
import torch zero_tensor = torch.[1]((3, 3)) print(zero_tensor)
torch.zeros creates a tensor filled with zeros of the given shape.
Fix the error in the code to create a 2x4 tensor filled with ones.
import torch one_tensor = torch.[1]([2, 4]) print(one_tensor)
torch.ones creates a tensor filled with ones. The shape must be passed as a tuple, but a list also works here.
Fill both blanks to create a 4x2 tensor with random values between 0 and 1.
import torch random_tensor = torch.[1]([2]) print(random_tensor)
torch.rand creates a tensor with random values between 0 and 1. The shape must be a tuple like (4, 2).
Fill all three blanks to create a tensor from a list, then create zeros and ones tensors with the same shape.
import torch base_tensor = torch.[1]([5, 6, 7]) zeros_tensor = torch.[2](base_tensor.[3]) ones_tensor = torch.ones(base_tensor.shape) print(base_tensor) print(zeros_tensor) print(ones_tensor)
First, torch.tensor creates a tensor from a list. Then torch.zeros creates a zeros tensor with the same shape accessed by shape.