且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

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

更新时间:2023-12-02 14:35:04

您正在混淆:

  • 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, the tf.keras API is optimized to work well with other TensorFlow modules: you can pass a tf.data Dataset to the .fit() method of a tf.keras model, for instance, or convert a tf.keras model to a TensorFlow estimator with tf.keras.estimator.model_to_estimator. Currently, the tf.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