在Ubuntu上安装PyTorch与CUDA时,确保它们的版本兼容是非常重要的。以下是一些关键步骤和建议,帮助你实现PyTorch与CUDA的兼容:
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu121
import torch
print(torch.__version__)
if torch.cuda.is_available():
print("CUDA is available. You can use GPU.")
else:
print("CUDA is not available. Using CPU.")
通过以上步骤,你可以确保在Ubuntu上安装的PyTorch与CUDA兼容,从而充分利用GPU加速进行深度学习任务。如果遇到具体问题,可以参考PyTorch官方文档或寻求社区帮助。