How to use PyTorch with CUDA?

Published on Aug. 22, 2023, 12:18 p.m.

To use PyTorch with CUDA, you need to have an NVIDIA GPU and install the corresponding CUDA toolkit for your GPU. Once you have CUDA installed, you can install a version of PyTorch that supports CUDA by using the appropriate pip install command for your CUDA version. Here is an example of how to install PyTorch with CUDA 11.0:

pip install torch torchvision torchaudio cudatoolkit=11.0 -c pytorch

In your PyTorch code, you can specify whether to use CPU or GPU by calling cuda() or cpu() on your tensors, or by specifying the device when constructing your model. Here is an example of how to use a PyTorch model on the GPU:

import torch

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

model = MyModel().to(device)

input_tensor = torch.randn(10, 20).to(device)

output = model(input_tensor)

The code above checks if CUDA is available and if it is, moves the model and input tensor to the GPU using .to(device). You can also use torch.cuda.device() to specify which GPU to use if you have multiple GPUs available.

Tags: