首页 > 解决方案 > 将张量流 tf.contrib.layers.layer_norm 转换为 tf2.0

问题描述

我想将以下代码从 tf1.0 更改为 tf2.0

 tf.contrib.layers.layer_norm(
      inputs=input_tensor, begin_norm_axis=-1, begin_params_axis=-1, scope=name)

此代码取自https://github.com/google-research/bert/blob/master/modeling.py 第 364 行。

请帮我。

标签: tensorflowlayer

解决方案


@rishabh-sahrawat 的回答是正确的,但你应该这样做:

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

在您链接的 BERT 案例中,您应该使用以下内容修改代码:

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)

推荐阅读