0
0
PyTorchml~10 mins

First PyTorch computation - Interactive Code Practice

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

Complete the code to create a tensor with values [1, 2, 3].

PyTorch
import torch
x = torch.[1]([1, 2, 3])
print(x)
Drag options to blanks, or click blank then click option'
Atensor
Barray
Cmatrix
Dlist
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'array' instead of 'tensor' causes an error because PyTorch does not have torch.array.
Using 'list' or 'matrix' are not valid PyTorch functions.
2fill in blank
medium

Complete the code to add two tensors x and y.

PyTorch
import torch
x = torch.tensor([1, 2])
y = torch.tensor([3, 4])
z = x [1] y
print(z)
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.
Using '/' divides instead of adds.
3fill in blank
hard

Fix the error in the code to multiply tensor x by 2.

PyTorch
import torch
x = torch.tensor([1, 2, 3])
y = x [1] 2
print(y)
Drag options to blanks, or click blank then click option'
A+
Badd
Cmultiply
D*
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'add' or 'multiply' as operators causes syntax errors.
Using '+' adds instead of multiplies.
4fill in blank
hard

Fill both blanks to create a tensor of zeros with shape (2, 3) and print its shape.

PyTorch
import torch
zeros_tensor = torch.[1]((2, 3))
print(zeros_tensor.[2])
Drag options to blanks, or click blank then click option'
Azeros
Bsize
Cshape
Dempty
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'empty' creates uninitialized values, not zeros.
Using 'size' instead of 'shape' is incorrect in PyTorch.
5fill in blank
hard

Fill all three blanks to create a tensor from a list, convert it to float type, and print its data type.

PyTorch
import torch
x = torch.tensor([1, 2, 3]).[1]([2])
print(x.[3])
Drag options to blanks, or click blank then click option'
Ato
Btorch.float32
Cdtype
Dfloat
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'float' instead of 'torch.float32' causes errors.
Using 'float' as a method or attribute is incorrect.
Using 'dtype' as a method instead of attribute causes errors.