1 from __future__
import absolute_import
2 from __future__
import division
3 from __future__
import print_function
4 from __future__
import unicode_literals
17 name=
'layer_normalization',
22 use_layer_norm_op=
True,
25 super(LayerNormalization, self).__init__(
26 model, name, input_record, **kwargs)
29 "Incorrect input type: {}".format(input_record))
35 "This layer supports only >= 2D tesnors")
45 initializer=(
'ConstantFill', {
'value': 1.0}),
46 optimizer=scale_optim)
49 initializer=(
'ConstantFill', {
'value': 0.0}),
57 "When using alternative implementation, " 58 "input data can only be 2D" 60 self.
epsilon = model.maybe_add_global_constant(
61 "%s_epsilon" % self.
name, float(epsilon)
64 def add_ops_with_layer_norm_op(self, net):
65 input_blob = self.input_record.field_blobs()
66 ln_output = self.output_schema.field_blobs()
68 output_blobs = [net.NextScopedBlob(
'ln_output'), net.NextScopedBlob(
'ln_mean'),
69 net.NextScopedBlob(
'ln_stdev')]
71 normalized, mean, stdev = net.LayerNorm(input_blob,
77 [normalized, self.
scale],
78 [net.NextScopedBlob(
'ln_scaled')],
90 def add_ops_without_layer_norm_op(self, net):
94 ln_output = net.NextScopedBlob(
"ln_output")
95 ln_mean = net.NextScopedBlob(
"ln_mean")
96 ln_stdev = net.NextScopedBlob(
"ln_stdev")
97 ln_mean_arr = net.NextScopedBlob(
"ln_mean_arr")
98 net.ReduceBackMean(self.input_record.field_blobs(), [ln_mean_arr])
99 net.ExpandDims([ln_mean_arr], [ln_mean], dims=[1])
100 ln_centered = net.NextScopedBlob(
"ln_centered")
101 net.Sub(self.input_record.field_blobs() + [ln_mean], [ln_centered])
102 ln_sqr = net.NextScopedBlob(
"ln_sqr")
103 net.Sqr([ln_centered], [ln_sqr])
104 ln_sqr_mean = net.NextScopedBlob(
"ln_sqr_mean")
105 net.ReduceBackMean([ln_sqr], [ln_sqr_mean])
106 ln_var = net.NextScopedBlob(
"ln_var")
107 net.Add([ln_sqr_mean, self.
epsilon], ln_var)
108 ln_std_arr = net.NextScopedBlob(
"ln_std_arr")
109 net.Pow([ln_var], [ln_std_arr], exponent=0.5)
110 net.ExpandDims([ln_std_arr], [ln_stdev], dims=[1])
111 net.Div([ln_centered, ln_stdev], [ln_output])
112 ln_scaled = net.NextScopedBlob(
"ln_scaled")
113 net.Mul([ln_output, self.
scale], [ln_scaled])
114 net.Add([ln_scaled, self.
bias], self.output_schema.field_blobs())
116 def add_ops(self, net):
def add_ops_with_layer_norm_op(self, net)
def get_next_blob_reference(self, name)
def create_param(self, param_name, shape, initializer, optimizer, ps_param=None, regularizer=None)
def add_ops_without_layer_norm_op(self, net)