0
0
PyTorchml~10 mins

GPU tensors (to, cuda) in PyTorch - Interactive Code Practice

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

Complete 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'
Acpu
Bcuda
Cdevice
Dto
Attempts:
3 left
💡 Hint
Common Mistakes
Using .cuda() directly instead of .to('cuda')
Using .cpu() which moves tensor to CPU
2fill in blank
medium

Complete 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'
Acuda
Bto
Cdevice
Dcopy
Attempts:
3 left
💡 Hint
Common Mistakes
Using .cuda() without checking availability
Using .copy() which is not a tensor method
3fill in blank
hard

Fix 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'
Ato
Bdevice
Ccuda
Dto('cuda')
Attempts:
3 left
💡 Hint
Common Mistakes
Using to without arguments
Using to('cuda') with parentheses inside blank
4fill in blank
hard

Fill 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'
A'cuda'
Bcpu
Dcuda
Attempts:
3 left
💡 Hint
Common Mistakes
Using device='cpu' to create GPU tensor
Using to('cpu') instead of cpu() method
5fill in blank
hard

Fill 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'
A'cpu'
Bto
C'cuda'
Dcuda
Attempts:
3 left
💡 Hint
Common Mistakes
Using device='cuda' initially
Using cuda() method with argument
Using to without argument