在Ubuntu系统中选择PyTorch网络库时,需重点关注CUDA版本兼容性、硬件支持和安装方式,具体步骤如下:
确认CUDA版本
运行命令 nvcc --version 查看已安装的CUDA版本(若未安装CUDA,需先安装对应版本,如CUDA 11.7/12.1等)。
选择匹配的PyTorch版本
安装方式
# GPU版本(以CUDA 12.1为例)
conda install pytorch torchvision torchaudio pytorch-cuda=12.1 -c pytorch -c nvidia
# CPU版本
conda install pytorch torchvision torchaudio cpuonly -c pytorch
# GPU版本(CUDA 11.3为例)
pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113
# CPU版本
pip3 install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cpu
验证安装
运行以下代码检查是否成功及是否支持GPU:
import torch
print("PyTorch版本:", torch.__version__)
print("CUDA可用性:", torch.cuda.is_available())
注意事项:
conda create -n pytorch_env),避免依赖冲突。参考来源: