科技 > 电脑产品 > CPU

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

72人参与 2025-03-12 CPU

前言

下载的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内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)
打赏 微信扫一扫 微信扫一扫

您想发表意见!!点此发布评论

推荐阅读

64位和32位CPU有什么区别? 电脑32位和62位系统区别介绍

03-07

买主板送顶级CPU! 铭凡BD795i SE ITX主板全面测评

03-04

CPU散热你了解多少? CPU 热节流机制的利与弊分析

02-19

AMD 9000系CPU的平民化座驾! 华硕 B850 重炮手 WIFI主板测评

01-21

2025年不加国补也划算的高性价比千元级平板推荐

01-11

一文看懂主板上的CPU_FAN与CPU_OPT接口有什么区别?

01-11

猜你喜欢

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论