MCPcopy
hub / github.com/Turing-Project/WriteGPT / layer_norm

Function layer_norm

LanguageNetwork/GPT2/scripts/utils.py:114–125  ·  view source on GitHub ↗

Run layer normalization on the last dimension of the tensor.

(input_tensor, name=None, epsilon=1e-5)

Source from the content-addressed store, hash-verified

112
113
114def layer_norm(input_tensor, name=None, epsilon=1e-5):
115 """Run layer normalization on the last dimension of the tensor."""
116 name2use = f'LayerNorm_{name}' if name is not None else name
117 with tf.compat.v1.variable_scope(name2use, default_name='LayerNorm'):
118 dim = input_tensor.shape[-1].value
119 gamma = tf.compat.v1.get_variable('gamma', [dim], initializer=tf.constant_initializer(1))
120 beta = tf.compat.v1.get_variable('beta', [dim], initializer=tf.constant_initializer(0))
121 mean = tf.reduce_mean(input_tensor, axis=-1, keepdims=True)
122 std = tf.reduce_mean(tf.square(input_tensor - mean), axis=-1, keepdims=True)
123 input_tensor = (input_tensor - mean) * tf.math.rsqrt(std + epsilon)
124 input_tensor = input_tensor * gamma + beta
125 return input_tensor
126
127
128def dropout(input_tensor, dropout_prob):

Callers 2

residual_mlp_layerFunction · 0.90
embedFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected