pytorch when do I need to use `.to(device)` on a model or tensor?

It is necessary to have both the model, and the data on the same device, either CPU or GPU, for the model to process data. Data on CPU and model on GPU, or vice-versa, will result in a Runtime error.

You can set a variable device to cuda if it’s available, else it will be set to cpu, and then transfer data and model to device :

import torch

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

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)