我正在try 创建一个运行tensorflow的隔离虚拟环境&tf2onnx在vscode中使用jupyter笔记本.

tf2onnx packge建议使用python 3.7,而我的本地3.7.9版本通常可以很好地与tensorflow项目配合使用,因此我使用pyenv将本地和全局版本设置为3.7.9.

以下是我的设置过程:

python -m venv .venv

然后在vscode中启动新终端后:

pip install tensorflow==2.7.0

pip freeze > requirements.txt

在此之后,在我jupyter笔记本的一个单元格中,以下行失败

import tensorflow.keras as keras

例外情况:

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).

此时,在我的需求文件中,protobuf包版本显示为v4.21.0.在安装tensorflow之前,我try 将3.20.1版本预安装到虚拟环境中,但这没有产生任何效果.

以下是安装tensorflow后的完整需求文件:

absl-py==1.0.0
astunparse==1.6.3
cachetools==5.1.0
certifi==2022.5.18.1
charset-normalizer==2.0.12
flatbuffers==2.0
gast==0.4.0
google-auth==2.6.6
google-auth-oauthlib==0.4.6
google-pasta==0.2.0
grpcio==1.46.3
h5py==3.7.0
idna==3.3
importlib-metadata==4.11.4
keras==2.7.0
Keras-Preprocessing==1.1.2
libclang==14.0.1
Markdown==3.3.7
numpy==1.21.6
oauthlib==3.2.0
opt-einsum==3.3.0
protobuf==4.21.0
pyasn1==0.4.8
pyasn1-modules==0.2.8
requests==2.27.1
requests-oauthlib==1.3.1
rsa==4.8
six==1.16.0
tensorboard==2.9.0
tensorboard-data-server==0.6.1
tensorboard-plugin-wit==1.8.1
tensorflow==2.7.0
tensorflow-estimator==2.7.0
tensorflow-io-gcs-filesystem==0.26.0
termcolor==1.1.0
typing-extensions==4.2.0
urllib3==1.26.9
Werkzeug==2.1.2
wrapt==1.14.1
zipp==3.8.0

推荐答案

最近的change in protobuf导致TensorFlow to break.在安装TensorFlow之前降级可能不起作用,因为TensorFlow可能会升级版本本身.判断安装过程中是否发生这种情况.

您可能希望:

使用降级

pip install --upgrade "protobuf<=3.20.1"

安装TensorFlow后,或

升级TensorFlow至最新版本,因为TensorFlow已在其2.9.1版本中更新了安装文件.

Python相关问答推荐

Pandas 都是(),但有一个门槛

在Pandas DataFrame操作中用链接替换'方法的更有效方法

如何在Python脚本中附加一个Google tab(已经打开)

Pandas:将多级列名改为一级

把一个pandas文件夹从juyter笔记本放到堆栈溢出问题中的最快方法?

Python+线程\TrocessPoolExecutor

使用Python更新字典中的值

Odoo 16使用NTFS使字段只读

如何禁用FastAPI应用程序的Swagger UI autodoc中的application/json?

如何防止Pandas将索引标为周期?

Python—转换日期:价目表到新行

使用__json__的 pyramid 在客户端返回意外格式

什么是一种快速而优雅的方式来转换一个包含一串重复的列,而不对同一个值多次运行转换,

如何将泛型类类型与函数返回类型结合使用?

你能把函数的返回类型用作其他地方的类型吗?'

我什么时候应该使用帆布和标签?

如何防止html代码出现在quarto gfm报告中的pandas表之上

如果不使用. to_list()[0],我如何从一个pandas DataFrame中获取一个值?

替换包含Python DataFrame中的值的<;

具有不同坐标的tkinter canvs.cocords()和canvs.moveto()