TensorFlow安装CPU版本和GPU版本的实现步骤

 更新时间:2025年03月12日 09:41:54   作者:席惜兮兮  
本文主要介绍了TensorFlow安装CPU版本和GPU版本的实现步骤,文中通过图文示例介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

前言

下载的Anaconda是Anaconda3-2024.02-1-Windows-x86_64版本

一、TensorFlow安装CPU版本

本例子,下载的Python版本为3.11.7和tensorflow版本为2.16.1

1.新建虚拟环境

打开Anaconda Prompt,输入

conda create -n myenvname python=3.11.7

“myenvname”为自己的虚拟环境名字

在这里插入图片描述

2.激活虚拟环境

继续输入

activate myenvname

“myenvname”为自己的虚拟环境名字

在这里插入图片描述

3.下载tensorflow

直接安装tensorflow会遇到以下报错,这是提示有一些依赖没有安装

在这里插入图片描述

所以我先安装了依赖再下载tensorflow

pip install joblib==1.2.0 scipy==1.11.4 tabulate==0.9.0 tqdm==4.65.0 tensorflow==2.16.1 -i https://mirrors.aliyun.com/pypi/simple

在这里插入图片描述

4.验证是否下载成功

输入ipython,进入交互环境(要是报错,那可能是没有ipython,可以pip list查看一下,没有的话需要下载一个)
导入tensorflow

import tensorflow as tf

在这里插入图片描述

成功

二、TensorFlow安装GPU版本

本例子,下载的CUDA版本是11.5.2,cuDNN的版本是8.3.2,Python环境是3.9,tensorflow-gpu的版本是2.7.0。注:CUDA、cuDNN、python的环境要对应,不然会安装失败(很重要!!!)。

1.新建虚拟环境

打开Anaconda Prompt,输入

conda create -n myenvname python=3.9

“myenvname”为自己的虚拟环境名字

在这里插入图片描述

2.激活虚拟环境

activate myenvname

“myenvname”为自己的虚拟环境名字

在这里插入图片描述

3.安装tensorflow-gpu

pip install tensorflow-gpu==2.7.0 -i https://pypi.tuna.tsinghua.edu.cn/simple

在这里插入图片描述

4.验证是否下载成功

进入python环境,导入tensorflow

 import tensorflow as tf

要是遇到这个问题,提示protobuf版本过低

(tensorflow2) C:\Users\asus>python
Python 3.9.19 (main, May  6 2024, 20:12:36) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\__init__.py", line 41, in <module>
    from tensorflow.python.eager import context
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\eager\context.py", line 33, in <module>
    from tensorflow.core.framework import function_pb2
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\core\framework\function_pb2.py", line 16, in <module>
    from tensorflow.core.framework import attr_value_pb2 as tensorflow_dot_core_dot_framework_dot_attr__value__pb2
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\core\framework\attr_value_pb2.py", line 16, in <module>
    from tensorflow.core.framework import tensor_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__pb2
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\core\framework\tensor_pb2.py", line 16, in <module>
    from tensorflow.core.framework import resource_handle_pb2 as tensorflow_dot_core_dot_framework_dot_resource__handle__pb2
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\core\framework\resource_handle_pb2.py", line 16, in <module>
    from tensorflow.core.framework import tensor_shape_pb2 as tensorflow_dot_core_dot_framework_dot_tensor__shape__pb2
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\core\framework\tensor_shape_pb2.py", line 36, in <module>
    _descriptor.FieldDescriptor(
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\google\protobuf\descriptor.py", line 553, in __new__
    _message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
 1. Downgrade the protobuf package to 3.20.x or lower.
 2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).

More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
>>>

输入exit()退出python环境,回到虚拟环境

pip install protobuf==3.19.6 -i https://pypi.tuna.tsinghua.edu.cn/simple --trusted-host pypi.tuna.tsinghua.edu.cn

在这里插入图片描述

再次进入python环境,输入“import tensorflow as tf”,要是遇到如下问题,提示TensorFlow与NumPy的版本不兼容

(tensorflow2) C:\Users\asus>python
Python 3.9.19 (main, May  6 2024, 20:12:36) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow as tf

A module that was compiled using NumPy 1.x cannot be run in
NumPy 2.0.0 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 "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\__init__.py", line 41, in <module>
    from tensorflow.python.eager import context
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\eager\context.py", line 38, in <module>
    from tensorflow.python.client import pywrap_tf_session
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\client\pywrap_tf_session.py", line 23, 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.0 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 "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\__init__.py", line 46, in <module>
    from tensorflow.python import data
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\__init__.py", line 25, in <module>
    from tensorflow.python.data import experimental
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 98, in <module>
    from tensorflow.python.data.experimental import service
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 374, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 27, in <module>
    from tensorflow.python.data.experimental.ops import compression_ops
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 20, in <module>
    from tensorflow.python.data.util import structure
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\util\structure.py", line 26, in <module>
    from tensorflow.python.data.util import nest
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\util\nest.py", line 40, in <module>
    from tensorflow.python.framework import sparse_tensor as _sparse_tensor
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 28, in <module>
    from tensorflow.python.framework import constant_op
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\framework\constant_op.py", line 29, in <module>
    from tensorflow.python.eager import execute
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\eager\execute.py", line 27, in <module>
    from tensorflow.python.framework import dtypes
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\framework\dtypes.py", line 30, 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 "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\__init__.py", line 41, in <module>
    from tensorflow.python.tools import module_util as _module_util
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\__init__.py", line 46, in <module>
    from tensorflow.python import data
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\__init__.py", line 25, in <module>
    from tensorflow.python.data import experimental
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\__init__.py", line 98, in <module>
    from tensorflow.python.data.experimental import service
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\service\__init__.py", line 374, in <module>
    from tensorflow.python.data.experimental.ops.data_service_ops import distribute
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\ops\data_service_ops.py", line 27, in <module>
    from tensorflow.python.data.experimental.ops import compression_ops
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\experimental\ops\compression_ops.py", line 20, in <module>
    from tensorflow.python.data.util import structure
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\util\structure.py", line 26, in <module>
    from tensorflow.python.data.util import nest
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\data\util\nest.py", line 40, in <module>
    from tensorflow.python.framework import sparse_tensor as _sparse_tensor
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\framework\sparse_tensor.py", line 28, in <module>
    from tensorflow.python.framework import constant_op
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\framework\constant_op.py", line 29, in <module>
    from tensorflow.python.eager import execute
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\eager\execute.py", line 27, in <module>
    from tensorflow.python.framework import dtypes
  File "C:\Users\asus\.conda\envs\tensorflow2\lib\site-packages\tensorflow\python\framework\dtypes.py", line 33, in <module>
    _np_bfloat16 = _pywrap_bfloat16.TF_bfloat16_type()
TypeError: Unable to convert function return value to a Python type! The signature was
        () -> handle
>>>

输入exit()退出python环境,回到虚拟环境

pip install numpy==1.21.6 -i https://pypi.tuna.tsinghua.edu.cn/simple/

在这里插入图片描述

进入python环境,输入

import tensorflow as tf
tf.__version__
tf.test.is_gpu_available()

在这里插入图片描述

查看版本2.7.0,版本正确。末尾显示True,TensorFlow检测到可用的GPU,安装成功,exit()退出python环境

后续我想用ipython查看是否安装成功,出现以下问题进入ipython环境,输入

import tensorflow as tf
tf.__version__
tf.test.is_gpu_available()

在这里插入图片描述

创建虚拟环境的时候指定python版本为3.9,但是这里却显示3.11.7。
末尾显示False,TensorFlow没有检测到可用的GPU。
猜测可能是这个虚拟环境没有ipython,可能用了其他环境的ipython。
解决方案,可以在虚拟环境中用pip list查看虚拟环境中是否有ipython,要是没有,需要安装一个,然后就可以解决了

到此这篇关于TensorFlow安装CPU版本和GPU版本的实现步骤的文章就介绍到这了,更多相关TensorFlow安装CPU和GPU内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

相关文章

  • python如何通过闭包实现计算器的功能

    python如何通过闭包实现计算器的功能

    这篇文章主要介绍了python如何通过闭包实现计算器的功能,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-02-02
  • python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例

    python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例

    这篇文章主要介绍了python GUI库图形界面开发之PyQt5打开保存对话框QFileDialog详细使用方法与实例,需要的朋友可以参考下
    2020-02-02
  • 用Python从0开始实现一个中文拼音输入法的思路详解

    用Python从0开始实现一个中文拼音输入法的思路详解

    中文输入法是一个历史悠久的问题,但也实在是个繁琐的活,不知道这是不是网上很少有人分享中文拼音输入法的原因,接下来通过本文给大家分享使用Python从0开始实现一个中文拼音输入法,需要的朋友可以参考下
    2019-07-07
  • Tensorflow tf.tile()的用法实例分析

    Tensorflow tf.tile()的用法实例分析

    这篇文章主要介绍了Tensorflow tf.tile()的用法实例分析,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-05-05
  • Python对象的属性访问过程详解

    Python对象的属性访问过程详解

    这篇文章主要介绍了Python对象的属性访问过程详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-03-03
  • 使用Python编制一个批处理文件管理器

    使用Python编制一个批处理文件管理器

    在软件开发和系统管理中,批处理文件(.bat)是一种常见且有用的工具,它们可以自动化重复性任务,简化复杂的操作流程,今天,我们将探讨如何使用Python和wxPython创建一个图形用户界面(GUI)应用程序来管理和执行批处理文件,需要的朋友可以参考下
    2025-01-01
  • linux mint中搜狗输入法导致pycharm卡死的问题

    linux mint中搜狗输入法导致pycharm卡死的问题

    这篇文章主要介绍了linux mint中搜狗输入法导致pycharm卡死的问题,这篇文章给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-10-10
  • Python实现壁纸下载与轮换

    Python实现壁纸下载与轮换

    本人对于壁纸一直偏佛系,不爱特意去找一堆壁纸。因此用Python简单地搞了一个自动下载壁纸,定时随机轮换的功能来自娱自乐,顺便分享给大家。
    2020-10-10
  • 图文详解OpenCV中光流以及视频特征点追踪

    图文详解OpenCV中光流以及视频特征点追踪

    光流是空间运动物体在观察成像平面上的像素运动的瞬时速度,是利用图像序列中像素在时间域上的变化以及相邻帧之间的相关性来找到上一帧跟当前帧之间存在的相应关系,这篇文章主要给大家介绍了关于OpenCV中光流以及视频特征点追踪的相关资料,需要的朋友可以参考下
    2021-08-08
  • Python实现读取并保存文件的类

    Python实现读取并保存文件的类

    这篇文章主要介绍了Python实现读取并保存文件的类,涉及Python针对文件的读写操作相关实现技巧,需要的朋友可以参考下
    2017-05-05

最新评论