Kerasで機械学習を試しているが、どうもGPUを使っていないっぽい。
自分のPCはCPUは微妙(Intel 10400F)だが、GPUは結構強め(Nvidia Geforce RTX 4070)。
せっかくなのでGPUでやりたい。
【Keras/TensorFlow】GPUを使うまでの手順と注意点などを解説します
とりあえず上の記事を見つけて、書いてある通りCUDAやCuDNNを入れてみる。
tensorflowをアンインストールしてtensorflow-gpuをインストールし直してみたが、最新版はインストールできないよ、的な表示が出る。
で、以下のURLに誘導される。
tensorflow-gpu · PyPI
tensorflow-gpu has been removed. Please install tensorflow instead. The tensorflow package supports GPU accelerated operations via Nvidia CUDA.
どうも書いてあることを読むと、「tensorflow 2.1以降は同じパッケージになってるからtensorflowにしろ。もうtensorflow-gpuはリリースやめてる」的なことを言われる。
で、もう一回tensorflowをpipで入れなおして、とりあえずGPUを認識しているか確認してみたが、やっぱり認識してないね。
from tensorflow.python.client import device_lib device_lib.list_local_devices()
で確認できるらしいが、CPU使ってるやん、という表示。一部引用すると以下のような感じ。
name: "/device:CPU:0" device_type: "CPU"
とりあえずググったら以下のページがヒット。
GPU サポート | TensorFlow
改めて自分のCUDAバージョンを確認。
nvcc -V
nvcc: NVIDIA (R) Cuda compiler driver Copyright (c) 2005-2022 NVIDIA Corporation Built on Wed_Sep_21_10:41:10_Pacific_Daylight_Time_2022 Cuda compilation tools, release 11.8, V11.8.89 Build cuda_11.8.r11.8/compiler.31833905_0
で、v11.8であることがわかる。
コマンドプロンプトを開き、バージョンに合わせてコードを一部変更したものを入れてみる。
SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\bin;%PATH% SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\extras\CUPTI\lib64;%PATH% SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.8\include;%PATH% SET PATH=C:\Program Files\NVIDIA\CUDNN\bin;%PATH%
そのあとコマンドラインからPythonの対話モードに入り、デバイス確認コードを打つも変わらず。
>>> from tensorflow.python.client import device_lib >>> device_lib.list_local_devices() 2024-01-21 09:53:57.302180: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: SSE SSE2 SSE3 SSE4.1 SSE4.2 AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. [name: "/device:CPU:0" device_type: "CPU" memory_limit: 268435456
日本語版の記述が古い可能性もあるため、英語版のドキュメントも確認してみる。
GPU device plugins | TensorFlow
Note: This page is for non-NVIDIA® GPU devices. For NVIDIA® GPU support, go to the Install TensorFlow with pip guide.
とのこと。
誘導先は以下。
Install TensorFlow with pip
Windows Nativeのタブを見るとこんな記述がある。
Caution: TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows. Starting with TensorFlow 2.11, you will need to install TensorFlow in WSL2, or install tensorflow or tensorflow-cpu and, optionally, try the TensorFlow-DirectML-Plugin
改めて、自分の環境のtensoflowのバージョンを確認する。
pip list |grep tensorflow
tensorflow 2.15.0 tensorflow-estimator 2.15.0 tensorflow-intel 2.15.0 tensorflow-io-gcs-filesystem 0.31.0
うーん。2.15。
2.10以前に戻さないとGPUサポートしてないってことなので、入っているものをアンインストールして2.10以前を入れなおす。
pip uninstall tensorflow
pip install "tensorflow<2.11"
するとこんなエラー。
ERROR: Could not find a version that satisfies the requirement tensorflow<2.11 (from versions: 2.12.0rc0, 2.12.0rc1, 2.12.0, 2.12.1, 2.13.0rc0, 2.13.0rc1, 2.13.0rc2, 2.13.0, 2.13.1, 2.14.0rc0, 2.14.0rc1, 2.14.0, 2.14.1, 2.15.0rc0, 2.15.0rc1, 2.15.0)
ERROR: No matching distribution found for tensorflow<2.11
ついでにtensorflow-gpuでも試してみたが
pip install tensorflow-gpu==
pipでインストールできるのはv2.12.0だけらしい。(つまり使えない)
ERROR: Could not find a version that satisfies the requirement tensorflow-gpu== (from versions: 2.12.0)
ERROR: No matching distribution found for tensorflow-gpu==
諦めてWindows WSL2で試すことにします。
Windows NativeのPythonとWSL2で並列させて何かよろしくないことがあるのか?はよくわからないけど。まあいいや。