TensorFlow の環境構築

Uncategorized
1.4k words

公式サイト の手順通りにやったものです。

公式サイトより詳しくないのでご了承ください。自分の覚書みたいなものです。

環境

  • Windows 10 Home 21H2 19044.1526
  • Python 3.9.6
  • pip 21.1.3
  • TensorFlow 2.8.0
  • CUDA 11.2
  • cuDNN SDK 8.1.0

準備

Microsoft Visual C++ 再頒布可能パッケージ

このパッケージに含まれる msvcp140_1.dll が必要のためインストールする。

うちのPCは 64ビットOS なので、アーキテクチャX64 の実行ファイルをダウンロードしてインストールした。

Microsoft Visual C++ 再頒布可能パッケージ ダウンロード

Microsoft Visual C++ 2015-2022 Redistributable (x64) - 14.31.31103

Microsoft Visual C++ 2015-2022 Redistributable (x64) - 14.31.31103

ロングパスの有効化

Windows だと 256文字以上のフォルダ名+ファイル名の場合エラーとなるため、ロングパスを有効にさせます。

スタートメニューに「regedit」と入力し、レジストリエディッターを起動させます。

レジストリエディッター起動

レジストリエディター で「HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\FileSystem」に移動します。

「FileSystem」フォルダ内に「LongPathsEnabled」が無い場合は追加します。

右クリックメニューから「新規 -> DWORD (32ビット) 値」を選択し、新しく追加します。

DWORD (32ビット) 値

  • キー: LongPathsEnabled
  • 値: 1

LongPathsEnabled

設定後

LongPathsEnabled

TensorFlow のインストール

次のコマンドで TensorFlow がインストールできます。

1
pip install --upgrade tensorflow

TensorFlowインストール

動作確認

次のコマンドで TensorFlow のインストールに成功したか確認することができます。

1
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

TensorFlow実行

GPUを使うためのDLLが無いのでエラーになってますが、動いてはいそうです。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
C:\WINDOWS\system32>python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2022-03-06 16:59:24.092377: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-03-06 16:59:24.092624: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
2022-03-06 16:59:27.917585: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-03-06 16:59:27.918185: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublas64_11.dll'; dlerror: cublas64_11.dll not found
2022-03-06 16:59:27.919028: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cublasLt64_11.dll'; dlerror: cublasLt64_11.dll not found
2022-03-06 16:59:27.919546: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cufft64_10.dll'; dlerror: cufft64_10.dll not found
2022-03-06 16:59:27.920028: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'curand64_10.dll'; dlerror: curand64_10.dll not found
2022-03-06 16:59:27.920502: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusolver64_11.dll'; dlerror: cusolver64_11.dll not found
2022-03-06 16:59:27.920977: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cusparse64_11.dll'; dlerror: cusparse64_11.dll not found
2022-03-06 16:59:27.921440: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudnn64_8.dll'; dlerror: cudnn64_8.dll not found
2022-03-06 16:59:27.921521: W tensorflow/core/common_runtime/gpu/gpu_device.cc:1850] Cannot dlopen some GPU libraries. Please make sure the missing libraries mentioned above are installed properly if you would like to use GPU. Follow the guide at https://www.tensorflow.org/install/gpu for how to download and setup the required libraries for your platform.
Skipping registering GPU devices...
2022-03-06 16:59:27.922366: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
tf.Tensor(93.55823, shape=(), dtype=float32)

GPU サポート

CPUだけでも機械学習できますが、GPUを使えるようにします。

GPU サポートの設定方法も 公式サイト を参考にしました。

NVIDIA GPU ドライバー v450.80.02 以降

使ってるグラボ用の GPUドライバー をダウンロードしてインストールします。

NVIDIA GPU ドライバー

v450.80.02 以降でないと正常に動作しないらしいです。

今回は「NVIDIAグラフィックスドライバー」にチェックを入れてインストールしました。

NVIDIAグラフィックスドライバー

CUDA 11.2

TensorFlow 2.8.0 は CUDA 11.2 に対応しています。

CUDA Toolkit Archive よりダウンロードできます。

CUDA 11.2

ダウンロードが出来たので実行したところ、インストールできませんでした。

You already have a newer version of the NVIDIA Frameview SDK installed.

You already have a newer version of the NVIDIA Frameview SDK installed.

Frameview というアプリが既にインストールされていたので、一度アンインストールして再度実行したところ、無事にインストールすることができました。

Frameview アンインストール

カスタムインストールを選択して、CUDA のみをインストールしました。

CUDA のみインストール

cuDNN SDK 8.1.0

cuDNN Archive から cuDNN SDK v8.1.0 をダウンロードします。

CUDA 11.2 対応版をダウンロードします。cuDNN v8.1.1 でも問題ありませんでした。

cuDNN SDK 8.1.0

x64 が無かったので「cuDNN Library for Windows (x86)」をダウンロードして、

cuDNN Library for Windows (x86)

Cドライブの直下に新規フォルダー「tools」を作って、そこに展開して置いておきます。

toolsに展開

環境変数

最後に今インストールしたディレクトリを、環境変数に登録します。

1
2
3
4
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\bin
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\extras\CUPTI\lib64
C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2\include
C:\tools\cuda\bin

環境変数設定

動作確認

動作確認をします。

1
python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

tensorflow実行

正常に GPU が認識されているので、問題なさそうです。

1
2
3
4
5
C:\WINDOWS\system32>python -c "import tensorflow as tf;print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
2022-03-06 19:34:12.714818: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1900] Ignoring visible gpu device (device: 0, name: NVIDIA GeForce GTX 760, pci bus id: 0000:01:00.0, compute capability: 3.0) with Cuda compute capability 3.0. The minimum required Cuda capability is 3.5.
2022-03-06 19:34:12.718506: I tensorflow/core/platform/cpu_feature_guard.cc:151] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
tf.Tensor(392.52243, shape=(), dtype=float32)

今回のオチ

よく読んでみると NVIDIA GeForce GTX 760 は計算能力が低いから使わないって書いてありますね。

せっかく設定したのに、無駄に終わりました。