文章目录
1. 提前准备
显卡驱动:Ubuntu/Linux 安装GPU 驱动&检测(PyTorch准备)
CUDA:Ubuntu/Linux 安装CUDA、检测(PyTorch准备)
cuDNN:Ubuntu/Linux 安装cuDNN、检测(PyTorch准备)
2. 安装Anaconda 3-2023.09
Debain 安装提前下载
Anaconda版本:repo.anaconda.com/archive/
sudo apt update && sudo apt upgrade -y
sudo apt-get install libgl1-mesa-glx libegl1-mesa libxrandr2 libxrandr2 libxss1 libxcursor1 libxcomposite1 libasound2 libxi6 libxtst6
sudo apt install curl
curl -O https://repo.anaconda.com/archive/Anaconda3-2023.09-0-Linux-x86_64.sh
安装
find / -name "Anaconda3-2023.09-0-Linux-x86_64.sh"
bash **path**/Anaconda3-2023.09-0-Linux-x86_64.sh
输入“yes” 使得SHELL之后可以直接使用conda init
2.1. 检查Anaconda安装
conda info
conda -V
3. 建立环境&安装PyTorch
3.1. 虚拟环境
conda create -n base_pytorch python=3.7 # 创建base_pytorch python3.7 环境
conda activate base_pytorch # 激活环境
conda info --envs # 检查环境
3.2. PyTorch版本选择
链接: https://pytorch.org/get-started/locally/
最好使用低版本1.10
pip3 install torch==1.10.1+cu113 torchvision==0.11.2+cu113 torchaudio==0.10.1+cu113 -f https://download.pytorch.org/whl/cu113/torch_stable.html
遇到问题可以使用官网的代码: https://pytorch.org/get-started/previous-versions/
conda install pytorch==1.11.0 torchvision==0.12.0 torchaudio==0.11.0 -c pytorch
4. 全面检查 PyTorch、CUDA、cuDNN
4.1. 检查PyTorch
python
import torch
print(torch.__version__) # 查看pytorch版本1.11.0
4.2. 全面检查CUDA
python
import torch
torch.cuda.is_available() # cuda是否可用 True
print(torch.version.cuda) # 查看pytorch 对应的cuda版本11.3
torch.cuda.device_count() # 返回gpu数量
torch.cuda.get_device_name(0) # 返回gpu名字,设备索引默认从0开始
torch.cuda.current_device() # 返回当前设备索引
4.3. 检测cudnn可用性
python
import torch
print(torch.backends.cudnn.version()) # 8200
a = torch.tensor(1.)
a.cuda() # tensor(1., device='cuda:0')
from torch.backends import cudnn # 若正常则静默
cudnn.is_available() # 若正常返回True
cudnn.is_acceptable(a.cuda()) # 若正常返回True
5. cuDNN 版本问题解决
参考文章:Ubuntu/Linux 安装cuDNN、检测、版本修改(PyTorch准备)
RuntimeError: cuDNN version incompatibility: PyTorch was compiled against (8, 9, 2) but found runtime version (8, 7, 0). PyTorch already comes bundled with cuDNN. One option to resolving this error is to ensure PyTorch can find the bundled cuDNN.Looks like your LD_LIBRARY_PATH contains incompatible version of cudnnPlease either remove it from the path or install cudnn (8, 9, 2)
6. 安装Jupyter Notebook
conda install jupyter
conda activate base
conda update conda # 一定要在base先升级一下
conda activate base_pytorch
conda install jupyter
cd /home/×××/ #到想存放jupyter notebook的文件夹
jupyter notebook