-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
layer_normalization.py
84 lines (64 loc) · 2.91 KB
/
layer_normalization.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
import functools
import operator
from chainer.functions.normalization import layer_normalization
from chainer import link
from chainer import variable
class LayerNormalization(link.Link):
"""Layer normalization layer on outputs of linear functions.
.. warning::
This feature is experimental. The interface can change in the future.
This link implements a "layer normalization" layer
which normalizes the input units by statistics
that are computed along the second axis,
scales and shifts them.
Parameter initialization will be deferred until
the first forward data pass at which time the size will be determined.
Args:
size (int): Size of input units. If ``None``, parameter initialization
will be deferred until the first forward data pass at which time
the size will be determined.
eps (float): Epsilon value for numerical stability of normalization.
initial_gamma (~chainer.Initializer): Initializer for scaling vector.
If ``None``, then the vector is filled by 1.
If a scalar, the vector is filled by it.
If ``numpy.ndarray``, the vector is set by it.
initial_beta (~chainer.Initializer): Initializer for shifting vector.
If ``None``, then the vector is filled by 0.
If a scalar, the vector is filled by it.
If ``numpy.ndarray``, the vector is set by it.
Attributes:
gamma (~chainer.Parameter): Scaling parameter.
beta (~chainer.Parameter): Shifting parameter.
eps (float): Epsilon value for numerical stability.
See: `Layer Normalization <https://arxiv.org/abs/1607.06450>`_
"""
def __init__(self, size=None, eps=1e-6, initial_gamma=None,
initial_beta=None):
super(LayerNormalization, self).__init__()
if initial_gamma is None:
initial_gamma = 1
if initial_beta is None:
initial_beta = 0
with self.init_scope():
self.gamma = variable.Parameter(initial_gamma)
self.beta = variable.Parameter(initial_beta)
self.eps = eps
if size is not None:
self._initialize_params(size)
def _initialize_params(self, size):
self.gamma.initialize(size)
self.beta.initialize(size)
def forward(self, x):
"""Apply layer normalization to given input.
Args:
x (~chainer.Variable): Batch vectors.
Shape of this value must be `(batch_size, unit_size)`,
e.g., the output of :func:`~chainer.functions.linear`.
Returns:
~chainer.Variable: Output of the layer normalization.
"""
if self.gamma.array is None:
in_size = functools.reduce(operator.mul, x.shape[1:], 1)
self._initialize_params(in_size)
return layer_normalization.layer_normalization(
x, self.gamma, self.beta, self.eps)