-
Notifications
You must be signed in to change notification settings - Fork 2
/
regularizer.py
66 lines (55 loc) · 1.73 KB
/
regularizer.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
from torch import nn
import torch
class Regularizer(nn.Module):
"""Base class for sparse regularizer.
Attributes
----------
weight : float
the weight of regularizer in the loss
T : int
the weight get exponentially increased over T steps
"""
def __init__(self, weight=0.0, T=1000) -> None:
"""
Intializing the regularizer with weight and decaying steps
Parameters
----------
weight: float
the regularizer's weight in the loss
T: int
warming up steps
"""
super().__init__()
self.weight_T = weight
self.weight_t = 0
self.T = T
self.t = 0
def step(self):
"""
Perform a warming up step.
The weight starts with zero and get expoentially increased step by step until the T-th step.
"""
if self.t >= self.T:
pass
else:
self.t += 1
self.weight_t = self.weight_T * (self.t / self.T) ** 2
def forward(self, reps):
"""
reps: batch representation
"""
raise NotImplementedError("This is an abstract regularizer only.")
class FLOPs(Regularizer):
"""
Implementation of the FLOPs regularizer which is a mooth approximation for number of term overlap between a query and a document.
Paper: https://arxiv.org/abs/2004.05665
"""
def forward(self, reps):
return (torch.abs(reps).mean(dim=0) ** 2).sum() * self.weight_t
class L1(Regularizer):
"""
Implementation of the L1 regularizer
"""
def forward(self, reps):
with torch.cuda.amp.autocast(enabled=False):
return torch.abs(reps*self.weight_t).sum(dim=1).mean()