TensorFlow-GPUをCUDAで使えるようにするときのメモ
1.1 導入
普段グラボと言われると我々の多くはゲームを連想するだろう。
しかし今回はそれを一般的な用途(General Purpose)で使うのである。
(まぁGPGPUのGPってこれのことだし)
1.2 今回この記事の内容を行った環境
CPU:Ryzen5 1400
GPU:GTX1060
RAM:16GB
OS:Ubuntu16.04
2.1 手順1:CUDAを入れる
とりあえず動かないのは承知で入れてみる.
ターミナルにて
pip3 install tensorflow-gpu
その後
python3
を起動し
>> import tensorflow as tf
と入力。 すると
ImportError: libcublas.so.10.0: cannot open shared object file:
という結果またはそれに類するものが帰ってくる。
このlibcublas.so.〜
のあとに続く数字の部分(今回はlibcublas.so.10.0
なので10.0)
が今回インストールするのに必要なCUDAのバージョンである。
次にここ
から必要なバージョンのCUDAをダウンロードする。
基本的にTensorFlowが出たばかりの最新版のCUDAを使用することは少ないため
右側にあるLegacy Releasesの部分から該当のバージョンを探しだしてダウンロードする。
CUDA Toolkit 10.0 Archive を選択し
Operating System > Linux
Architecture > x86_64
Distribution > Ubuntu
Version > 16.04
Installer Type > deb[local]
をそれぞれ選択してダウンロード あとは
Installation Instructions: | |
に従えばOK 多分うまく行く
2.2 手順2:CuDnnを入れる
まだこのままじゃ動かない
次に
https://developer.nvidia.com/rdp/cudnn-download
からcuDNNをダウンロードする。
これにはアカウントが必要なので各自作ってやってほしい。
CUDAのバージョンにあった部分をクリック(今回はDownload cuDNN v7.5.0 (Feb 21, 2019), for CUDA 10.0)して
cuDNN Runtime Library for Ubuntu16.04 (Deb)
cuDNN Developer Library for Ubuntu16.04 (Deb)
cuDNN Code Samples and User Guide for Ubuntu16.04 (Deb)
の3つをダウンロードしてインストールする。
インストールする順番はRuntime→Developer→CodeSamplesでないとエラーが出る。
2.3 手順3:仕上げ
homeディレクトリの隠しファイルに.bashrc と.profile という設定ファイルがある。
これらを開いて中身の最後に
export PATH="/usr/local/cuda/bin:$PATH"
export LD_LIBRARY_PATH="/usr/local/cuda/lib64:$LD_LIBRARY_PATH"
をそれぞれ書き込む。
3.1 結果
ターミナルで
python3
と入力、その後
>>import tensorflow
でエラーが出なければ
>>tensorflow.Session()
と入力、すると
2019-04-04 00:58:16.697057: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-04-04 00:58:16.856579: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:998] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero
2019-04-04 00:58:16.857726: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x56fbb20 executing computations on platform CUDA. Devices:
2019-04-04 00:58:16.857749: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0): GeForce GTX 1060 6GB, Compute Capability 6.1
2019-04-04 00:58:16.892815: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 3193620000 Hz
2019-04-04 00:58:16.893704: I tensorflow/compiler/xla/service/service.cc:150] XLA service 0x5765840 executing computations on platform Host. Devices:
2019-04-04 00:58:16.893781: I tensorflow/compiler/xla/service/service.cc:158] StreamExecutor device (0):
という出力が得られる。
もし変なエラーや抜けてる部分や要望があったらツイッター(@TDU_ML_AI)でリプなりDMなりで指摘していただければありがたい。