Tensorflow环境配置

参考资料

机器学习:完整配置TensorFlow开发环境

tensorflow与高版本numpy不兼容的问题

环境配置

安装Anaconda

安装过程需要注意选择自动添加环境变量检查项。选择该项后安装包会自动将安装路径注册到Windows系统变量,命令行窗口运行Anaconda时,不会出现找不到Anaconda问题。

image

创建tensorflow-cpu环境

image

查看环境命令

若需要查看当前Anaconda有哪些虚拟环境,可以使用下面的命令:

conda env list

激活环境命令

该命令是常用的操作命令,使用该命令激活环境后,才能进入该虚拟环境进行编程。命令如下:

conda activate ENV_NAME

其中ENV_NAME是要进入的环境名称。进入成功后,一般可以看到命令行的最左端括号中也切换成了ENV_NAME的名称。另外,如果想要从一个环境A切换到另一个环境B去,也可以直接执行该命令,将ENV_NAME写为B即可。

退出环境命令

若需要退出当前虚拟开发环境,可以使用下面的命令:

conda deactivate

该命令直接退回到base环境。

删除环境命令

该命令需谨慎使用,因为该命令会删除虚拟环境下的所有数据。命令如下:

conda remove -n ENV_NAME --all

进入tensorflow-cpu环境安装python依赖包

conda activate tensorflow-cpu

在tensorflow-cpu环境下使用pip工具软件安装numpy、matplotlib、pillow、pandas、scikit-learn。

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

再安装tensorflow-cpu版本

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

注意:keras版本需要和tensorflow版本相同

报错:cannot import name ‘dtensor‘ from ‘tensorflow.compat.v2.experimental‘ (/Users/pxs/anaconda3/lib_cannot import name 'dtensor

遇见的报错

遇见的报错主要是numpy、keras版本和tensorflow不适配的问题,最终正常运行版本如下:

版本
tensorflow 2.6
keras 2.6
numpy 1.22.4

numpy报错

ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
contourpy 1.2.0 requires numpy<2.0,>=1.20, but you have numpy 1.19.5 which is incompatible.
matplotlib 3.8.2 requires numpy<2,>=1.21, but you have numpy 1.19.5 which is incompatible.
pandas 2.2.0 requires numpy<2,>=1.22.4; python_version < "3.11", but you have numpy 1.19.5 which is incompatible.
scipy 1.12.0 requires numpy<1.29.0,>=1.22.4, but you have numpy 1.19.5 which is incompatible.

numpy版本似乎有点问题,直接更新numpy

pip install numpy --upgrade

protoc版本报错

在python中运行

import tensorflow as tf

也出现报错

TypeError: Descriptors cannot not 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).

返回tensorflow-cpu环境

pip install protobuf==3.19.6

numpy新报错

再次运行python,新报错:

AttributeError: module 'numpy' has no attribute 'object'.
`np.object` was a deprecated alias for the builtin `object`. To avoid this error in existing code, use `object` by itself. Doing this will not modify any behavior and is safe.
The aliases was originally deprecated in NumPy 1.20; for more details and guidance see the original release note at:

似乎是numpy版本过高导致的,回到tensorflow-cpu环境慢慢尝试安装各个版本的numpy,先尝试一下numpy1.19.2,遇见新的报错,给出了numpy版本的关键信息,需要1.22.4以上版本的numpy:

2024-02-02 19:41:41.093851: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2024-02-02 19:41:41.094246: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
D:\Anaconda\envs\tensorflow-cpu\lib\site-packages\keras_preprocessing\image\affine_transformations.py:13: UserWarning: A NumPy version >=1.22.4 and <1.29.0 is required for this version of SciPy (detected version 1.19.3)
  import scipy
RuntimeError: module compiled against API version 0xf but this version of numpy is 0xd
RuntimeError: module compiled against API version 0xf but this version of numpy is 0xd

安装numpy1.22.4后运行正常

PS C:\Users\13666\Desktop\机器学习时间> & D:/Anaconda/envs/tensorflow-cpu/python.exe c:/Users/13666/Desktop/机器学习时间/test.py
2024-02-02 19:46:35.075765: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2024-02-02 19:46:35.076293: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
hello tensorflow!,运行版本:2.6.0

在vc中将解释器配置为tensorflow-cpu中的python解释器,开始tensorflow之旅!

posted @ 2024-02-02 20:29  Exungsh💫  阅读(126)  评论(0编辑  收藏  举报