我应该使用独立的 Keras 库还是 tf.keras?

Should I use the standalone Keras library or tf.keras?

随着 Keras 成为 TensorFlow 的 API,有很多旧版本的 Keras 代码,例如 https://github.com/keiserlab/keras-neural-graph-fingerprint/blob/master/examples.py

from keras import models

使用当前版本的 TensorFlow,我们是否需要将每个 Keras 代码更改为?

from tensorflow.keras import models

你把事情搞混了:

  • Keras (https://keras.io/) 是一个独立于 TensorFlow 的库,它指定了一个高级 API 用于构建和训练神经网络,并且能够使用多个后端之一(其中, TensorFlow) 用于低级张量计算。
  • tf.keras (https://www.tensorflow.org/guide/keras) 在 TensorFlow 中实现了 Keras API 规范。此外,tf.keras API 经过优化,可以与其他 TensorFlow 模块很好地协同工作:您可以将 tf.data 数据集传递给 tf.keras 模型的 .fit() 方法,例如,或将 tf.keras 模型转换为具有 tf.keras.estimator.model_to_estimator 的 TensorFlow 估计器。目前,tf.keras API 是在 TensorFlow 中构建模型时要寻找的高级 API,未来将继续与其他 TensorFlow 功能集成。

所以回答你的问题:不,你不需要将 Keras 代码转换为 tf.keras 代码。 Keras 代码使用 Keras 库,甚至可能运行在与 TensorFlow 不同的后端之上,并且将来会继续正常运行。更重要的是,不要在同一个脚本中混合使用 Keras 和 tf.keras 对象,因为这可能会产生不兼容性,例如 .

更新:Keras 将被弃用 tf.keras:https://twitter.com/fchollet/status/1174019423541157888