A GPU tensor is a tensor stored in the GPU's memory, allowing faster computation by using the GPU's parallel processing power.
Click to reveal answer
beginner
How do you move a tensor to the GPU using the to() method?
You call tensor.to('cuda') to move the tensor to the GPU if available.
Click to reveal answer
beginner
What does tensor.cuda() do in PyTorch?
It moves the tensor to the default GPU device, similar to tensor.to('cuda').
Click to reveal answer
intermediate
Why should you check if a GPU is available before moving tensors to it?
Because if no GPU is available, trying to move tensors to GPU will cause errors. Use torch.cuda.is_available() to check.
Click to reveal answer
intermediate
What is the difference between tensor.to('cuda') and tensor.cuda()?
tensor.to('cuda') can specify any device and is more flexible. tensor.cuda() moves tensor to the default GPU device. Both move tensors to GPU memory.
Click to reveal answer
Which PyTorch method moves a tensor to the GPU?
Atensor.numpy()
Btensor.to('cpu')
Ctensor.reshape()
Dtensor.to('cuda')
✗ Incorrect
The to('cuda') method moves a tensor to the GPU device.
What does torch.cuda.is_available() check?
AIf GPU is available
BIf CPU is available
CIf tensor is on GPU
DIf CUDA is installed
✗ Incorrect
It checks if a GPU device is available for PyTorch to use.
What happens if you try to move a tensor to GPU when no GPU is available?
ATensor moves to GPU anyway
BTensor stays on CPU
CError occurs
DTensor is deleted
✗ Incorrect
Trying to move a tensor to GPU without a GPU causes an error.
Which method is more flexible for moving tensors between devices?
Atensor.cuda()
Btensor.to()
Ctensor.cpu()
Dtensor.numpy()
✗ Incorrect
tensor.to() can move tensors to any device, not just GPU.
What device does tensor.cuda() move the tensor to?
ADefault GPU device
BCPU
CRandom device
DDisk storage
✗ Incorrect
tensor.cuda() moves the tensor to the default GPU device.
Explain how to move a tensor to GPU in PyTorch and why it is useful.
Think about device names and checking if GPU exists.
You got /3 concepts.
Describe the difference between tensor.to('cuda') and tensor.cuda().
Consider device flexibility and default behavior.
You got /3 concepts.
Practice
(1/5)
1. What does the PyTorch method tensor.to('cuda') do?
easy
A. Moves the tensor to the GPU for faster computation
B. Converts the tensor to a CPU tensor
C. Changes the tensor data type to float
D. Deletes the tensor from memory
Solution
Step 1: Understand the purpose of to('cuda')
This method moves the tensor from CPU memory to GPU memory to speed up calculations.
Step 2: Recognize the effect on computation
By moving the tensor to GPU, PyTorch can use GPU cores for faster processing.
Final Answer:
Moves the tensor to the GPU for faster computation -> Option A
Quick Check:
to('cuda') = move tensor to GPU [OK]
Hint: to('cuda') means move tensor to GPU memory [OK]
Common Mistakes:
Thinking it converts data type
Assuming it moves tensor to CPU
Believing it deletes the tensor
2. Which of the following is the correct syntax to move a tensor named x to GPU in PyTorch?
easy
A. x.to('cpu')
B. x.gpu()
C. x.to('cuda')
D. x.cuda('cpu')
Solution
Step 1: Identify correct method to move tensor to GPU
The method to('cuda') moves tensor to GPU.
Step 2: Check each option
x.to('cuda') uses to('cuda'), which is correct. Others either move to CPU or use invalid methods.
Final Answer:
x.to('cuda') -> Option C
Quick Check:
Correct syntax for GPU move = x.to('cuda') [OK]
Hint: Use .to('cuda') to move tensor to GPU [OK]
Common Mistakes:
Using .to('cpu') instead of 'cuda'
Calling .cuda('cpu') which is invalid
Using non-existent .gpu() method
3. What will be the output device of the tensor after running this code?
import torch
x = torch.tensor([1, 2, 3])
if torch.cuda.is_available():
x = x.cuda()
print(x.device)
medium
A. cuda:0
B. cuda:1
C. cpu
D. None
Solution
Step 1: Check if GPU is available
The code checks torch.cuda.is_available(). If True, it moves tensor to GPU device 0 by default.
Step 2: Understand x.device output
After x.cuda(), the tensor is on GPU device 0, so x.device prints cuda:0.
Final Answer:
cuda:0 -> Option A
Quick Check:
cuda() moves tensor to cuda:0 device [OK]
Hint: cuda() moves tensor to GPU device 0 by default [OK]
Common Mistakes:
Assuming device is 'cpu' after cuda()
Thinking cuda() moves to cuda:1 by default
Expecting None as device
4. Identify the error in this code snippet that tries to move a tensor to GPU:
import torch
x = torch.tensor([1, 2, 3])
x = x.to('gpu')
print(x.device)
medium
A. Tensor cannot be moved to GPU
B. Tensor must be converted to float before moving
C. Missing .cuda() call after .to()
D. The device string 'gpu' is invalid; should be 'cuda'
Solution
Step 1: Check device string in to() method
The string 'gpu' is not recognized by PyTorch; the correct string is 'cuda'.
Step 2: Understand correct usage
Using 'cuda' moves tensor to GPU. Using 'gpu' causes a runtime error.
Final Answer:
The device string 'gpu' is invalid; should be 'cuda' -> Option D
Quick Check:
Use 'cuda' not 'gpu' in to() [OK]
Hint: Use 'cuda' not 'gpu' as device string [OK]
Common Mistakes:
Using 'gpu' instead of 'cuda'
Thinking tensor must be float to move
Missing .cuda() call after .to()
5. You want to write code that safely moves a tensor x to GPU if available, otherwise keeps it on CPU. Which code snippet correctly does this?
hard
A. x = x.to('cuda') if torch.cuda.is_available() else x.to('gpu')
B. if torch.cuda.is_available():
x = x.to('cuda')
else:
x = x.to('cpu')
C. x = x.cuda() if torch.cuda.is_available() else x.cuda()
D. x = x.to('cpu') if torch.cuda.is_available() else x.to('cuda')
Solution
Step 1: Understand device check logic
We must check if GPU is available using torch.cuda.is_available().
Step 2: Apply correct device move
If GPU is available, move tensor to 'cuda', else keep on 'cpu'. if torch.cuda.is_available():
x = x.to('cuda')
else:
x = x.to('cpu') does this correctly.
Final Answer:
if torch.cuda.is_available():
x = x.to('cuda')
else:
x = x.to('cpu') -> Option B
Quick Check:
Check GPU availability before moving tensor [OK]
Hint: Check cuda availability before moving tensor [OK]
Common Mistakes:
Using invalid device string 'gpu'
Moving to cuda even if unavailable
Swapping cpu and cuda in else
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.