Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to check if CUDA is available in PyTorch.
PyTorch
import torch cuda_available = torch.cuda.[1]() print(f"CUDA available: {cuda_available}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.cuda.available() which does not exist.
Trying to access a property instead of calling a function.
✗ Incorrect
The correct PyTorch function to check CUDA availability is torch.cuda.is_available().
2fill in blank
mediumComplete the code to get the name of the current CUDA device.
PyTorch
import torch if torch.cuda.is_available(): device_name = torch.cuda.get_device_[1](torch.cuda.current_device()) print(f"Current CUDA device: {device_name}")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.cuda.get_device_id() which does not exist.
Using torch.cuda.get_device_info() which is incorrect.
✗ Incorrect
The function torch.cuda.get_device_name(device_id) returns the name of the CUDA device.
3fill in blank
hardFix the error in the code to correctly check CUDA availability and print a message.
PyTorch
import torch if torch.cuda.[1]: print("CUDA is available") else: print("CUDA is not available")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting parentheses after is_available.
Using a non-existent attribute like available.
✗ Incorrect
torch.cuda.is_available() is a function and must be called with parentheses to get a boolean result.
4fill in blank
hardFill both blanks to create a tensor on the CUDA device if available, otherwise on CPU.
PyTorch
import torch device = torch.device("cuda" if torch.cuda.[1]() else "[2]") tensor = torch.tensor([1, 2, 3], device=device) print(tensor.device)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'cuda' instead of 'cpu' as fallback device.
Using a non-existent function like is_ready.
✗ Incorrect
Use torch.cuda.is_available() to check CUDA, and 'cpu' as fallback device string.
5fill in blank
hardFill all three blanks to print the number of CUDA devices and the name of the first device if available.
PyTorch
import torch if torch.cuda.[1](): count = torch.cuda.[2]() name = torch.cuda.get_device_[3](0) print(f"CUDA devices: {count}, First device: {name}") else: print("No CUDA devices found.")
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using torch.cuda.count() which does not exist.
Using torch.cuda.get_device_id() instead of get_device_name().
✗ Incorrect
Use torch.cuda.is_available() to check availability, torch.cuda.device_count() to get number of devices, and torch.cuda.get_device_name() to get device name.