Converting tensorflow tf.contrib.layers.layer_norm to tf2.0

Viewed 5094
2 Answers

@rishabh-sahrawat's answer is right, but you should do something like this:

layer_norma = tf.keras.layers.LayerNormalization(axis = -1)
layer_norma(input_tensor)

In the BERT case you linked, you should modify the code with something like this:

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)
  return layer_norma(input_tensor)

This is the equivalent way to do this in TF2.0

tf.keras.layers.LayerNormalization(input_tensor, axis = -1)
Related