TensorFlow 2.10.0无法识别GPU的问题排查与解决求助
TensorFlow 2.10.0无法识别RTX 3060Ti GPU(Windows 10)
环境与安装步骤
通过conda创建环境并安装TensorFlow:
conda create -n foo python=3.10 conda activate foo conda install mamba mamba install tensorflow -c conda-forge mamba install cudnn cudatoolkit
安装完成后TensorFlow版本为2.10.0,本地已预装CUDA 11.2和cuDNN 8.1。
问题现象
运行以下代码时,GPU列表为空:
import tensorflow as tf print(f"GPUs available: {tf.config.list_physical_devices('GPU')}")
测试代码及日志显示当前使用纯CPU版本TensorFlow:
测试代码:
import tensorflow as tf import numpy as np def make_nn(): model = tf.keras.models.Sequential() model.add(tf.keras.layers.Dense(1, input_shape=(1,))) model.compile(loss='mean_squared_error', optimizer='sgd') return model def dataset(): x = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) y = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9]) return tf.data.Dataset.from_tensor_slices((x, y)).batch(1) def main(): model = make_nn() model.fit(dataset(), epochs=1, steps_per_epoch=9) if __name__ == '__main__': print(f"GPUs available: {tf.config.list_physical_devices('GPU')}") print(f"Built with cuda: {tf.test.is_built_with_cuda()}") main()
输出日志:
GPUs available: [] Built with cuda: False 2023-02-06 09:47:32.744450: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2023-02-06 09:47:32.779280: I tensorflow/core/common_runtime/process_util.cc:146] Creating new thread pool with default inter op setting: 2. Tune using inter_op_parallelism_threads for best performance.
系统与硬件信息
- 系统:Windows 10
- 显卡:RTX 3060Ti
nvidia-smi输出:
+-----------------------------------------------------------------------------+ | NVIDIA-SMI 528.24 Driver Version: 528.24 CUDA Version: 12.0 | |-------------------------------+----------------------+----------------------+ | GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC | | Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. | | | | MIG M. | |===============================+======================+======================| | 0 NVIDIA GeForce ... WDDM | 00000000:09:00.0 On | N/A | | 30% 43C P8 16W / 200W | 809MiB / 8192MiB | 3% Default | | | | N/A | +-------------------------------+----------------------+----------------------+ +-----------------------------------------------------------------------------+ | Processes: | | GPU GI CI PID Type Process name GPU Memory | | ID ID Usage | |=============================================================================| | 0 N/A N/A 7176 C+G ...perience\NVIDIA Share.exe N/A | | 0 N/A N/A 9240 C+G C:\Windows\explorer.exe N/A | | 0 N/A N/A 12936 C+G ...cw5n1h2txyewy\LockApp.exe N/A | | 0 N/A N/A 13652 C+G ...e\PhoneExperienceHost.exe N/A | | 0 N/A N/A 14020 C+G ...2txyewy\TextInputHost.exe N/A | | 0 N/A N/A 14888 C+G ...ser\Application\brave.exe N/A | | 0 N/A N/A 15112 C+G ...5n1h2txyewy\SearchApp.exe N/A | | 0 N/A N/A 16516 C+G ...oft OneDrive\OneDrive.exe N/A | | 0 N/A N/A 18296 C+G ...aming\Spotify\Spotify.exe N/A | | 0 N/A N/A 18624 C+G ...in7x64\steamwebhelper.exe N/A | | 0 N/A N/A 18672 C+G ...\app-1.0.9010\Discord.exe N/A | | 0 N/A N/A 18828 C+G ...lPanel\SystemSettings.exe N/A | | 0 N/A N/A 19284 C+G ...Central\Razer Central.exe N/A | | 0 N/A N/A 20020 C+G ...arp.BrowserSubprocess.exe N/A | | 0 N/A N/A 22912 C+G ...8wekyb3d8bbwe\Cortana.exe N/A | | 0 N/A N/A 24848 C+G ...ontend\Docker Desktop.exe N/A | | 0 N/A N/A 25804 C+G ...y\ShellExperienceHost.exe N/A | | 0 N/A N/A 27064 C+G ...8bbwe\WindowsTerminal.exe N/A | +-----------------------------------------------------------------------------+
nvcc -V输出:
Copyright (c) 2005-2021 NVIDIA Corporation Built on Sun_Feb_14_22:08:44_Pacific_Standard_Time_2021 Cuda compilation tools, release 11.2, V11.2.152 Build cuda_11.2.r11.2/compiler.29618528_0
解决方案
1. 匹配TensorFlow与CUDA版本
TensorFlow 2.10.0官方要求CUDA 11.2、cuDNN 8.1.0,先检查conda环境内的CUDA组件版本:
conda list cudatoolkit
若版本不符,重新安装指定版本:
mamba install cudatoolkit=11.2 cudnn=8.1 -c conda-forge
2. 安装GPU版TensorFlow
当前安装的是纯CPU版,卸载后重新安装GPU专属包:
mamba remove tensorflow mamba install tensorflow-gpu=2.10.0 -c conda-forge
注:TensorFlow 2.10是conda-forge上最后一个单独提供
tensorflow-gpu包的版本,后续版本统一使用tensorflow包,但2.10版本需单独安装GPU版。
3. 配置环境变量(Windows)
确保conda环境内的CUDA库被优先识别,激活环境后执行:
set PATH=%CONDA_PREFIX%\Library\bin;%PATH%
也可手动添加系统环境变量:
- 新增
CUDA_PATH变量,值为你的conda环境路径\Library(例如C:\Users\你的用户名\.conda\envs\foo\Library) - 将
%CUDA_PATH%\bin添加到系统PATH最前端
4. 验证GPU支持
重新激活环境后,运行以下代码确认:
import tensorflow as tf print(tf.test.is_built_with_cuda()) print(tf.config.list_physical_devices('GPU')) print(tf.test.gpu_device_name())
若输出能识别到GPU,说明配置成功。
5. 检查驱动兼容性
当前驱动版本528.24支持CUDA 12.0,虽向下兼容CUDA 11.2,若仍有问题,可尝试降级到CUDA 11.2对应的驱动版本(如460.x系列)。
内容的提问来源于stack exchange,提问作者João Areias
相关产品推荐
相关产品推荐

