Course contentsShow
← Machine Learning and Deep Learning
Lesson 844 of 3,53819. PyTorch FundamentalsFree lesson

Device Management Best Practices

Use device objects and write device-agnostic code that runs on both CPU and GPU without modification.

Device Management Best Practices

What you'll learn: How to write clean, device-agnostic PyTorch code that seamlessly runs on both CPU and GPU without manual changes.

The Problem with Hardcoded Devices

You've learned to move tensors and models to GPU with .to() and .cuda(), but hardcoding device references creates fragile code. If you write model.cuda() everywhere, your code breaks on machines without GPUs. If you write model.to('cpu'), you're wasting available GPU resources.

The Device Object Solution

PyTorch provides a device object that acts as a universal handle for your hardware. Think of it as a smart adapter that automatically connects to the best available resource.

# Create a device object that picks GPU if available, CPU otherwise
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Now use it consistently
model = MyModel().to(device)
data = data.to(device)

Best Practice Pattern

The gold standard approach:

  1. Define device once at the top of your script
  2. Use .to(device) for all tensors and models (avoid .cuda())
  3. Create new tensors on the correct device from the start
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# Models
model.to(device)

# Existing tensors
x = x.to(device)

# New tensors created directly on device
y = torch.zeros(10, device=device)

This pattern means your training loop works identically on laptops (CPU), workstations (GPU), or cloud instances—without a single line change.

Key Takeaway: Always use a device object defined once and referenced everywhere, and prefer .to(device) over .cuda() for truly portable, device-agnostic PyTorch code.