-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlayer_norm.py
67 lines (58 loc) · 1.96 KB
/
layer_norm.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
import torch
import torch.nn as nn
class LayerNorm(nn.Module):
"""
LayerNorm is a module that applies layer normalisation to the input activations.
Attributes:
-----------
shape : int
The shape of the layer to be normalised.
eps : int
A small value to prevent division by zero.
scale : nn.Parameter
The learnable scale parameter for normalisation.
offset : nn.Parameter
The learnable offset parameter for normalisation.
Methods:
--------
forward(activations: torch.Tensor) -> torch.Tensor:
Applies layer normalisation to the input activations.
"""
def __init__(self, shape: int, eps: float = 1e-5) -> None:
"""
Initialises the LayerNorm module.
Parameters:
-----------
shape : int
The shape of the layer to be normalised.
eps : float, optional
A small value to prevent division by zero (default is 1e-5).
"""
super().__init__()
self.shape = shape
self.eps = eps
self.scale = nn.Parameter(torch.ones(self.shape))
self.offset = nn.Parameter(torch.zeros(self.shape))
def forward(self, activations: torch.Tensor) -> torch.Tensor:
"""
Forward pass for the LayerNorm module.
Applies layer normalisation to the input activations.
Parameters:
-----------
activations : torch.Tensor
A tensor of input activations to be normalised.
Returns:
--------
torch.Tensor
A tensor of normalised activations.
"""
m = activations.mean()
nu = activations.std()
normalised_activations = ((activations - m) / (nu + self.eps)) * self.scale + self.offset
return normalised_activations
if __name__ == "__main__":
shape = 20
activations = torch.normal(0, 1, (shape,))
layer_norm = LayerNorm(shape)
normalised_activations = layer_norm(activations)
# (shape,)