Ubuntu支持广泛的PyTorch操作,具体包括以下方面:
使用Conda安装(推荐):
conda install pytorch torchvision torchaudio pytorch-cuda11.8 -c pytorch -c nvidia
将11.8
替换为你安装的CUDA版本号。
使用pip安装:
仅支持CPU:
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
支持GPU:
pip3 install torch torchvision torchaudio cudatoolkit=11.8 -f https://download.pytorch.org/whl/cu118
将11.8
替换为你安装的CUDA版本号。
安装完成后,可以通过以下Python代码验证PyTorch是否成功安装,并检查CUDA是否可用:
import torch
print(f"PyTorch版本: {torch.__version__}")
print(f"CUDA可用: {torch.cuda.is_available()}")
print(f"当前设备: {torch.device('cuda' if torch.cuda.is_available() else 'cpu')}")
以上信息提供了在Ubuntu上安装和验证PyTorch操作的基本指南,包括版本选择、安装方法以及安装后的验证步骤。