Ubuntu上で、TensorFlowのGPU版をpipでインストールする手順をお伝えする。
TensorFlowのバージョン0.6.0からPython3が使えるようになったので、Python3を用いることにする。

ここに記載されていることの大部分は、TensorFlowのDownload and Setupに記載されている。

環境

OS Ubuntu 14.04.3 LTS(64bit)
Python 3系
CPU Intel(R) Core(TM) i7-6700T CPU @ 2.80GHz
Memory DDR4 16G (8G+8G)
GPU NVIDIA Corporation GM107 [GeForce GTX 750 Ti]

準備

TensorFlowでGPUを使うためには、cuDNN v2 Libraryをインストールしなければならないが、cuDNN v2 Libraryをダウンロードするためには、事前にnVIDIA DEVELOPERのサイト上でアカウントを作成しなければならない。
アカウント作成後、cuDNN v2 Libraryがダウンロードできるようになるまで、およそ1〜2営業日かかるので、アカウント作成は先に済ませておいたほうが良い。

CUDA 7.0のダウンロードとインストール

CUDA 7.0 DownloadsからUbuntu 14.04のNetwork Installer版をダウンロードし、次のコマンドでインストールを行う。


$ sudo dpkg -i cuda-repo-ubuntu1404_7.0-28_amd64.deb
$ sudo apt-get update
$ sudo apt-get install cuda-7-0 

cuDNN v2 Libraryのダウンロードとライブラリの追加

cuDNN ArchiveからcuDNN v2 Library for Linuxをダウンロードし、次のコマンドでライブラリの解凍および移動を行う。


$ tar xvzf cudnn-6.5-linux-x64-v2.tgz 
$ sudo cp cudnn-6.5-linux-x64-v2/cudnn.h /usr/local/cuda/include
$ sudo cp cudnn-6.5-linux-x64-v2/libcudnn* /usr/local/cuda/lib64

環境変数の設定

Ubuntuの起動時に自動的に環境変数を読み込ませるため、~/.profileに環境変数を追記する。


$ vim ~/.profile

最下部に次を追記する。


export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:/usr/local/cuda/lib64"
export CUDA_HOME=/usr/local/cuda

即時反映させるため、次のコマンドを実行する。


$ source ~/.profile

環境変数が追加されているかを確認するため、次のコマンドを実行する。


$ export -p

TensorFlowのインストール

TensorFlowをpipでインストールする。Python3系を使うため、Python3用のpipをインストールしておく。


$ sudo apt-get install python3-pip python3-dev

TensorFlow GPU版をインスールするため、次のコマンドを実行する。


$ sudo pip3 install --upgrade https://storage.googleapis.com/tensorflow/linux/gpu/tensorflow-0.6.0-cp34-none-linux_x86_64.whl

TensorFlowは/usr/local/lib/python3.4/dist-packages以下に配置される。

TensorFlowのテスト

TensorFlowがきちんと動くかどうか、テストする。
ここでは、ターミナル上でPython3を起動し、対話型で入力していく。
下のコードで「>>>」の右側が入力する文字となる。
対話を終了するには、「exit()」を入力すれば良い。
下のコードを簡単説明すると、TensorFlow上で10+32を計算している。


$ python3 
Python 3.4.3 (default, Oct 14 2015, 20:28:29) 
[GCC 4.8.4] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcublas.so.7.0 locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcudnn.so.6.5 locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcufft.so.7.0 locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcuda.so locally
I tensorflow/stream_executor/dso_loader.cc:101] successfully opened CUDA library libcurand.so.7.0 locally
>>> hello = tf.constant('Hello, TensorFlow!')
>>> sess = tf.Session()
I tensorflow/core/common_runtime/local_device.cc:40] Local device intra op parallelism threads: 8
I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:909] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
I tensorflow/core/common_runtime/gpu/gpu_init.cc:103] Found device 0 with properties: 
name: GeForce GTX 750 Ti
major: 5 minor: 0 memoryClockRate (GHz) 1.0845
pciBusID 0000:01:00.0
Total memory: 2.00GiB
Free memory: 1.56GiB
I tensorflow/core/common_runtime/gpu/gpu_init.cc:127] DMA: 0 
I tensorflow/core/common_runtime/gpu/gpu_init.cc:137] 0:   Y 
I tensorflow/core/common_runtime/gpu/gpu_device.cc:702] Creating TensorFlow device (/gpu:0) -> (device: 0, name: GeForce GTX 750 Ti, pci bus id: 0000:01:00.0)
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:42] Allocating 1.37GiB bytes.
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:52] GPU 0 memory begins at 0x701a40000 extends to 0x759128000
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 1.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 2.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 4.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 8.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 16.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 32.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 64.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 128.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 256.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 512.0KiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 1.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 2.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 4.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 8.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 16.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 32.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 64.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 128.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 256.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 512.00MiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 1.00GiB
I tensorflow/core/common_runtime/gpu/gpu_bfc_allocator.cc:66] Creating bin of max chunk size 2.00GiB
I tensorflow/core/common_runtime/direct_session.cc:58] Direct session inter op parallelism threads: 8
>>> print(sess.run(hello))
b'Hello, TensorFlow!'
>>> a = tf.constant(10)
>>> b = tf.constant(32)
>>> print(sess.run(a + b))
42
>>> exit()

エラー

もし、「import tensorflow as tf」の後に、次のようなエラーが表示されたら、環境変数が設定されていない可能性が高いので、今一度、環境変数を見直すことをおすすめする。


ImportError: libcudart.so.7.0: cannot open shared object file: No such file or directory
Ubuntu TensorFlowのGPU版をpipでインスールする手順