Complete the code to create a tensor from a list.
import torch my_tensor = torch.[1]([1, 2, 3, 4]) print(my_tensor)
The torch.tensor function creates a tensor from a Python list.
Complete the code to get the shape of a tensor.
import torch x = torch.tensor([[1, 2], [3, 4], [5, 6]]) shape = x.[1] print(shape)
The shape attribute returns the dimensions of the tensor as a tuple.
Fix the error in the code to perform element-wise addition of two tensors.
import torch a = torch.tensor([1, 2, 3]) b = torch.tensor([4, 5, 6]) result = a [1] b print(result)
The + operator performs element-wise addition between tensors.
Fill both blanks to create a tensor of zeros with shape (3, 4) and type float.
import torch zeros_tensor = torch.[1]((3, 4), dtype=torch.[2]) print(zeros_tensor)
torch.zeros creates a tensor filled with zeros. The dtype=torch.float32 sets the data type to float.
Fill all three blanks to create a tensor from a list, move it to GPU if available, and print its device.
import torch x = torch.[1]([1, 2, 3]) if torch.cuda.is_available(): x = x.[2]('cuda') print(x.[3])
torch.tensor creates the tensor. to('cuda') moves it to GPU. device shows where the tensor is stored.
