python - 使用 TensorFlow 作为后端的 keras 出错
问题描述
我在我的项目的开始,我刚刚导入了我需要的包:
from keras.models import Sequential
from keras.layers import Dense
显然 TensorFlow 和 keras 都已经安装好了。无论如何,如果我运行代码,我会收到此错误:
Using TensorFlow backend.
ERROR:root:Internal Python error in the inspect module.
Below is the traceback from this internal error.
.
.
.
Traceback (most recent call last):
File "c:\users\franc\appdata\local\programs\python\python37\lib\site-
File "c:\users\franc\appdata\local\programs\python\python37\lib\site-packages\keras\backend\tensorflow_backend.py", line 6, in <module>
from tensorflow.python.eager import context
ImportError: cannot import name 'context' from 'tensorflow.python.eager' (C:\Users\franc\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\python\eager\__init__.py)
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\franc\appdata\local\programs\python\python37\lib\site-packages\IPython\core\interactiveshell.py", line 2040, in showtraceback
stb = value._render_traceback_()
AttributeError: 'ImportError' object has no attribute '_render_traceback_'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "c:\users\franc\appdata\local\programs\python\python37\lib\site-packages\IPython\core\ultratb.py", line 1101, in get_records
return _fixed_getinnerframes(etb, number_of_lines_of_context, tb_offset)
.
.
.
File "c:\users\franc\appdata\local\programs\python\python37\lib\site-packages\tensorflow_core\__init__.py", line 42, in <module>
from . _api.v2 import audio
ImportError: cannot import name 'audio' from 'tensorflow_core._api.v2' (C:\Users\franc\AppData\Roaming\Python\Python37\site-packages\tensorflow_core\_api\v2\__init__.py)
我真的不知道如何解决这个问题,我已经检查了许多网站以遵循两个软件包的正确安装但没有结果。我做了正确的安装,但我仍然有这个错误。
解决方案
您可能想尝试 tensorflow 附带的 keras。这个 keras 仅使用 tensorflow 作为后端,这正是您所需要的:
import tensorflow as tf
from tf.keras import Sequential
from tf.keras.layers import Dense
推荐阅读
- jenkins-pipeline - 如何在 jenkins groovy 中签出 SCM 的子目录?
- python - 转换为.exe后,pyscreenshot不截图
- validation - 第一次循环迭代后未生成 InputTagHelper 验证属性
- reverse-geocoding - 如何在一次调用中获取当前位置的纬度、经度和邮政编码
- postgresql - postgresql - NOW() 但不太具体
- python-3.x - 如果我知道特定的跨度文本,则获取 ID
- c++ - cmath 库中的 asin() 函数返回不准确的值
- java - 使注释处理器读取 Maven 更新上的 src/main/resources 文件的方法
- c# - Microsoft.VisualBasic 的 InputBox 无法在我的系统上呈现
- c++ - ldap_search_s 返回 LDAP_OPERATIONS_ERROR