GPU版tensorflow安装

硬件配置:Win11+RTX4060

本文章记录了安装CUDA11.2+cuDNN8.1+Tensorflow2.10.0的简要过程

Build from source on Windows  |  TensorFlowicon-default.png?t=N7T8https://tensorflow.google.cn/install/source_windows#tested_build_configurations

前言和详细安装步骤与博主PyTorch安装步骤类似:GPU版PyTorch安装_torch gpu版本-CSDN博客icon-default.png?t=N7T8https://blog.csdn.net/cherryc_/article/details/136301111?spm=1001.2014.3001.5501

一、下载安装CUDA及cuDNN

(1)下载安装CUDA 11.2.2【具体安装步骤见Pytorch安装文章记录】

CUDA Toolkit Archive | NVIDIA Developericon-default.png?t=N7T8https://developer.nvidia.com/cuda-toolkit-archive

  1. 选择版本并下载:

  2. 选择安装位置并按上一篇博客的方法安装:

  3. 查看系统变量更新:

(2)下载安装cuDNN 8.1

https://developer.nvidia.com/rdp/cudnn-archive#a-collapse51bicon-default.png?t=N7T8https://developer.nvidia.com/rdp/cudnn-archive#a-collapse51b

  1. 解压缩文件夹

  2. 将三个文件夹拷贝到CUDA安装目录的文件夹中

  3. 验证安装通过:

二、安装Tensorflow

  1. 创建python 3.10虚拟环境:

    conda create --prefix=D:\\conda_env\\tensorflow_2_10_0 python=3.10
    
  2. 激活虚拟环境

  3. 使用pip命令安装tensorflow 2.10.0(不需要特别指明gpu版本):

    pip install tensorflow==2.10.0
    

  4. 验证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
    

    报错原因:

    A module that was compiled using NumPy 1.x cannot be run in NumPy 2.0.0 as it may crash. To support-CSDN博客文章浏览阅读1.7k次,点赞8次,收藏8次。6月17日 numpy 更新到了2.0 版本,NumPy 2.0.0 是一个重大的版本更新,引入了许多变化,包括新功能和重大更改。一些库还没来得及更新。这时候如果着急使用,可以先回退版本。NumPy 2.0.0 是一个重大的版本更新,引入了许多变化,包括新功能和重大更改。_a module that was compiled using numpy 1.x cannot be run in numpy 2.0.0 as ihttps://blog.csdn.net/qq_51116518/article/details/140112113

    https://blog.csdn.net/C_C666/article/details/139871484?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-1-139871484-blog-140112113.235^v43^pc_blog_bottom_relevance_base9&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-1-139871484-blog-140112113.235^v43^pc_blog_bottom_relevance_base9&utm_relevant_index=2icon-default.png?t=N7T8https://blog.csdn.net/C_C666/article/details/139871484?spm=1001.2101.3001.6650.1&utm_medium=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-1-139871484-blog-140112113.235%5Ev43%5Epc_blog_bottom_relevance_base9&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2~default~BlogCommendFromBaidu~Rate-1-139871484-blog-140112113.235%5Ev43%5Epc_blog_bottom_relevance_base9&utm_relevant_index=2

    解决方案:降级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')))
    

  • 15
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值