前言:
(1)因项目需求,需要安装TensorFlow-GPU版本,故本文在此记录安装过程。
(2)有注释,优先看注释
(3)本文所使用的GPU为NVIDIA GeForce RTX 5080 Laptop GPU, 16303MiB
(4)本文安装的TensorFlow为2.10
1、搜索TensorFlow,并进入官网
官网链接:关于TensorFlow | TensorFlow中文官网
2、进入官网,切换语言,切换到English

3、install->pip依次点击,进入到官方安装教程

4、在3、所在页面往下滑,直到Step-by-step instructions
Step-by-step instructions以下为TensorFlow的详细安装步骤,按照它的步骤来
step1:创造一个conda环境
conda create --name tf python=3.9
step2:激活conda环境
conda deactivate
step3:在conda环境中安装CUDA、cuDNN
conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
*注:*CUDA、cuDNN的版本需自己确认,一定是要等于或高于才可以。否则无法按照它的教程安装TensorFlow-GPU版本
step4:升级pip 安装,确保运行的是最新版本
pip install --upgrade pip
step5:安装TensorFlow
pip install "tensorflow<2.11"
*(官方提醒) 注:*不要用conda安装TensorFlow。它可能没有最新的稳定版本 的版本。推荐使用pip,因为TensorFlow仅正式发布 PyPI。
step6:验证安装
(1)验证CPU设置:
python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"
运行效果如下:
(E:\miniconda\tf) PS C:\Users\MR_YANG> python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))" 2025-08-27 10:00:01.885219: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2027] TensorFlow was not built with CUDA kernel binaries compatible with compute capability 12.0. CUDA kernels will be jit-compiled from PTX, which could take 30 minutes or longer. 2025-08-27 10:00:01.891753: 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: AVX AVX2 To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags. 2025-08-27 10:00:01.895637: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2027] TensorFlow was not built with CUDA kernel binaries compatible with compute capability 12.0. CUDA kernels will be jit-compiled from PTX, which could take 30 minutes or longer.(E:\miniconda\tf) PS C:\Users\MR_YANG> python -c "import tensorflow as tf;打印(tf.reduce_sum (tf.random。正常([1000,1000]))" 2015-08-27 10:00:01.885219:W tensorflow/core/common_runtime/gpu/gpu_device。TensorFlow没有使用与compute capability 12.0兼容的CUDA内核二进制构建。CUDA内核将从PTX进行jit编译,这可能需要30分钟或更长时间。2015-08-27 10:00:01.891753: I tensorflow/core/platform/cpu_feature_guard。此TensorFlow二进制文件使用oneAPI深度神经网络库(oneDNN)进行优化,以在关键性能操作中使用以下CPU指令:AVX AVX2在其他操作中启用它们,使用适当的编译器标志重新构建TensorFlow。2016-08-27 10:00:01.895637: W tensorflow/core/common_runtime/gpu/gpu_device。TensorFlow没有使用与compute capability 12.0兼容的CUDA内核二进制构建。CUDA内核将从PTX进行jit编译,这可能需要30分钟或更长时间。
*注:*需等待它执行完成,因为如果你的cuda版本过高,TensorFlow在首次运行时会自动编译适配你的cuda版本,在这个过程中可能出现"卡顿时长较长",但编译完成后,后续任务会恢复正常速度,无需重复编译。
(2)验证GPU设置
python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"
运行效果如下:
(E:\miniconda\tf) PS C:\Users\MR_YANG> python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))" 2025-08-27 10:06:30.147288: W tensorflow/core/common_runtime/gpu/gpu_device.cc:2027] TensorFlow was not built with CUDA kernel binaries compatible with compute capability 12.0. CUDA kernels will be jit-compiled from PTX, which could take 30 minutes or longer. [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
注: 虽然仍显示 "未预编译适配 Compute Capability 12.0 的 CUDA 内核" 的警告,但这是首次运行后的 "历史提示残留" ------ 实际上你之前执行 tf.reduce_sum
时,JIT 编译已经完成(从后续输出的计算结果和 GPU 设备创建日志可确认)。下次执行 GPU 计算任务(如模型训练、矩阵运算)时,该警告会自动消失,因为 TensorFlow 会复用已编译好的内核,无需重复耗时编译。
自此,TensorFlow-GPU版本已经安装完成!
5、相关说明
Q1:为什么3、中需要切换到英文界面
因为在中文界面,它不会显示如下警告,导致你直接运行pip3 install --user --upgrade tensorflow之后,安装的是最新版本的TensorFlow,从而无法使用本地GPU进行训练。挺阴间的就是了!

Q2:如果自己已经安装其他CUDA、cuDNN版本,会受到影响嘛?
不会。在执行step3时,我其实也在担心这个问题,但执行之后,并没有影响到我原有的conda环境,所以请放心安装。
Q3:可以安装其他版本的TensorFlow嘛?
可以的,你可以根据自己任务需求下载相应的TensorFlow。

安装参考指令:
pip install https://storage.googleapis.com/tensorflow/windows/gpu/tensorflow_gpu-2.6.0-cp38-cp38-win_amd64.whl
6、相关报错
报错1:TensorFlow 与当前安装的 NumPy 2.0.2 版本不兼容
报错信息:
(E:\miniconda\tf) PS C:\Users\MR_YANG> python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))" A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.2 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'. If you are a user of the module, the easiest solution will be to downgrade to 'numpy<2' or try to upgrade the affected module. We expect that some modules will need time to support NumPy 2. Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\miniconda\tf\lib\site-packages\tensorflow\init.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "E:\miniconda\tf\lib\site-packages\tensorflow\python\init.py", line 37, in <module> from tensorflow.python.eager import context File "E:\miniconda\tf\lib\site-packages\tensorflow\python\eager\context.py", line 35, in <module> from tensorflow.python.client import pywrap_tf_session File "E:\miniconda\tf\lib\site-packages\tensorflow\python\client\pywrap_tf_session.py", line 19, in <module> from tensorflow.python.client._pywrap_tf_session import * AttributeError: _ARRAY_API not found A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.2 as it may crash. To support both 1.x and 2.x versions of NumPy, modules must be compiled with NumPy 2.0. Some module may need to rebuild instead e.g. with 'pybind11>=2.12'. If you are a user of the module, the easiest solution will be to downgrade to 'numpy<2' or try to upgrade the affected module. We expect that some modules will need time to support NumPy 2. Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\miniconda\tf\lib\site-packages\tensorflow\init.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "E:\miniconda\tf\lib\site-packages\tensorflow\python\init.py", line 42, in <module> from tensorflow.python import data File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\init.py", line 21, in <module> from tensorflow.python.data import experimental File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\init.py", line 96, in <module> from tensorflow.python.data.experimental import service File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\service\init.py", line 419, in <module> from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 24, in <module> from tensorflow.python.data.experimental.ops import compression_ops File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 16, in <module> from tensorflow.python.data.util import structure File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\util\structure.py", line 23, in <module> from tensorflow.python.data.util import nest File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\util\nest.py", line 36, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "E:\miniconda\tf\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 24, in <module> from tensorflow.python.framework import constant_op File "E:\miniconda\tf\lib\site-packages\tensorflow\python\framework\constant_op.py", line 25, in <module> from tensorflow.python.eager import execute File "E:\miniconda\tf\lib\site-packages\tensorflow\python\eager\execute.py", line 23, in <module> from tensorflow.python.framework import dtypes File "E:\miniconda\tf\lib\site-packages\tensorflow\python\framework\dtypes.py", line 29, in <module> from tensorflow.python.lib.core import _pywrap_bfloat16 AttributeError: _ARRAY_API not found ImportError: numpy.core._multiarray_umath failed to import ImportError: numpy.core.umath failed to import Traceback (most recent call last): File "<string>", line 1, in <module> File "E:\miniconda\tf\lib\site-packages\tensorflow\init.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "E:\miniconda\tf\lib\site-packages\tensorflow\python\init.py", line 42, in <module> from tensorflow.python import data File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\init.py", line 21, in <module> from tensorflow.python.data import experimental File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\init.py", line 96, in <module> from tensorflow.python.data.experimental import service File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\service\init.py", line 419, in <module> from tensorflow.python.data.experimental.ops.data_service_ops import distribute File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 24, in <module> from tensorflow.python.data.experimental.ops import compression_ops File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 16, in <module> from tensorflow.python.data.util import structure File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\util\structure.py", line 23, in <module> from tensorflow.python.data.util import nest File "E:\miniconda\tf\lib\site-packages\tensorflow\python\data\util\nest.py", line 36, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "E:\miniconda\tf\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 24, in <module> from tensorflow.python.framework import constant_op File "E:\miniconda\tf\lib\site-packages\tensorflow\python\framework\constant_op.py", line 25, in <module> from tensorflow.python.eager import execute File "E:\miniconda\tf\lib\site-packages\tensorflow\python\eager\execute.py", line 23, in <module> from tensorflow.python.framework import dtypes File "E:\miniconda\tf\lib\site-packages\tensorflow\python\framework\dtypes.py", line 34, in <module> _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type() TypeError: Unable to convert function return value to a Python type! The signature was () -> handle
报错原因:
早期版本的 TensorFlow(如安装的 <2.11 版本)是基于 NumPy 1.x 编译的,无法在 NumPy 2.x 环境中正常运行。
解决方案:降级 NumPy 到 1.x 版本
在TensorFlow所处的conda 环境中执行以下命令:
pip install "numpy<2.0"
这条命令会将 NumPy 降级到 1.x 系列的最新版本,与你的 TensorFlow 版本兼容。