官方教程:https://www.tensorflow.org/install/gpu?hl=zh-cn
硬件与软件要求:
我们需要做的大致是以下几步:
1.安装 NVIDIA GPU 驱动程序 https://www.nvidia.com/Download/index.aspx?lang=en-us
按自己的需求选择并下载即可
CUDA 和 cuDNN 安装教程 https://blog.csdn.net/qq_37296487/article/details/83028394
2. 安装 Tensorflow 对应版本的 CUDA (如这里支持的是10.0版本,装10.1可能会出错)
https://developer.nvidia.com/cuda-toolkit-archive
3. 安装符合要求的cuDNN(如这里要求7.4.1 及更高版本;官网需注册/登录账户)
https://developer.nvidia.com/rdp/cudnn-download
4. 安装 tensorflow-gpu(若安装慢,可以复制控制台中的网址,先下载到本地,再进行安装,类似pytorch安装)
pip install tensorflow-gpu
验证是否成功:
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))输出为:
可能会遇到的问题: 报错:NVIDIA-SMI has failed because it couldn't communicate with the NVIDIA driver
可能原因:NVIDIA驱动程序未安装 或 要更新 或者 Ubuntu系统自动更新所导致
网上搜到的解决方法多是卸载NVIDIA驱动程序并重装
可以用 nvidia-smi 命令,是否能查看到显卡信息,若不行则说明 多是 NVIDIA驱动程序 的问题,再装一遍,并尝试nvidia-smi查看即可 (Windows用户使用nvidia-smi方法)
另一篇帖子 https://github.com/tensorflow/tensorflow/issues/2882