python - 使用 TensorFlow 作为后端的 keras 出现错误

标签 python python-3.x 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

引用:https://www.tensorflow.org/guide/keras/overview

关于python - 使用 TensorFlow 作为后端的 keras 出现错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59840411/

相关文章:

python - 多处理/psycopg2 TypeError : can't pickle _thread. RLock 对象

python - 在python中使用c代码时出错

python-3.x - Python 就地 bool 运算符

python smtplib登录报错smtplib.SMTPException : STARTTLS extension not supported by server

python - 同时分配多个实例的属性

python - 如何分离xpath语句的输出

python - 如何正确使用 Keras LSTM batch_input_size

python - ValueError : features should be a dictionary of `Tensor` s. 给定类型:<class 'NoneType' >

python - 将 KERAS 张量转换为 K.tf.int32

python - 树莓派 RS485/Uart Modbus