There are different methods. That’s how I do it:
First, check if the GPU is available, otherwise fall back to CPU (I only have 1 GPU so I can fix it to “cuda:0”)
device = torch.device("cuda:0" if use_cuda else "cpu")

For the network model:
model = MyCoolModel(...)
model.to(device)

For the data
test_tensor = torch.ones(32)
test_tensor.to(device)

You only have to make sure that all your data and the model are moved to the same device. I common error is that the model is on the GPU but the current batch (still) on the CPU. Having basically the .to(device) at your model and all the tensors should take care of that.