MCPcopy Create free account
hub / github.com/google-research/language / layer_norm

Function layer_norm

language/common/utils/model_utils.py:29–53  ·  view source on GitHub ↗

Implements layer norm from [Ba et al. 2016] Layer Normalization. See eqn. 4 in (https://arxiv.org/pdf/1607.06450.pdf). Args: layer_inputs (tensor): The inputs to the layer. shape [batch_size, hidden_size] hidden_size (int): Dimensionality of the hidden layer. Returns:

(layer_inputs, hidden_size)

Source from the content-addressed store, hash-verified

27
28
29def layer_norm(layer_inputs, hidden_size):
30 """Implements layer norm from [Ba et al. 2016] Layer Normalization.
31
32 See eqn. 4 in (https://arxiv.org/pdf/1607.06450.pdf).
33
34 Args:
35 layer_inputs (tensor): The inputs to the layer.
36 shape <float32>[batch_size, hidden_size]
37 hidden_size (int): Dimensionality of the hidden layer.
38
39 Returns:
40 normalized (tensor): layer_inputs, normalized over all the hidden units in
41 the layer.
42 shape <float32>[batch_size, hidden_size]
43 """
44
45 mean, var = tf.nn.moments(layer_inputs, [1], keep_dims=True)
46 with tf.variable_scope("layernorm", reuse=tf.AUTO_REUSE):
47 gain = tf.get_variable(
48 "gain", shape=[hidden_size], initializer=tf.constant_initializer(1))
49 bias = tf.get_variable(
50 "bias", shape=[hidden_size], initializer=tf.constant_initializer(0))
51
52 normalized = gain * (layer_inputs - mean) / tf.sqrt(var + _EPSILON) + bias
53 return normalized
54
55
56def hamming_loss(preds, targets, sign=False):

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…