【问题标题】:TensorFlow 2 and Keras:TensorFlow 2 和 Keras:
【发布时间】:2023-03-10 10:22:01
【问题描述】:

当我想在 TensorFlow 2 中使用 Keras 时,我收到了这个错误:

AttributeError: 模块 'tensorflow' 没有属性 'get_default_graph'

【问题讨论】:

  • 你的tensorflow更新到最新版本了吗?您是否检查过是否调用了正确的代码文件?试试这个 - 尝试从 spyder IDE 运行这段代码。一旦遇到此错误,请检查是否调用了正确版本的 ops.py 文件。

标签: tensorflow tensorflow2.0


【解决方案1】:

Keras API (https://keras.io/) 有多种实现,包括原始实现和参考实现 (https://github.com/keras-team/keras),还有其他各种实现,包括 TensorFlow 的一部分 tf.keras。

因此,您可以通过两种方式将 Keras 与 TensorFlow 结合使用:

  1. 将参考实现与 TensorFlow 后端结合使用。但是,此实现尚未更新以支持 TensorFlow 2(截至 2019 年 6 月)。
  2. 使用 TensorFlow 的实现,tf.keras。这个适用于 TF 2。

要使用 tf.keras,您必须确保使用正确的导入:

from tensorflow import keras
# NOT: import keras

同样,使用:

from tensorflow.keras.layers import Dense
# Not from keras.layers import Dense

希望这会有所帮助。

【讨论】:

    【解决方案2】:

    由于 TensorFlow 2 默认为急切执行,Keras 需要进行一些更改才能与其兼容,但在此之前需要之前版本的 TensorFlow。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-13
      • 2019-05-30
      • 2021-09-08
      • 1970-01-01
      • 2020-03-27
      • 2021-06-08
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多