8

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

With the current version of TensorFlow, do we need to change every Keras code as?

from tensorflow.keras import models
nbro
  • 15,395
  • 32
  • 113
  • 196
jason
  • 1,998
  • 3
  • 22
  • 42
  • 1
    According to [the docs](https://www.tensorflow.org/guide/keras), they adhere to the Keras API specification, so there seems to be no need to change code. Just make sure the versions are aligned, i.e. `pip install keras==`. Though that might require code updates, if the code in question uses deprecated API specifications. – a_guest Jan 08 '19 at 02:36
  • 1
    Possible duplicate of [What is the difference between Keras and tf.keras in TensorFlow 1.1+?](https://stackoverflow.com/questions/44068899/what-is-the-difference-between-keras-and-tf-keras-in-tensorflow-1-1) – thepurpleowl Apr 04 '19 at 09:27

1 Answers1

5

You are mixing things up:

  • 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.

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.

Update: Keras will be abandoned in favor of tf.keras: https://twitter.com/fchollet/status/1174019423541157888

sdcbr
  • 7,021
  • 3
  • 27
  • 44