我应该使用独立的Keras库还是tf.keras?
随着Keras成为TensorFlow的API,Keras代码有很多旧版本,例如
As Keras becomes an API for TensorFlow, there are lots of old versions of Keras code, such as https://github.com/keiserlab/keras-neural-graph-fingerprint/blob/master/examples.py
from keras import models
在当前版本的TensorFlow中,我们是否需要将每个Keras代码更改为?
With the current version of TensorFlow, do we need to change every Keras code as?
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 (https://keras.io/) is a library independent from TensorFlow, which specifies a high-level API for building and training neural networks and is capable of using one of multiple backends (among which, TensorFlow) for low-level tensor computation.
-
tf.keras
(https://www.tensorflow.org/guide/keras) implements the Keras API specification within TensorFlow. In addition, thetf.keras
API is optimized to work well with other TensorFlow modules: you can pass atf.data
Dataset to the.fit()
method of atf.keras
model, for instance, or convert atf.keras
model to a TensorFlow estimator withtf.keras.estimator.model_to_estimator
. Currently, thetf.keras
API is the high-level API to look for when building models within TensorFlow, and the integration with other TensorFlow features will continue in the future.
因此回答您的问题:不,您不需要将Keras代码转换为tf.keras代码. Keras代码使用Keras库,甚至可能在与TensorFlow不同的后端上运行,并且将来会继续正常运行.更重要的是,不仅要在同一脚本中混合Keras和tf.keras
对象,还很重要,因为这可能会产生不兼容性,例如,您可以看到
So to answer your question: no, you don't need to convert Keras code to tf.keras code. Keras code uses the Keras library, potentially even runs on top of a different backend than TensorFlow, and will continue to work just fine in the future. Even more, it's important to not just mix up Keras and tf.keras
objects within the same script, since this might produce incompatabilities, as you can see for example in this question.
更新:将废弃Keras,转而使用tf.keras: https ://twitter.com/fchollet/status/1174019423541157888
Update: Keras will be abandoned in favor of tf.keras: https://twitter.com/fchollet/status/1174019423541157888