What is the difference between keras and tf.keras?

The difference between tf.keras and keras is the Tensorflow specific enhancement to the framework.

keras is an API specification that describes how a Deep Learning framework should implement certain part, related to the model definition and training. Is framework agnostic and supports different backends (Theano, Tensorflow, ...)

tf.keras is the Tensorflow specific implementation of the Keras API specification. It adds the framework the support for many Tensorflow specific features like: perfect support for tf.data.Dataset as input objects, support for eager execution, ...

In Tensorflow 2.0 tf.keras will be the default and I highly recommend to start working using tf.keras


The history of Keras Vs tf.keras is long and twisted.

Keras: Keras is a high-level (easy to use) API, built by Google AI Developer/Researcher, Francois Chollet. Written in Python and capable of running on top of backend engines like TensorFlow, CNTK, or Theano.

TensorFlow: A library, also developed by Google, for the Deep Learning developer Community, for making deep learning applications accessible and usable to public. Open Sourced and available on GitHub.

With the release of Keras v1.1.0, Tensorflow was made default backend engine. That meant: if you installed Keras on your system, you were also installing TensorFlow.

Later, with TensorFlow v1.10.0, for the first time tf.keras submodule was introduced in Tensorflow. The first step in integrating Keras within TensorFlow

With the release of Keras 2.3.0,

  • first release of Keras in sync with tf.keras
  • Last major release to support other multi-backend engines
  • And most importantly, going forward, recommend switching the code from keras to Tensorflow2.0 and tf.keras packages.

Refer this tweet from François Chollet to use tf.keras.

That means, Change Everywhere

From

from keras.models import Sequential
from keras.models import load_model

To

from tensorflow.keras.models import Sequential
from tensorflow.keras.models import load_model

And In requirements.txt,

tensorflow==2.3.0

*Disclaimer: it might give conflicts if you were using an older version of Keras. Do pip uninstall keras in that case.


At this point tensorflow has pretty much entirely adopted the keras API and for a good reason - it's simple, easy to use and easy to learn, whereas "pure" tensorflow comes with a lot of boilerplate code. And yes, you can use tf.keras without any issues, though you might have to re-work your imports in the code. For instance

from keras.layers.pooling import MaxPooling2D

Would turn into:

from tensorflow.keras.layers import MaxPooling2D