Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to import the main PyTorch library.
PyTorch
import [1]
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Importing TensorFlow instead of PyTorch.
Importing numpy which is for arrays but not PyTorch.
✗ Incorrect
The main PyTorch library is imported using import torch.
2fill in blank
mediumComplete the code to create a tensor filled with zeros of size 3x3.
PyTorch
x = torch.[1]((3, 3))
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.ones which creates a tensor of ones.
Using torch.rand which creates random values.
✗ Incorrect
Use torch.zeros to create a tensor filled with zeros.
3fill in blank
hardFix the error in the code to move a tensor to GPU if available.
PyTorch
device = torch.device('cuda' if torch.cuda.[1]() else 'cpu')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using incorrect method names like 'available' or 'has_cuda'.
Using camelCase instead of snake_case.
✗ Incorrect
The correct method to check if CUDA is available is torch.cuda.is_available().
4fill in blank
hardFill both blanks to define a simple neural network layer using PyTorch.
PyTorch
layer = torch.nn.[1](in_features=10, [2]=5)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using Conv2d which requires different parameters.
Confusing in_features with out_features.
✗ Incorrect
Use torch.nn.Linear for a fully connected layer. The parameter out_features defines the output size.
5fill in blank
hardFill both blanks to create a dictionary comprehension that maps layer names to their output sizes if size is greater than 5.
PyTorch
layer_sizes = {name: size for name, size in layers.items() if size [1] [2] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using < instead of > causing wrong filtering.
Using == which checks equality, not greater than.
✗ Incorrect
The comprehension filters layers with size greater than 5 using size > 5.