Using a GPU makes tensor calculations much faster. Moving tensors to the GPU lets your program use this speed.
GPU tensors (to, cuda) in PyTorch
tensor.to(device)
tensor.cuda(device=None)to(device) moves a tensor to the specified device like 'cpu' or 'cuda'.
cuda() moves a tensor to the GPU. You can specify which GPU if you have more than one.
t to the default GPU.import torch t = torch.tensor([1, 2, 3]) t_gpu = t.to('cuda')
t to the default GPU using cuda().import torch t = torch.tensor([1, 2, 3]) t_gpu = t.cuda()
import torch t = torch.tensor([1, 2, 3]) t_gpu = t.to('cuda:0')
This program checks if a GPU is available. It creates a tensor on the CPU, moves it to the GPU if possible, and then squares the tensor on that device.
import torch # Check if GPU is available if torch.cuda.is_available(): device = torch.device('cuda') else: device = torch.device('cpu') # Create a tensor on CPU t = torch.tensor([1.0, 2.0, 3.0]) print(f'Original tensor device: {t.device}') # Move tensor to GPU if available t_gpu = t.to(device) print(f'Tensor device after moving: {t_gpu.device}') # Perform a simple operation on GPU t_gpu_squared = t_gpu ** 2 print(f'Tensor squared on device: {t_gpu_squared}')
Always check if a GPU is available using torch.cuda.is_available() before moving tensors.
Moving tensors between CPU and GPU takes time, so do it only when needed.
Operations on tensors happen on the device where the tensor is located.
Moving tensors to GPU speeds up calculations.
Use to('cuda') or cuda() to move tensors to GPU.
Check GPU availability before moving tensors.
Practice
tensor.to('cuda') do?Solution
Step 1: Understand the purpose of
This method moves the tensor from CPU memory to GPU memory to speed up calculations.to('cuda')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 AQuick Check:
to('cuda') = move tensor to GPU [OK]
- Thinking it converts data type
- Assuming it moves tensor to CPU
- Believing it deletes the tensor
x to GPU in PyTorch?Solution
Step 1: Identify correct method to move tensor to GPU
The methodto('cuda')moves tensor to GPU.Step 2: Check each option
x.to('cuda') usesto('cuda'), which is correct. Others either move to CPU or use invalid methods.Final Answer:
x.to('cuda') -> Option CQuick Check:
Correct syntax for GPU move = x.to('cuda') [OK]
- Using .to('cpu') instead of 'cuda'
- Calling .cuda('cpu') which is invalid
- Using non-existent .gpu() method
import torch
x = torch.tensor([1, 2, 3])
if torch.cuda.is_available():
x = x.cuda()
print(x.device)Solution
Step 1: Check if GPU is available
The code checkstorch.cuda.is_available(). If True, it moves tensor to GPU device 0 by default.Step 2: Understand
Afterx.deviceoutputx.cuda(), the tensor is on GPU device 0, sox.deviceprintscuda:0.Final Answer:
cuda:0 -> Option AQuick Check:
cuda() moves tensor to cuda:0 device [OK]
- Assuming device is 'cpu' after cuda()
- Thinking cuda() moves to cuda:1 by default
- Expecting None as device
import torch
x = torch.tensor([1, 2, 3])
x = x.to('gpu')
print(x.device)Solution
Step 1: Check device string in
The string 'gpu' is not recognized by PyTorch; the correct string is 'cuda'.to()methodStep 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 DQuick Check:
Use 'cuda' not 'gpu' in to() [OK]
- Using 'gpu' instead of 'cuda'
- Thinking tensor must be float to move
- Missing .cuda() call after .to()
x to GPU if available, otherwise keeps it on CPU. Which code snippet correctly does this?Solution
Step 1: Understand device check logic
We must check if GPU is available usingtorch.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 BQuick Check:
Check GPU availability before moving tensor [OK]
- Using invalid device string 'gpu'
- Moving to cuda even if unavailable
- Swapping cpu and cuda in else
Ready to prove it?
Pass the test and this pattern moves unknown
