ubuntu安裝 tensorflow GPU

安裝支持GPU的tensorflow前提是正確安裝好了 CUDA 和 cuDNN。
html

CUDA 和 cuDNN的安裝見 Nvidia 官網和各類安裝教程,應該很容易,重點是要選準了支持本身GPU的 CUDA 版本,再選準支持 該 CUDA 版本的 cuDNN版本。html5


關於CUDA:
node

tensorflow-gpu 1.5 及以上版本要求 CUDA 版本爲9.0;python

若是本機裝的 CUDA版本是8,安裝了 tensorflow-gpu 1.5及以上版本,會報錯:
linux

ImportError: libcublas.so.9.0: cannot open shared object file: No such file or directory


查看本機 CUDA 版本方法markdown

cat /usr/local/cuda/version.txt

輸出:session

CUDA Version 8.0.61


關於cuDNN:
app

tensorflow-gpu 1.3及以上版本要求cudnn版本爲V6及以上;python2.7

若是本機裝得 cuDNN版本是 V5,安裝了 tensorflow-gpu 1.3及以上版本,會報錯:post

libcudnn.so.6:cannot open sharedobject file: No such file or directory


查看本機 cuDNN版本方法

cat /usr/local/cuda/include/cudnn.h | grep CUDNN_MAJOR -A 2

輸出:

#define CUDNN_MAJOR      5
#define CUDNN_MINOR      0
#define CUDNN_PATCHLEVEL 5
--
#define CUDNN_VERSION    (CUDNN_MAJOR * 1000 + CUDNN_MINOR * 100 + CUDNN_PATCHLEVEL)

#include "driver_types.h"

本機裝了 CUDA 8和 cuDNN V5,對照以上兩條,選擇安裝 tensorflow 1.2版本,安裝指令:

pip install tensorflow-gpu==1.2

輸出貼出來:

Collecting tensorflow-gpu==1.2
  Downloading tensorflow_gpu-1.2.0-cp27-cp27mu-manylinux1_x86_64.whl (89.2MB)
    100% |████████████████████████████████| 89.2MB 15kB/s 
Collecting backports.weakref==1.0rc1 (from tensorflow-gpu==1.2)
  Downloading backports.weakref-1.0rc1-py2-none-any.whl
Requirement already satisfied: wheel in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Requirement already satisfied: bleach==1.5.0 in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Requirement already satisfied: numpy>=1.11.0 in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Collecting markdown==2.2.0 (from tensorflow-gpu==1.2)
  Downloading Markdown-2.2.0.tar.gz (236kB)
    100% |████████████████████████████████| 245kB 42kB/s 
Requirement already satisfied: mock>=2.0.0 in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Requirement already satisfied: html5lib==0.9999999 in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Requirement already satisfied: werkzeug>=0.11.10 in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Requirement already satisfied: six>=1.10.0 in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Requirement already satisfied: protobuf>=3.2.0 in ./anaconda2/lib/python2.7/site-packages (from tensorflow-gpu==1.2)
Requirement already satisfied: funcsigs>=1; python_version < "3.3" in ./anaconda2/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow-gpu==1.2)
Requirement already satisfied: pbr>=0.11 in ./anaconda2/lib/python2.7/site-packages (from mock>=2.0.0->tensorflow-gpu==1.2)
Requirement already satisfied: setuptools in ./anaconda2/lib/python2.7/site-packages (from protobuf>=3.2.0->tensorflow-gpu==1.2)
Building wheels for collected packages: markdown
  Running setup.py bdist_wheel for markdown ... done
  Stored in directory: /home/dcrmg/.cache/pip/wheels/b9/4f/6c/f4c1c5207c1d0eeaaf7005f7f736620c6ded6617c9d9b94096
Successfully built markdown
Installing collected packages: backports.weakref, markdown, tensorflow-gpu
  Found existing installation: backports.weakref 1.0.post1
    Uninstalling backports.weakref-1.0.post1:
      Successfully uninstalled backports.weakref-1.0.post1
  Found existing installation: Markdown 2.6.11
    Uninstalling Markdown-2.6.11:
      Successfully uninstalled Markdown-2.6.11
Successfully installed backports.weakref-1.0rc1 markdown-2.2.0 tensorflow-gpu-1.2.0


要安裝哪一個版本的tensorflow-gpu,使用 ‘tensorflow-gpu==xx’就好了,安裝 1.4版本:

pip install tensorflow-gpu==1.4

要卸載也很容易:

pip uninstall tensorflow-gpu


測試tensorflow是否可使用GPU

import tensorflow as tf
sess = tf.Session(config=tf.ConfigProto(log_device_placement=True))

輸出:

2018-03-19 07:29:43.114843: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations.
2018-03-19 07:29:43.114898: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.
2018-03-19 07:29:43.114917: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2018-03-19 07:29:43.114940: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
2018-03-19 07:29:43.114960: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.
2018-03-19 07:29:43.388602: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:893] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2018-03-19 07:29:43.389607: I tensorflow/core/common_runtime/gpu/gpu_device.cc:940] Found device 0 with properties: name: GeForce GTX 970 major: 5 minor: 2 memoryClockRate (GHz) 1.1775 pciBusID 0000:01:00.0 Total memory: 3.94GiB
Free memory: 3.71GiB
2018-03-19 07:29:43.389661: I tensorflow/core/common_runtime/gpu/gpu_device.cc:961] DMA: 0 
2018-03-19 07:29:43.389682: I tensorflow/core/common_runtime/gpu/gpu_device.cc:971] 0:   Y 
2018-03-19 07:29:43.389722: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1030] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 970, pci bus id: 0000:01:00.0)
Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 970, pci bus id: 0000:01:00.0
2018-03-19 07:29:43.455817: I tensorflow/core/common_runtime/direct_session.cc:265] Device mapping:
/job:localhost/replica:0/task:0/gpu:0 -> device: 0, name: GeForce GTX 970, pci bus id: 0000:01:00.0

輸出裏有 GPU的名稱,顯存等信息,表示tensorflow可使用GPU了。

相關文章
相關標籤/搜索