Step Guide to Run TensorFlow 1.x Version Model or Source Code in TensorFlow 2.0 – TensorFlow Tutorial

By | June 2, 2022

We have build some tensorflow models using tensorflow 1.x version, such as tensorflow 1.10. However, if you plan to run these model in tensorflow 2.0 version. You may have to make some changes. In this tutorial, we will tell you how to do.

Step 1. Use tensorflow.compat.v1

In python, we often import tensorflow package as follows:

import tensorflow as tf

However, if you plan to run tensorflow 1.0 source code in 2.0 version, you should import tensorflow as follows:

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

Then, we can run tensorflow 1.0 version.

Step 2. Replace tensorflow.contrib packages

However, tensorflow.contrib packages are removed in tensorflow 2.0, if your codes have used these apis, you have to modify them using tf.keras.

For example:

def layer_norm(input_tensor, name=None):
  """Run layer normalization on the last dimension of the tensor."""
  return tf.contrib.layers.layer_norm(
      inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name)

This code is from bert model, however, it can not be run in tensorflow 2.0 environment. tf.contrib.layers.layer_norm() is removed in 2.0 version.

In order to fix this error, we can use tf.keras.

def layer_norm(input_tensor, name=None):
  """Run layer normalization on the last dimension of the tensor."""
  layer_norma = tf.keras.layers.LayerNormalization(axis=-1, name = name)
  return layer_norma(input_tensor)

Then, we can find it works.