深度阅读

How to use PyTorch with CUDA?

作者
作者
2023年08月22日
更新时间
10.71 分钟
阅读时间
0
阅读量

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.

相关标签

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。