Complete the code to create a tensor of shape (3, 4).
import torch x = torch.zeros([1]) print(x.shape)
The shape (3, 4) creates a tensor with 3 rows and 4 columns.
Complete the code to get the number of dimensions of tensor x.
import torch x = torch.randn(2, 5, 7) dimensions = x.[1]() print(dimensions)
shape which returns the size tuple, not the number of dimensions.ndim which is not a PyTorch method.The dim() method returns the number of dimensions of a tensor.
Fix the error in the code to reshape tensor x to shape (6, 10).
import torch x = torch.randn(3, 20) x = x.[1]((6, 10)) print(x.shape)
reshape() which can sometimes cause errors if tensor is not contiguous.resize() which changes the tensor size in-place and can cause unexpected behavior.The view() method reshapes a tensor without copying data, but requires the tensor to be contiguous.
Fill both blanks to create a tensor of shape (2, 3) and then flatten it to 1D.
import torch x = torch.randn([1]) x_flat = x.[2]() print(x_flat.shape)
view() without specifying the new shape.First, create a tensor with shape (2, 3). Then use flatten() to convert it to 1D.
Fill all three blanks to create a tensor, get its shape, and access the size of the second dimension.
import torch x = torch.randn([1]) shape = x.[2] second_dim = shape[[3]] print(second_dim)
size() method instead of shape attribute.Create a tensor with shape (4, 5, 6). Use shape to get its size tuple. Access index 1 for the second dimension.