-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtils.py
79 lines (63 loc) · 1.8 KB
/
Utils.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
# Edited by: daehuiKim
import torch.nn as nn
import copy
import torch
"""
LayerNorm class
params explanation
features => input size
eps => epsilon for numerical stability
function expl
forward => return normalized input
"""
class LayerNorm(nn.Module):
def __init__(self, features, eps = 1e-6) -> None:
super(LayerNorm, self).__init__()
self.gamma = nn.Parameter(torch.ones(features)) #for efficiency we assume it as 1
self.beta = nn.Parameter(torch.zeros(features)) #for efficiency we assume it as 0
self.eps = eps
#Normalization eps:small value
def forward(self,x):
mean = x.mean(-1, keepdim=True)
std = x.std(-1, keepdim=True)
return self.gamma * (x - mean) / (std + self.eps) + self.beta
"""
SublayerConnection class
params explanation
size => input size
dropout => dropout rate
function explanation
forward => return normalized input
"""
class SublayerConnection(nn.Module):
def __init__(self, size, dropout) -> None:
super(SublayerConnection, self).__init__()
self.norm = LayerNorm(size)
self.dropout = nn.Dropout(dropout)
def forward(self, x, sublayer):
return x + self.dropout(sublayer(self.norm(x)))
"""
params explanation
module => EncoderLayer
N => number of layers
"""
def clones(module, N):
return nn.ModuleList([copy.deepcopy(module) for _ in range(N)])
"""
return subsequent masking
"""
def subsequent_mask(size):
shape = (1,size,size)
mask = torch.triu(torch.ones(shape), diagonal=1).type(torch.uint8)
return mask == 0
class DummyOptimizer(torch.optim.Optimizer):
def __init__(self):
self.param_groups = [{"lr": 0}]
None
def step(self):
None
def zero_grad(self, set_to_none=False):
None
class DummyScheduler:
def step(self):
None