此文章默认显卡支持CUDA和已安装显卡驱动,貌似目前只有英伟达的显卡支持CUDA。
本人显卡为GTX1660,需要的CUDA和cuDNN已打包好(hyfi),其余请自行下载
绿色软件 GPU-Z
可直接检测显卡,下载链接(os41)
查看显卡支持的 CUDA 版本
- 先在官网查显卡的算力(Compute Capability),如 RTX 3060 为 8.6
- 再查该算力支持的 CUDA SDK,如 8.6 支持的 CUDA SDK 为 11.1 - 11.4(建议 RTX 3060 装 11.1)
一、显卡型号
桌面右键→NVIDIA控制面板→帮助→系统信息
显卡型号为GTX 1660
若桌面右键无NVIDIA控制面板,查看参考文献
二、CUDA
-
查找显卡对应的CUDA版本:接着上一步→组件
显卡GTX 1660
对应的CUDA版本为CUDA 10.1
,可以选低一个版本,如CUDA 10.0
-
安装,环境变量自动配上
-
命令行输入
nvcc -V
三、cuDNN
- 根据CUDA版本下载cuDNN(需登录),关键是CUDA版本,cuDNN版本选新的
- 下载得到压缩包,解压后将三个文件夹粘贴到CUDA
四、tensorflow-gpu
- 查看版本:右上角Language设为English,拉到最下面
CUDA 10.0+cuDNN 7.6.1,装tensorflow-gpu 1.14.0
- 安装
pip install tensorflow-gpu==1.14.0
- 测试代码
import tensorflow as tf
a = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[2, 3], name='a')
b = tf.constant([1.0, 2.0, 3.0, 4.0, 5.0, 6.0], shape=[3, 2], name='b')
c = tf.matmul(a, b)
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))
print(sess.run(c))
出现此结果证明使用的是GPU运行
a: (Const): /job:localhost/replica:0/task:0/device:GPU:0
b: (Const): /job:localhost/replica:0/task:0/device:GPU:0
[[22. 28.]
[49. 64.]]
若为tensorflow2,测试代码
import tensorflow as tf
try:
with tf.device('/device:GPU:0'):
v = tf.Variable(tf.zeros([10, 10]))
print(v)
except:
print('no gpu')
五、查看显卡利用率
- 配置环境变量Path:
C:\Program Files\NVIDIA Corporation\NVSMI\
- 查看显卡利用率
简单查看:nvidia-smi
每秒刷新:nvidia-smi -l 1
Linux系统
默认已安装 CUDA 和 cuDNN
1. 查看显卡型号
nvidia-smi
2. 查看CUDA版本
cat /usr/local/cuda/version.txt
3. 查看cuDNN版本
cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2
4. 选择对应版本安装
pip install tensorflow-gpu==2.3.0
Linux下Anaconda
在 Linux 下基本没有 root 权限,通过 conda
命令安装不同版本的 CUDA 和 cuDNN
1. 查看显卡驱动版本
cat /proc/driver/nvidia/version
根据 CUDA和最低要求驱动 最高可安装 CUDA 10.1
2. 创建虚拟环境
conda create -n deep2 python=3.6
3. 激活虚拟环境
conda activate deep2
4. 安装CUDA
conda install cudatoolkit=10.1
5. 安装cuDNN
conda install -c conda-forge cudnn
6. 查看CUDA版本
conda list cudatoolkit
7. 查看cuDNN版本
conda list cudnn
8. 选择对应版本安装
pip install tensorflow-gpu==2.3.0
参考文献
- 如何检查显卡支持哪个版本的CUDA
- NVIDIA-SMI系列命令总结
- tensorflow各个版本的CUDA以及Cudnn版本对应关系
- WIN10 + GTX1660Ti配置TensorFlow GPU版本
- TensorFlow2.0教程-Variables
- Linux 和 Windows 查看 CUDA 和 cuDNN 版本
- Ubuntu安装cuda和cudnn及测试方法
- Cudnn :: Anaconda.org
- CUDA - Wikipedia
- CUDA GPUs - Compute Capability
- CUDA Compatibility :: NVIDIA Data Center GPU Driver Documentation