一、安装tensorflow-gpu环境
Tensorflow-gpu 官网:https://www.tensorflow.org/install/pip
1、激活conda环境
参考《Ubuntu 18.04 LTS操作系统的Conda安装及配置》
2、pip3安装tensorflow-gpu
pip3 install --ignore-installed --upgrade tensorflow-gpu=1.13.1
3、查看 GPU支持
Tensorflow-gpu官网查看 : https://www.tensorflow.org/install/gpu
软件要求:
必须在系统中安装以下 NVIDIA® 软件:
NVIDIA® GPU 驱动程序 - CUDA 10.0 需要 410.x 或更高版本。
CUDA® 工具包 - TensorFlow 支持 CUDA 10.0(TensorFlow 1.13.0 及更高版本)
CUDA 工具包附带的 CUPTI。
cuDNN SDK(7.4.1 及更高版本)
(可选)TensorRT 5.0,可缩短在某些模型上进行推断的延迟并提高吞吐量。
根据要求主要安装CUDA 10.0和cuDNN 7.4.1
二、安装CUDA 10.0 + CUDNN 7.4.1环境
1、下载并安装CUDA 10.0
1)进入下载页面
下载地址:https://developer.nvidia.com/cuda-toolkit-archive
选择【CUDA Toolkit Archive】--> 【Archived Releases】-->
CUDA Toolkit 10.0(Sept 2018)
2)选择软件
按如下选择
下载地址: https://developer.download.nvidia.cn/compute/cuda/10.0/secure/Prod/local_installers/cuda_10.0.130_410.48_linux.run?7sW17FsbLAfFJTdNtkEFKRyv-qY-yJSUHjZeRHsf2kF8vfYm6KtYibzmIybSBpvfuJTicLX4pDlaroKw0Gdi0I7RmGhTsRi8v7GFBEMBK-L9_PcOy0-bdXlLzY6bbUXQJeN0Jq4i5h18KM7vQyuaq5D8xlVIvvUwhWZYO4Iv54OcPi142jAcg15iWJI
下载文件名:uda_10.0.130_410.48_linux.run
3) 安装
在终端中执行下如下命令安装:
sudo sh cuda_10.0.130_410.48_linux.run
将软件安装在/usr/local/cuda-10.0目录
2、下载并安装 cuDNN 7.4.1
(需要nvidia官网developer.nvidia.com注册账号才能下载)
1 ) 进入下载页面下载
下载地址:https://developer.nvidia.com/rdp/cudnn-archive
下载【cuDNN Library for Linux】
下载地址:
https://developer.download.nvidia.cn/compute/machine-learning/cudnn/secure/v7.4.1.5/prod/10.0_20181108/cudnn-10.0-linux-x64-v7.4.1.5.tgz?Drb-LVroFtbylrk-vNGt4UagLplL-KWK5lBlMQjVWp2m0hcMCWgwyYFVTXm3ITvLxV1E-nY0GouhTstoOQSuwjZTII3np5TY2FTNdDO32LyWPuz2CclKfyz-_v_OWVSR-pC8UzYMSD8lUt_3T-1TIPFJwMOXHj17WM9uYPqSfpGtkurnVGlQI0Ok5UZtstQDUJhnSrLWudjkgnAQRAckTUaL
下载文件名:
cudnn-10.0-linux-x64-v7.4.1.5
2) 安装
解压文件
tar xvfz cudnn-10.0-linux-x64-v7.4.1.5
拷贝文件到/usr/local
cp -rfv cuda /usr/local
3) 修改环境变量
修改/etc/profile 或者 ~/.bashrc ,添加如下环境变量
export PATH=/usr/local/cuda-10.0/bin${PATH:+:${PATH}}
export LD_LIBRARY_PATH=/usr/local/cuda-10.0/lib64${LD_LIBRARY_PATH:+:${LD_LIBRARY_PATH}}
export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda
4)更新系统环境
source /etc/profile
sudo ldconfig
3、验证
1)【nvidia-smi】
Tue Aug 13 11:25:30 2019
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 410.48 Driver Version: 410.48 |
|-------------------------------+----------------------+----------------------+
| GPU Name Persistence-M| Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
|===============================+======================+======================|
| 0 GeForce GTX 108... Off | 00000000:03:00.0 Off | N/A |
| 0% 29C P8 13W / 250W | 16MiB / 11177MiB | 0% Default |
+-------------------------------+----------------------+----------------------+
+-----------------------------------------------------------------------------+
| Processes: GPU Memory |
| GPU PID Type Process name Usage |
|=============================================================================|
| 0 1485 G /usr/lib/xorg/Xorg 13MiB |
+-----------------------------------------------------------------------------+
2)【nvcc -V 】
nvcc: NVIDIA (R) Cuda compiler driver
Copyright (c) 2005-2018 NVIDIA Corporation
Built on Sat_Aug_25_21:08:01_CDT_2018
Cuda compilation tools, release 10.0, V10.0.13
三、Tensorflow 验证
1、查看是否使用GPU
(在python开发环境下执行如下程序)
>>>import tensorflow as tf
>>>tf.test.gpu_device_name()
….
Found device 0 with properties:
name: GeForce GTX 1080 Ti major: 6 minor: 1 memoryClockRate(GHz): 1.683
pciBusID: 0000:03:00.0
totalMemory: 10.92GiB freeMemory: 10.75GiB
…..
'/device:GPU:0'
….
2.查看在使用哪个GPU
>>>from tensorflow.python.client import device_lib
>>>device_lib.list_local_devices()
…
incarnation: 16567897232232910199
physical_device_desc: "device: XLA_CPU device"
, name: "/device:GPU:0"
device_type: "GPU"
memory_limit: 10961603789
locality {
bus_id: 1
links {
}
}
…
3、Tensorflow应用程序
>>>import tensorflow as tf
>>>hello = tf.constant('Hello, TensorFlow!')
>>>sess = tf.Session()
>>>print(sess.run(hello))