我在Windows 10机器上运行TensorFlow时遇到了问题。代码在我的MacOS机器上运行良好。
Traceback (most recent call last):
File "c:UsersFynnDocumentsGitHubAlpacaTradingBotai.py", line 15, in <module>
from keras.models import Sequential, load_model
File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskeras__init__.py", line 24, in <module>
from keras import models
File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskerasmodels__init__.py", line 18, in <module>
from keras.engine.functional import Functional
File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskerasenginefunctional.py", line 24, in <module>
from keras.dtensor import layout_map as layout_map_lib
File "C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packageskerasdtensor__init__.py", line 22, in <module>
from tensorflow.compat.v2.experimental import dtensor as dtensor_api # pylint: disable=g-import-not-at-top
ImportError: cannot import name 'dtensor' from 'tensorflow.compat.v2.experimental' (C:UsersFynnAppDataLocalProgramsPythonPython39libsite-packagestensorflow_apiv2compatv2experimental__init__.py)
这可能是由于tensorflow
和keras
版本之间的不兼容性造成的。特别是在tensorflow==2.6.0
和keras==2.9.0
中,我看到了这种情况,尽管如果其他版本也会导致这种情况,我也不会感到惊讶。
更新tensorflow
版本:
pip install tensorflow==2.8
或将keras
版本降级:
pip install keras==2.6
直接运行:
pip install --disable-pip-version-check --no-cache-dir tensorflow
必须在site-package中删除tensorflow和keras文件夹。打开管理命令提示符,使用这些代码
pip uninstall tensorflow
pip install tensorflow --ignore-installed
我尝试了许多解决方案都无济于事,最后这对我有用!
pip3 uninstall tensorflow absl-py astunparse flatbuffers gast google-pasta grpcio h5py keras keras-preprocessing libclang numpy opt-einsum protobuf setuptools six tensorboard tensorflow-io-gcs-filesystem termcolor tf-estimator-nightly typing-extensions wrapt
pip3 install --disable-pip-version-check --no-cache-dir tensorflow