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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)