硬件配置:Win11+RTX4060
本文章记录了安装CUDA11.2+cuDNN8.1+Tensorflow2.10.0的简要过程
前言和详细安装步骤与博主PyTorch安装步骤类似:GPU版PyTorch安装_torch gpu版本-CSDN博客https://blog.csdn.net/cherryc_/article/details/136301111?spm=1001.2014.3001.5501
一、下载安装CUDA及cuDNN
(1)下载安装CUDA 11.2.2【具体安装步骤见Pytorch安装文章记录】
CUDA Toolkit Archive | NVIDIA Developerhttps://developer.nvidia.com/cuda-toolkit-archive
-
选择版本并下载:
-
选择安装位置并按上一篇博客的方法安装:
-
查看系统变量更新:
(2)下载安装cuDNN 8.1
-
解压缩文件夹
-
将三个文件夹拷贝到CUDA安装目录的文件夹中
-
验证安装通过:
二、安装Tensorflow
-
创建python 3.10虚拟环境:
conda create --prefix=D:\\conda_env\\tensorflow_2_10_0 python=3.10
-
激活虚拟环境
-
使用pip命令安装tensorflow 2.10.0(不需要特别指明gpu版本):
pip install tensorflow==2.10.0
-
验证tensorflow是否可以正常调用GPU:
python >>> import tensorflow as tf
报如下错误:
>>> import tensorflow as tf A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.1 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 "<stdin>", line 1, in <module> File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\__init__.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\__init__.py", line 37, in <module> from tensorflow.python.eager import context File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\eager\\context.py", line 35, in <module> from tensorflow.python.client import pywrap_tf_session File "D:\\conda_env\\tensorflow2_10_0\\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.1 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 "<stdin>", line 1, in <module> File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\__init__.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\__init__.py", line 42, in <module> from tensorflow.python import data File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\__init__.py", line 21, in <module> from tensorflow.python.data import experimental File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\experimental\\__init__.py", line 96, in <module> from tensorflow.python.data.experimental import service File "D:\\conda_env\\tensorflow2_10_0\\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 "D:\\conda_env\\tensorflow2_10_0\\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 "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\experimental\\ops\\compression_ops.py", line 16, in <module> from tensorflow.python.data.util import structure File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\util\\structure.py", line 23, in <module> from tensorflow.python.data.util import nest File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\util\\nest.py", line 36, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\framework\\sparse_tensor.py", line 24, in <module> from tensorflow.python.framework import constant_op File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\framework\\constant_op.py", line 25, in <module> from tensorflow.python.eager import execute File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\eager\\execute.py", line 23, in <module> from tensorflow.python.framework import dtypes File "D:\\conda_env\\tensorflow2_10_0\\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 "<stdin>", line 1, in <module> File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\__init__.py", line 37, in <module> from tensorflow.python.tools import module_util as _module_util File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\__init__.py", line 42, in <module> from tensorflow.python import data File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\__init__.py", line 21, in <module> from tensorflow.python.data import experimental File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\experimental\\__init__.py", line 96, in <module> from tensorflow.python.data.experimental import service File "D:\\conda_env\\tensorflow2_10_0\\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 "D:\\conda_env\\tensorflow2_10_0\\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 "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\experimental\\ops\\compression_ops.py", line 16, in <module> from tensorflow.python.data.util import structure File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\util\\structure.py", line 23, in <module> from tensorflow.python.data.util import nest File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\data\\util\\nest.py", line 36, in <module> from tensorflow.python.framework import sparse_tensor as _sparse_tensor File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\framework\\sparse_tensor.py", line 24, in <module> from tensorflow.python.framework import constant_op File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\framework\\constant_op.py", line 25, in <module> from tensorflow.python.eager import execute File "D:\\conda_env\\tensorflow2_10_0\\lib\\site-packages\\tensorflow\\python\\eager\\execute.py", line 23, in <module> from tensorflow.python.framework import dtypes File "D:\\conda_env\\tensorflow2_10_0\\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
报错原因:
解决方案:降级Numpy,注意引号的使用
pip install "numpy<2"
成功解决,tensorflow成功调用GPU:
>>> import tensorflow as tf >>> print(tf.__version__)#查看tensorflow版本 >>> print(tf.config.list_physical_devices('GPU')) >>> print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))