/
norms.py
44 lines (37 loc) · 1.15 KB
/
norms.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
"""Collection of Ivy normalization functions."""
# local
import ivy
# Extra #
# ------#
# noinspection PyUnresolvedReferences
def layer_norm(x, normalized_idxs, epsilon=None, scale=None, offset=None, new_std=None):
"""Applies Layer Normalization over a mini-batch of inputs.
Parameters
----------
x
Input array
normalized_idxs
Indices to apply the normalization to.
epsilon
small constant to add to the denominator, use global ivy._MIN_BASE by default.
scale
Learnable gamma variables for post-multiplication, default is None.
offset
Learnable beta variables for post-addition, default is None.
new_std
The standard deviation of the new normalized values. Default is 1.
Returns
-------
ret
The layer after applying layer normalization.
"""
mean = ivy.mean(x, normalized_idxs, keepdims=True)
var = ivy.var(x, normalized_idxs, keepdims=True)
x = (-mean + x) / ivy.stable_pow(var, 0.5, epsilon)
if new_std is not None:
x = x * new_std
if scale is not None:
x = x * scale
if offset is not None:
x = x + offset
return x