要更详细的教程可以参考Tensorflow + PyTorch 安装(CPU + GPU 版本),这里是有基础之后的快速安装。
一、Pytorch
- 安装
bash
conda create -n torch_env python=3.10.13
conda activate torch_env
conda install cudatoolkit==11.8 -c nvidia
pip install torch==2.1.1 torchvision==0.16.1 torchaudio==2.1.1 --index-url https://download.pytorch.org/whl/cu118
conda install -c "nvidia/label/cuda-11.8.0" cuda-nvcc
- 测试
python
# 进入python
python
import torch
print(torch.cuda.is_available())
二、Tensorflow
- 安装
安装完Pytorch,再安装Tensorflow
bash
conda create -n tensorflow_env python=3.7
conda activate tensorflow_env
conda install cudatoolkit==11.8 -c nvidia
pip install tensorflow-gpu==2.6.0
# 测试时会报错说LD_LIBRARY_PATH: :/usr/local/cuda/lib64下没有libcudnn.so.8
# 于是用locate在电脑中查找
# 显示其中一个路径是 your_path/Anaconda3/envs/torch_env/lib/python3.10/site-packages/torch/lib/libcudnn.so.8,复制到/usr/local/cuda/lib64
sudo updatedb
locate libcudnn.so.8
sudo cp your_path/Anaconda3/envs/torch_env/lib/python3.10/site-packages/torch/lib/libcudnn.so.8 /usr/local/cuda/lib64
- 测试
python
import tensorflow as tf
print(tf.test.is_gpu_available())
三、查看显卡利用率
bash
# 简单查看
nvidia-smi
# 每2秒刷新
nvidia-smi -l 1