A.I, Data and Software Engineering

New TensorFlow 2.0 vs 1.X – Quick note

TensorFlow 2.0

TensorFlow 2.0 is out! Get hands-on practice at TF World, Oct 28-31.

TensorFlow Ads

Since the TF2.0 API reference lists have already been made publicly available, TF2.0 is still in RC.2 version. It is expected that the final release will be made available in the next few days (or weeks).

What’s new in TF2.0:

The obvious different – The version. In Colab, you can force using 2.0 by:

The latest version of 1.X and 2.X by the time of this article is shown below.



  • Eager execution default No more tf.Sesson()

Not long ago, I wrote a short tutorial for Graph and Session. Now, it has lost half of its value. TensorFlow 2.0 runs with eager execution (no more tf.session) by default for ease of use and smooth debugging. Consequently, sessions instantiating and running computation graphs will no longer be necessary. This simplifies many API calls and removes some boilerplate code from the codebase.



Yeah! Happy tensorflow
  • tf.function decorator

The tf.function function decorator transparently translates your Python programs into TensorFlow graphs. This process retains all the advantages of 1.x TensorFlow graph-based execution and also you get the benefits of faster execution, running on GPU or TPU, or exporting to SavedModel.

This is the significant change and paradigm shift from v1.X to v2.0.

Bye tf – Hi tf.keras

Keras has received much praise for its simple and intuitive API for defining network architectures and training them. It integrates tightly with the rest of TensorFlow so you can access TensorFlow’s features whenever you want.

Keras vs TensorFlow vs PyTorch
Keras vs TensorFlow vs PyTorch

Importantly, Keras provides several model-building APIs (Sequential, Functional, and Subclassing), so you can choose the right level of abstraction for your project. With the increasing popularity of Keras, it is not difficult to understand that.

  • tf.data

Training data is read using input pipelines which are created using tf.data. This will be the preferred way of declaring input pipelines. Pipelines using tf.placeholders and feed dicts for sessions will no longer benefit from performance improvements in subsequent tf2.0 versions.

 Without pipelining, the CPU and the GPU/TPU sit idle much of the time:
Without pipelining, the CPU and the GPU/TPU sit idle much of the time:
 With pipelining, idle time diminishes significantly:
With pipelining, idle time diminishes significantly:
  • R.I.P tf.contrib

Most of the modules in tf.contrib will depreciate in tf2.0 and will be either moved into core TensorFlow or removed altogether.

Migrate to TF2.X

It will still be possible to run tf1.X code in tf2 without any modifications. However, to take advantage of many of the improvements made in TensorFlow 2.0, you must convert it to TF2. Fortunately, the TF team does provide a conversion script that automatically converts the old tf1.XX calls to tf2 calls, if possible.

Example of converted code from 1.X to 2.X:

After conversion:

Surprise, tensor-coder!
Surprise, tensor-coder!

Happy Kerasing!

Add comment

A.I, Data and Software Engineering

PetaMinds focuses on developing the coolest topics in data science, A.I, and programming, and make them so digestible for everyone to learn and create amazing applications in a short time.

Pin It on Pinterest


You have successfully subscribed to the newsletter

There was an error while trying to send your request. Please try again.

Petaminds will use the information you provide on this form to be in touch with you and to provide updates.