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

Moving Models Between Devices: CPU and GPU

Use .to(device) and .cuda() to move entire models between CPU and GPU, including all parameters and buffers.

Moving Models Between Devices: CPU and GPU

What you'll learn: How to relocate your entire neural network—including all its parameters and internal state—from CPU to GPU (or vice versa) for faster training and inference.

Why Move Models Between Devices?

Think of your model as a chef and the device as a kitchen. Sometimes you need to move the chef (and all their tools) from a small home kitchen (CPU) to an industrial kitchen (GPU) to cook faster. In deep learning, GPUs dramatically accelerate tensor operations, but you need to explicitly tell PyTorch where your model should "live."

The Core Methods

PyTorch provides two main ways to move models:

.to(device) — The flexible, modern approach. You specify a device object:

import torch

device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = MyNetwork()
model.to(device)

This moves all parameters and buffers (like BatchNorm running statistics) to the specified device.

.cuda() — A shortcut for GPU only:

model.cuda()  # Moves to default GPU (cuda:0)
model.cuda(1)  # Moves to GPU 1

To return to CPU:

model.cpu()

Critical Detail: Input Data Must Match

Once your model is on GPU, your input tensors must also be on the same device:

model.to(device)
inputs = inputs.to(device)
outputs = model(inputs)  # Now they're compatible

Mismatched devices cause runtime errors—it's like trying to hand ingredients from one kitchen to a chef in another!

Key Takeaway: Use .to(device) to move your entire model and its learned parameters to CPU or GPU; always ensure input data lives on the same device as your model.