conda@tensorflow2安装@2.10+安装@tensorflow GPU(CUDA) version installation
创始人
2025-06-01 01:52:56
0

文章目录

    • ref
    • 前言
    • 查找当前channel可用的tensorflow
    • CPU版本
    • gpu版本
      • 英文网站@内容更新及时
      • 安装过程🎈
        • 预览安装结果(tf2.10 GPU version(CUDA11.2) for windows native)
      • windows native
        • tensorflow版本和cuda以及pyton版本的配套
        • 部分版本表格:GPU
      • 中文网站@内容可能过期
      • Tensorflow版本检测
      • GPU检测
    • 附@ 安装流程@windows native GPU
    • Step-by-step instructions🎈
      • 1. System requirements
      • 2. Install Microsoft Visual C++ Redistributable🎈
      • 3. Install Miniconda
      • 4. Create a conda environment
      • 5. GPU setup
      • 6. Install TensorFlow
      • 7. Verify install

ref

  • Tensorflow :: Anaconda.org

前言

  • 虽然现在pytorch可能更受欢迎,但是还是由不少项目用的tensorflow&Keras(老项目更多)
  • 趁此机会,再梳理以下安装tensorflow安装过程
  • 踩了一些坑,
  • 特此记录

查找当前channel可用的tensorflow

  • Loading channels: ...working... done
    # Name                       Version           Build  Channel             
    tensorflow                     1.7.0               0  anaconda/pkgs/main  
    tensorflow                     1.7.1               0  anaconda/pkgs/main  
    ...
    tensorflow                    2.10.0 gpu_py39h9bca9fa_0  anaconda/pkgs/main  
    tensorflow                    2.10.0 mkl_py310hd99672f_0  anaconda/pkgs/main  
    tensorflow                    2.10.0 mkl_py38ha5c4042_0  anaconda/pkgs/main  
    tensorflow                    2.10.0 mkl_py39ha510bab_0  anaconda/pkgs/main  
    ...
    
  • 查找2.8版本conda search tensorflow=2.8

    (d:\condaPythonEnvs\tf2.5) PS D:\repos\blogs\python> conda search tensorflow=2.8
    Loading channels: done
    # Name                       Version           Build  Channel
    tensorflow                     2.8.2 eigen_py310h3184f71_0  anaconda/pkgs/main
    tensorflow                     2.8.2 eigen_py37h326eb71_0  anaconda/pkgs/main
    tensorflow                     2.8.2 eigen_py38h0b14ea6_0  anaconda/pkgs/main
    tensorflow                     2.8.2 eigen_py39h9b0e0cb_0  anaconda/pkgs/main
    tensorflow                     2.8.2 gpu_py310h5cc41f4_0  anaconda/pkgs/main
    tensorflow                     2.8.2 gpu_py37h39c650d_0  anaconda/pkgs/main
    tensorflow                     2.8.2 gpu_py38he639981_0  anaconda/pkgs/main
    tensorflow                     2.8.2 gpu_py39h5ca5225_0  anaconda/pkgs/main
    tensorflow                     2.8.2 mkl_py310h517747f_0  anaconda/pkgs/main
    tensorflow                     2.8.2 mkl_py37h31f2aba_0  anaconda/pkgs/main
    tensorflow                     2.8.2 mkl_py38h6f30489_0  anaconda/pkgs/main
    tensorflow                     2.8.2 mkl_py39hfd350ca_0  anaconda/pkgs/main
    

CPU版本

  • cpu版本很简单(为例加速,可以更换国内源)
    • pip 安装pip install tensorflow
    • conda 安装,conda install tensorflow

gpu版本

  • 下面以英文版官方网站的安装教程为主

英文网站@内容更新及时

安装过程🎈

  • Install TensorFlow with pip
  • 用户根据官方文档安装即可

预览安装结果(tf2.10 GPU version(CUDA11.2) for windows native)

  • (d:\condaPythonEnvs\tf210) PS C:\Users\cxxu\Desktop> py
    Python 3.9.16 (main, Mar  8 2023, 10:39:24) [MSC v.1916 64 bit (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))
    [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]
    >>> import tensorflow as tf
    >>> print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
    Num GPUs Available:  1
    >>> import tensorflow as tf
    >>> print("TensorFlow version: ", tf.__version__)
    TensorFlow version:  2.10.0
    >>>
    
  • 完整的安装流程我放在最后一节(如果官网不方便打开的话)

  • 此处需要强调的是,tensorflow的安装不要用conda install

    • cudatoolkit和cudnn可以用conda install安装
      • conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0
    • 我本人是喜欢用conda install,但是安装tensorflow的时候则应该用pip install
      • 我试过用conda install,发现识别不了gpu
        • 官方给出的理由是,tf团队只再pypi发布:Note: Do not install TensorFlow with conda. It may not have the latest stable version. pip is recommended since TensorFlow is only officially released to PyPI.
      • 如果你已经用conda install tensorflow安装完,那么用conda remove tensorflow卸载
        • 这可能会导致protobuf被卸载掉
        • 没关系,当您检验tf的安装时,如果由缺失某些包,那么用conda install 安装回来
      • 然后重新用pip install tensorflow<2.11再安装一遍
        • 如果缺失protobuf,那么执行conda install protobuf进行依赖修复

windows native

  • 根据当下的版本发行情况来看,现在tensorflow团队更加重视Posix规范的系统(比如linux)构建的版本

  • 对于windows版本的主要支持已经转义到了WSL2子系统了

  • # Anything above 2.10 is not supported on the GPU on Windows Native
    pip install "tensorflow<2.11" 
    

tensorflow版本和cuda以及pyton版本的配套

  • Build from source | TensorFlow
    • 如果不是英文,请将中文切换到**英文**(中文内容可能是旧的)

部分版本表格:GPU

  • GPU

  • VersionPython versionCompilerBuild toolscuDNNCUDA
    tensorflow-2.11.03.7-3.10GCC 9.3.1Bazel 5.3.08.111.2
    tensorflow-2.10.03.7-3.10GCC 9.3.1Bazel 5.1.18.111.2
    tensorflow-2.9.03.7-3.10GCC 9.3.1Bazel 5.0.08.111.2
    tensorflow-2.8.03.7-3.10GCC 7.3.1Bazel 4.2.18.111.2
    tensorflow-2.7.03.7-3.9GCC 7.3.1Bazel 3.7.28.111.2
    tensorflow-2.6.03.6-3.9GCC 7.3.1Bazel 3.7.28.111.2
    tensorflow-2.5.03.6-3.9GCC 7.3.1Bazel 3.7.28.111.2

中文网站@内容可能过期

  • 我不得不吐槽以下中文版网站部分内容过期不更新的问题

    • 2023年查阅时发现更新日期为:最后更新时间 (UTC):2021-10-06
      • 相干内容只到2.6
    • 而其他tensorflow中文的首页因为是最新的(2.11)
      • 第一反映应该不至于说内容过期,没想到有的页面真的过期了
      • 还是我查找tf2.11为什么无法检测到GPU的时候找到的论坛截图才知道:Tensorflow 2.10 doesn’t detect GPU - General Discussion - TensorFlow Forum
  • GPU 支持 | TensorFlow

  • tensorflow-gpu · PyPI

  • 老版本(tf2.6GPU版是可以检测到CUDA12.0的

  • 但是令人惊讶的是,高版本的tf2.11竟然检测不到CUDA12.0的显卡(实际上是可以的))

    • 查阅官方文档后,发现tensorflow的软件架构已经发生了较大变化,为了确保正确识别,建议严格按照最新的官方文档(英文版)的指导按照

Tensorflow版本检测

  • import tensorflow as tf
    print("TensorFlow version: ", tf.__version__)

GPU检测

  • 检测当前tensorflow是支持GPU版本

    • import tensorflow as tf
      tf.test.is_built_with_cuda()
      
    • 返回True说明该tensorflow版本支持CUDA,但是这不等同于说明你的计算机可以使用CUDA

      • 比如您的显卡不是NVIDIA的,即使软件支持,缺少合适的硬件也徒劳
  • import tensorflow as tf
    print("Num GPUs Available: ", len(tf.config.list_physical_devices('GPU')))
    
    • 结果大于1时说明至少由1块GPU可用
    • 如果tf.test.is_built_with_cuda()是False,那么上述结果只能是0

附@ 安装流程@windows native GPU

  • 最后一个支持Native运行的windows版本

Step-by-step instructions🎈

  • Linux

  • MacOS

  • Windows Native

  • Windows WSL2

  • Caution: TensorFlow 2.10 was the last TensorFlow release that supported GPU on native-Windows. Starting with TensorFlow 2.11, you will need to install TensorFlow in WSL2,

  • or install tensorflow-cpu and, optionally, try the TensorFlow-DirectML-Plugin

1. System requirements

  • Windows 7 or higher (64-bit)

Note: Starting with TensorFlow 2.10, Windows CPU-builds for x86/x64 processors are built, maintained, tested and released by a third party: Intel. Installing the windows-native tensorflow or tensorflow-cpu package installs Intel’s tensorflow-intel package. These packages are provided as-is. Tensorflow will use reasonable efforts to maintain the availability and integrity of this pip package. There may be delays if the third party fails to release the pip package. See this blog post for more information about this collaboration.

2. Install Microsoft Visual C++ Redistributable🎈

Install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017, and 2019. Starting with the TensorFlow 2.1.0 version, the msvcp140_1.dll file is required from this package (which may not be provided from older redistributable packages). The redistributable comes with Visual Studio 2019 but can be installed separately:

  1. Go to the Microsoft Visual C++ downloads.
  2. Scroll down the page to the Visual Studio 2015, 2017 and 2019 section.
  3. Download and install the Microsoft Visual C++ Redistributable for Visual Studio 2015, 2017 and 2019 for your platform.

Make sure long paths are enabled on Windows.

3. Install Miniconda

Miniconda is the recommended approach for installing TensorFlow with GPU support. It creates a separate environment to avoid changing any installed software in your system. This is also the easiest way to install the required software especially for the GPU setup.

Download the Miniconda Windows Installer. Double-click the downloaded file and follow the instructions on the screen.

4. Create a conda environment

Create a new conda environment named tf with the following command.

conda create --name tf python=3.9

You can deactivate and activate it with the following commands.

conda deactivate
conda activate tf

Make sure it is activated for the rest of the installation.

5. GPU setup

You can skip this section if you only run TensorFlow on CPU.

First install NVIDIA GPU driver if you have not.

Then install the CUDA, cuDNN with conda.

conda install -c conda-forge cudatoolkit=11.2 cudnn=8.1.0

6. Install TensorFlow

TensorFlow requires a recent version of pip, so upgrade your pip installation to be sure you’re running the latest version.

pip install --upgrade pip

Then, install TensorFlow with pip.

Note: Do not install TensorFlow with conda. It may not have the latest stable version. pip is recommended since TensorFlow is only officially released to PyPI.

# Anything above 2.10 is not supported on the GPU on Windows Native
pip install "tensorflow<2.11" 

7. Verify install

Verify the CPU setup:

python -c "import tensorflow as tf; print(tf.reduce_sum(tf.random.normal([1000, 1000])))"

If a tensor is returned, you’ve installed TensorFlow successfully.

Verify the GPU setup:

python -c "import tensorflow as tf; print(tf.config.list_physical_devices('GPU'))"

If a list of GPU devices is returned, you’ve installed TensorFlow successfully.

相关内容

热门资讯

《自然》《科学》《细胞》大满贯... 得益于上海的稳定支持、学科交叉和团队合作,中国科学院分子植物科学卓越创新中心研究员张余潜心挑战世界级...
成都90后小伙让刀剑重获新生:... 转自:青蕉视频 【#成都90后小伙让刀剑重获新生#:“做...
50余场活动点亮“书香”童年   为迎庆“六一”国际儿童节,首都图书馆携手全市各区公共图书馆,依托北京市红领巾读书活动平台,以“童...
马斯克:不想为美国政府所做的一... 当地时间6月1日,美国企业家埃隆·马斯克在接受美国哥伦比亚广播公司的采访时表示,他不想公开反对美国政...
沈阳市于洪区文旅局副局长彭勃,... 彭勃 澎湃新闻获悉,沈阳市于洪区文旅局副局长彭勃近日已调任沈阳永安经济开发区管委会工作。彭勃1983...
纳夫罗茨基,赢了 独立候选人纳夫罗茨基赢得波兰总统选举根据波兰国家选举委员会网站2日公布的统计结果,独立候选人卡罗尔·...
杭州适合500元创业项目,杭州... 12月30日,观点网讯:,广宇集团有限公司发布对外投资公告。新媒体获悉,广宇集团董事会审议通过《关于...
创业招工视频,创业小白打工视频... 有人说,颜值就是正义,黑丝就是方向,高跟就是支撑。和联通小姐跳舞比知识普及更有吸引力。丝袜高跟鞋一穿...
无本钱创业的十个行业,低本钱的... 现实生活中,有很多宝妈不能出去为家人和孩子工作,但如果不工作,经济压力也很大。然而,带孩子出去工作是...
高校师生摆摊展示“家乡美”   本报讯(记者何蕊)尝一尝豆汁的味道、和志愿者学“手绢功”、猜粤语谜语……北京交通大学近日举办第十...
U16国足净胜3球击败澳大利亚...   本报讯(记者李立)昨晚,U16国足迎来呼和浩特国际足球邀请赛最后一场比赛。在开场2分钟就丢一球的...
中国人工智能创新应用区,全国人... n http://www .新浪。com/=http://。新浪。com/' http://ww...
创业致富养殖小龙虾,贫困户创业... 近年来,广西百色市乐叶县把发展村级集体经济作为产业扶贫的重要抓手,着力解决村级集体经济发展问题,积极...
光大银行宁波分行为春耕备足资金... 转自:中国银行保险报网中国银行保险报网讯 【记者 杜肖锦】近日,光大银行宁波分行相关负责人带队开展“...
儿童美术创业思路,少儿美术创业...   1月14日,娃哈哈创始人兼董事长宗在出席1月13日举行的2021风云浙商颁奖典礼时,被问及娃哈哈...
创业板打新债需要什么条件,创业... 小伙伴们好~这是一个全新的话题。我将购买新债券并在前一天晚上上市,准时提醒你。别忘了订阅~#新债务认...
华普亿方创业实训系统攻略,华普...         鲸媒讯(文/琴非白)1月15日,北京华普方毅教育科技股份有限公司(简称“华普教育”)...
猛跌!突现1.1折!网友:抓紧... 近期,机票价格处于“跳水季”。记者在多家在线旅游平台上看到,目前从杭州萧山国际机场出发前往三亚、成都...
青岛城投集团办公地址,青岛城投... 9月9日下午,青岛市文化和旅游局副局长蔡全基一行到访,与陕西文投集团总经理陆涛就文化和旅游、影视剧创...
外国比较火的创业项目,服装创新... 3354这是我们分享的第三个创业案例故事。qvj2l q49k 0/6a 12953113 E...