Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to move the tensor to GPU using the recommended method.
PyTorch
import torch x = torch.tensor([1, 2, 3]) x_gpu = x.[1]('cuda')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .cuda() directly instead of .to('cuda')
Using .cpu() which moves tensor to CPU
✗ Incorrect
The to method is the recommended way to move tensors to a device like GPU by specifying 'cuda'.
2fill in blank
mediumComplete the code to check if CUDA is available before moving the tensor to GPU.
PyTorch
import torch x = torch.tensor([4, 5, 6]) if torch.cuda.is_available(): x_gpu = x.[1]('cuda')
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using .cuda() without checking availability
Using .copy() which is not a tensor method
✗ Incorrect
Using to('cuda') moves the tensor to GPU if CUDA is available.
3fill in blank
hardFix the error in the code to move the tensor to GPU correctly.
PyTorch
import torch x = torch.tensor([7, 8, 9]) x_gpu = x.[1]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using to without arguments
Using to('cuda') with parentheses inside blank
✗ Incorrect
The cuda() method moves the tensor to the default CUDA device without arguments.
4fill in blank
hardFill both blanks to create a tensor on GPU and then move it back to CPU.
PyTorch
import torch x_gpu = torch.tensor([10, 11, 12], device=[1]) x_cpu = x_gpu.[2]()
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using device='cpu' to create GPU tensor
Using to('cpu') instead of cpu() method
✗ Incorrect
Specify device='cuda' to create tensor on GPU, then use cpu() to move it back to CPU.
5fill in blank
hardFill all three blanks to create a tensor on CPU, then move it to GPU.
PyTorch
import torch x = torch.tensor([13, 14, 15], device=[1]) x_gpu = x.[2]([3])
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using device='cuda' initially
Using cuda() method with argument
Using to without argument
✗ Incorrect
Create tensor on CPU with device='cpu', move to GPU with to('cuda').