当前位置:   article > 正文

TensorFlow安装CPU版本和GPU版本

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
  • 1

“myenvname”为自己的虚拟环境名字
在这里插入图片描述

2.激活虚拟环境

继续输入

activate myenvname
  • 1

“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
  • 1

在这里插入图片描述

4.验证是否下载成功

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

import tensorflow as tf
  • 1

在这里插入图片描述
成功

二、TensorFlow安装GPU版本

本例子,下载的CUDA版本是11.5.2,cuDNN的版本是8.3.2,Python环境是3.9,tensorflow-gpu的版本是2.7.0。注:CUDA、cuDNN、python的环境要对应,不然会安装失败(很重要!!!)
CUDA和cuDNN具体安装教程看这篇GPU配置环境,这个教程很详细的介绍了GPU的配置。

1.新建虚拟环境

打开Anaconda Prompt,输入

conda create -n myenvname python=3.9
  • 1

“myenvname”为自己的虚拟环境名字
在这里插入图片描述

2.激活虚拟环境

activate myenvname
  • 1

“myenvname”为自己的虚拟环境名字
在这里插入图片描述

3.安装tensorflow-gpu

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

在这里插入图片描述

4.验证是否下载成功

进入python环境,导入tensorflow

 import tensorflow as tf
  • 1

要是遇到这个问题,提示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
>>>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32

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

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

在这里插入图片描述
再次进入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
>>>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95

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

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

在这里插入图片描述
进入python环境,输入

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

在这里插入图片描述
查看版本2.7.0,版本正确。末尾显示True,TensorFlow检测到可用的GPU,安装成功,exit()退出python环境

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

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

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

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号