0
0
PyTorchml~10 mins

Why tensors are PyTorch's core data structure - Test Your Understanding

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a tensor from a list.

PyTorch
import torch

my_tensor = torch.[1]([1, 2, 3, 4])
print(my_tensor)
Drag options to blanks, or click blank then click option'
Alist
Barray
Cmatrix
Dtensor
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'tensor' causes an error.
Trying to use 'list' is invalid in PyTorch.
2fill in blank
medium

Complete the code to get the shape of a tensor.

PyTorch
import torch

x = torch.tensor([[1, 2], [3, 4], [5, 6]])
shape = x.[1]
print(shape)
Drag options to blanks, or click blank then click option'
Alength
Bshape
Csize
Ddim
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'size' as a method requires parentheses.
Using 'length' is not valid for tensors.
3fill in blank
hard

Fix the error in the code to perform element-wise addition of two tensors.

PyTorch
import torch

a = torch.tensor([1, 2, 3])
b = torch.tensor([4, 5, 6])
result = a [1] b
print(result)
Drag options to blanks, or click blank then click option'
A-
B*
C+
D/
Attempts:
3 left
💡 Hint
Common Mistakes
Using '-' subtracts instead of adds.
Using '*' multiplies instead of adds.
4fill in blank
hard

Fill both blanks to create a tensor of zeros with shape (3, 4) and type float.

PyTorch
import torch

zeros_tensor = torch.[1]((3, 4), dtype=torch.[2])
print(zeros_tensor)
Drag options to blanks, or click blank then click option'
Azeros
Bones
Cint32
Dfloat32
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'ones' creates a tensor of ones.
Using 'int32' sets integer type instead of float.
5fill in blank
hard

Fill all three blanks to create a tensor from a list, move it to GPU if available, and print its device.

PyTorch
import torch

x = torch.[1]([1, 2, 3])
if torch.cuda.is_available():
    x = x.[2]('cuda')
print(x.[3])
Drag options to blanks, or click blank then click option'
Atensor
Bto
Cdevice
Dcuda
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cuda' as a method instead of a string argument.
Trying to print 'to' instead of 'device'.