TensorFlow 自 2.11 版起,不再支援原生 Windows 利用 GPU 加速訓練。若是你想要在 Windows 上利用GPU 加速 TensorFlow 2.11 以上的版本,有以下種方式可以參考:
在 WSL 中安裝 CUDA Toolkit 與 cuDNN
TensorFlow Runtime Docker Images
DirectML Plugin for TensorFlow 2
本篇文章將介紹直接於 WSL 中安裝 TensorFlow 與 CUDA Toolkit 與 cuDNN 的方式。若你對 DirectML 有興趣,或是不想使用 WSL,可以參考 Microsoft 的文章來安裝 DirectML Plugin for TensorFlow 2。
WSL
Windows Subsystem for Linux,自 Windows 10 版本 2004 起內建於 Windows 中,若是你的版本過舊,請參考舊版 WSL 的手動安裝步驟,其他關於安裝 WSL 的說明可以參考安裝 WSL。現在開啟你最順手的終端機(cmd、powershell 等),輸入以下指令確認你的 wsl 是否可用:
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
請務必將此段程式放在 import tensorflow 前。
在 2.14、2.15 版本中,你會在 import tensorflow 時跳出錯誤訊息:
E external/local_xla/xla/stream_executor/cuda/cuda_dnn.cc:9261] Unable to register cuDNN factory: Attempting to register factory for plugin cuDNN when one has already been registered E external/local_xla/xla/stream_executor/cuda/cuda_fft.cc:607] Unable to register cuFFT factory: Attempting to register factory for plugin cuFFT when one has already been registered E external/local_xla/xla/stream_executor/cuda/cuda_blas.cc:1515] Unable to register cuBLAS factory: Attempting to register factory for plugin cuBLAS when one has already been registered
看到右上角的 CUDA Version,我的顯示 12.3,因此稍後我們所需要選擇版本時,都要圍繞著 12.3 (你的版本) 來挑選。確認好 CUDA 版本後,到 CUDA Toolkit Archive尋找你的 CUDA 版本。以我的情況,就是 12.3.0。進入版本的頁面後,依照 Linux > x86_64 > WSL-Ubuntu > 2.0 的路徑,選擇喜好選擇安裝的方式,這裡幫你們把 deb (local) 的按出來:
Selecting previously unselected package cudnn-local-repo-ubuntu2204-8.9.5.30.
(Reading database ... 39518 files and directories currently installed.)
Preparing to unpack cudnn-local-repo-ubuntu2204-8.9.5.30_1.0-1_amd64.deb ...
Unpacking cudnn-local-repo-ubuntu2204-8.9.5.30 (1.0-1) ...
Setting up cudnn-local-repo-ubuntu2204-8.9.5.30 (1.0-1) ...
The public cudnn-local-repo-ubuntu2204-8.9.5.30 GPG key does not appear to be installed.
To install the key, run this command:
sudo cp /var/cudnn-local-repo-ubuntu2204-8.9.5.30/cudnn-local-FB167084-keyring.gpg /usr/share/keyrings/