18

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.

nbro
  • 15,395
  • 32
  • 113
  • 196
eugene
  • 39,839
  • 68
  • 255
  • 489
  • Could you be more specific? What code do you need to convert exactly? Take a look https://stackoverflow.com/questions/54913830/whats-the-difference-between-import-keras-and-import-tensorflow-keras/54914559#54914559 – Sharky Mar 15 '19 at 08:22
  • 2
    Possible duplicate of [What is the difference between Keras and tf.contrib.keras in TensorFlow 1.1+?](https://stackoverflow.com/questions/44068899/what-is-the-difference-between-keras-and-tf-contrib-keras-in-tensorflow-1-1) – P-Gn Mar 15 '19 at 09:49
  • Possible duplicate of [Difference between Keras and tf.keras: should old Keras code be changed?](https://stackoverflow.com/questions/54083955/difference-between-keras-and-tf-keras-should-old-keras-code-be-changed) – thepurpleowl Apr 04 '19 at 09:18

4 Answers4

25

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

nessuno
  • 26,493
  • 5
  • 83
  • 74
  • Kerass 2.3.0 is the last multi-backend version of Keras, development continues with tf.keras https://github.com/keras-team/keras/releases/tag/2.3.0 – monolith Mar 02 '20 at 15:16
16

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
Alexander Ejbekov
  • 5,594
  • 1
  • 26
  • 26
  • 2
    So, for most code, only change I need to make would be import statements? (well I guess there could be exceptions) – eugene Mar 15 '19 at 08:42
  • 2
    Overall yes, unless you've imported the entire library and have to go through the code and find out what you've used where(which still isn't a big deal with a modern IDE). – Alexander Ejbekov Mar 15 '19 at 08:46
9

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.

sarjit07
  • 7,511
  • 1
  • 17
  • 15
0

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.

arivero
  • 777
  • 1
  • 9
  • 30