eugene
eugene

Reputation: 41755

What is the difference between keras and tf.keras?

I'm learning TensorFlow and Keras. I'd like to try https://www.amazon.com/Deep-Learning-Python-Francois-Chollet/dp/1617294438/, and it seems to be written in Keras.

Would it be fairly straightforward to convert code to tf.keras?

I'm not more interested in the portability of the code, rather than the true difference between the two.

Upvotes: 22

Views: 24392

Answers (5)

KrzysiekDD
KrzysiekDD

Reputation: 55

As of 2023, keras 3.0.0 supports other types of backends, specifically torch and jax. It most likely would be a good idea to write tensorflow "agnostic" keras code in the future, since in a real world scenario there is some boilerplate data handling usually mixed with model creation and decoupling from tf might be useful.

Upvotes: 1

arivero
arivero

Reputation: 965

I run the following in my install:

>>> from tensorflow.python.keras import __version__ as tf_p_k_version
>>> from tensorflow.keras import __version__ as tf_k_version
>>> from keras import __version__ as k_version
>>> print(tf_p_k_version, tf_k_version, k_version)
2.6.0 2.11.0 2.11.0

Also I grepped for the docstring of the Layer class, and the one for 2.11 is not installed in tensorflow but in keras package.

My guess is that tensorflow.keras actually imports keras if present, and possibly defaults to tensorflow.python.keras if it is not.

Upvotes: 0

sarjit07
sarjit07

Reputation: 10769

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.

Upvotes: 17

nessuno
nessuno

Reputation: 27070

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

Upvotes: 27

Alexander Ejbekov
Alexander Ejbekov

Reputation: 5960

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

Upvotes: 18

Related Questions