}

Tensorflow 2.0 and Keras

10/12/2021

Are your the sort of person who likes the good news first or the bad?

The good news is that Tensorflow 2.0, currently in beta, has integrated the Keras API. This means that developers looking to create machine learning and AI models in tensorflow can focus more on architecture and design and less on details of implementation.

The bad news is that Tensorflow 2.0, currently in beta, has integrated the Keras API. This means that none of the Python scripts you wrote for Tensorflow 1.x will work in 2.0. Not a single one. If you have already been using Keras as an external interface you will likely have an easier time migrating to 2.0 than people who have not, but there is still work to do.

The Backward Compatibility Cheat


It is possible to run a version 1.x script by slightly altering the import:

import tensorflow.compat.v1 as tf tf.disable_v2_behavior()


This method, of course, defeats the purpose of moving to 2.0 and will prevent you from applying best practices in Python code targeting tensorflow 2.0.

Using Keras


Come to think of it, there is a second bit of good news. Virtually all Keras code should work in tensorflow 2.0. The only difference is the import statement. We don't import keras, we import keras from tensorflow.

import tensorflow as tf

from tensorflow import keras

Modules within keras are imported in the same manner, e.g.

from tensorflow.keras.models import Sequential, Model

Come to think of it, there is also a second bit of bad news. If you are using Keras as a front-end to other deep-learning toolsets such as MXNET of PlaidML, there is no guarantee that future versions of the standalone keras module and the integrated tensorflow.keras module will be synchronized. Indeed, they will probably not be. You will likely have to be careful with your version management.

Conclusion


No one likes to re-write code, but it's the price to be paid for working in a new and fast-moving area like deep learning. It's inconceivable that development platforms can get it right the first time; indeed there are still plenty of warts and blemishes remaining to be fixed.

Keras provides the Tensorflow developer the ability to accomplish more with fewer lines of code, but does not extract a price in terms of flexibility or power for its ease of use. As of the middle of 2018, tensorflow and Keras were, respectively, the number one and two most widely adopted deep learning platforms. The integration of Keras into Tensorflow 2.0 would seem to ensure a leadership role for Keras in the deep-learning development community.

Dan Buskirk

Written by Dan Buskirk

"The pleasures of the table belong to all ages." Actually, Brillat-Savaron was talking about the dinner table, but the quote applies equally well to Dan’s other big interest, tables of data. Dan has worked with Microsoft Excel since the Dark Ages and has utilized SQL Server since Windows NT first became available to developers as a beta (it was 32 bits! wow!). Since then, Dan has helped corporations and government agencies gather, store, and analyze data and has also taught and mentored their teams using the Microsoft Business Intelligence Stack to impose order on chaos. Dan has taught Learning Tree in Learning Tree’s SQL Server & Microsoft Office curriculums for over 14 years. In addition to his professional data and analysis work, Dan is a proponent of functional programming techniques in general, especially Microsoft’s new .NET functional language F#. Dan enjoys speaking at .NET and F# user’s groups on these topics.

Chat With Us