Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between Keras and tf.keras in TensorFlow 1.1+?

Now that TensorFlow 1.1 supports the Keras API under tf.contrib.keras, which one should I use if I intend to use Keras with a TF backend?

Is the tf.contrib.keras version different in any way than a regular Keras distribution? (TF specific optimizations of internal data structures come to mind). Is there any benefit in terms of using Keras and TensorFlow Core together if I use one or the other?

Or is tf.contrib.keras simply a copy of the same codebase as Keras but under a different namespace?

like image 370
Jakub Arnold Avatar asked May 19 '17 11:05

Jakub Arnold


People also ask

What is difference between TF keras and keras?

TensorFlow is an open-sourced end-to-end platform, a library for multiple machine learning tasks, while Keras is a high-level neural network library that runs on top of TensorFlow. Both provide high-level APIs used for easily building and training models, but Keras is more user-friendly because it's built-in Python.

What is TF and keras?

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

Should I import keras or TensorFlow keras?

User should always use from tensorflow import keras which will give them the public API. import keras will directly access the keras PIP package, which is not 100% same as the public API namespace. It will probably give you keras. Model/layers.

Is TensorFlow 2.0 same as keras?

However, that's now changing — when Google announced TensorFlow 2.0 in June 2019, they declared that Keras is now the official high-level API of TensorFlow for quick and easy model design and training.


3 Answers

tf.keras (formerly tf.contrib.keras) is an implementation of keras 2 implemented exclusively with/for tensorflow. It is hosted on the tensorflow repo and has a distinct code base than the official repo (the last commit there in the tf-keras branch dates back from May 2017).

As a rule of thumb, if your code use any tensorflow-specific code, say anything in tf.data.* for providing inputs or tf.summary.* for visualization in tensorboard, it is simpler to just use tf.keras. (Some may even recommend not using the reference Keras implementation with TF because of occasional problems it has with this toolkit).

On the other hand, if you plan to actively maintain a framework-agnostic code, using keras' own package is your only choice.

If you don't care much about being framework-agnostic but don't use tensorflow-specific code, I would probably advise to go with tf.keras and start using tensorflow-specific code, esp. tf.data which is a game-changer in my opinion.

EDIT

I attended a talk by Chollet on TF2 (couldn't find a recording online) in which he basically said that support for frameworks other than TF would eventually drop and future developments of Keras would happen exclusively in tf.keras.

From what I can see, this is already happening, as Keras' commit stream is getting thin these days.

It makes a lot of sense since, as of now, the only other popular DL framework is pytorch, which is not supported by Keras. Keeping Keras code "agnostic" to tensorflow -- the only major framework it is supporting -- makes less and less sense.

So today, my answer would be to use tf.keras by default, and keep Keras for legacy projects that would be hard to migrate -- that is the future-proof choice for Keras.

like image 144
P-Gn Avatar answered Oct 17 '22 04:10

P-Gn


Keras is best understood as an API specification, not as a specific codebase. In fact, going fowards there will be two separate implementations of the Keras spec: the internal TensorFlow one, available as tf.keras, written in pure TensorFlow and deeply compatible with all TensorFlow functionality, and the external multi-backend one supporting both Theano and TensorFlow (and likely even more backends in the future).

https://blog.keras.io/introducing-keras-2.html

like image 12
Reza Roboubi Avatar answered Oct 17 '22 04:10

Reza Roboubi


Recent François Chollet tweet suggests to use tf.keras.

We recommend you switch your Keras code to tf.keras.

Both Theano and CNTK are out of development. Meanwhile, as Keras backends, they represent less than 4% of Keras usage. The other 96% of users (of which more than half are already on tf.keras) are better served with tf.keras.

Keras development will focus on tf.keras going forward.

Importantly, we will seek to start developing tf.keras in its own standalone GitHub repository at keras-team/keras in order to make it much easier for 3rd party folks to contribute.

like image 1
Krishna Avatar answered Oct 17 '22 03:10

Krishna