-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathregularization.py
More file actions
159 lines (122 loc) · 5.03 KB
/
Copy pathregularization.py
File metadata and controls
159 lines (122 loc) · 5.03 KB
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# Based on this implementations: https://github.com/szymonmaszke/torchlayers/blob/master/torchlayers/regularization.py
import abc
import torch
import torch.nn as nn
class NoiseInjection(nn.Module):
def __init__(self, p: float = 0.0, alpha: float = 0.05):
super(NoiseInjection, self).__init__()
self.p = p
self.alpha = alpha
def get_noise(self, x):
dims = tuple(i for i in range(len(x.shape)) if i != 1)
std = torch.std(x, dim=dims, keepdim=True)
noise = torch.randn(x.shape, device=x.device, dtype=x.dtype) * std
return noise
def forward(self, x):
if self.training:
mask = torch.rand(x.shape, device=x.device, dtype=x.dtype)
mask = (mask < self.p).float() * 1
x = x + self.alpha * mask * self.get_noise(x)
return x
return x
class NoiseMultiplicativeInjection(nn.Module):
def __init__(self, p: float = 0.05, alpha: float = 0.05, betta: float = 0.01):
super(NoiseMultiplicativeInjection, self).__init__()
self.p = p
self.alpha = alpha
self.betta = betta
def get_noise(self, x):
dims = tuple(i for i in range(len(x.shape)) if i != 1)
std = torch.std(x, dim=dims, keepdim=True)
noise = torch.randn(x.shape, device=x.device, dtype=x.dtype) * std
return noise
def get_m_noise(self, x):
noise = torch.randn(x.shape, device=x.device, dtype=x.dtype) * self.betta + 1
return noise
def forward(self, x):
if self.training:
mask = torch.rand(x.shape, device=x.device, dtype=x.dtype)
mask = (mask < self.p).float() * 1
mask_m = torch.rand(x.shape, device=x.device, dtype=x.dtype)
mask_m = (mask_m < self.p).float() * 1
x = x + x * mask_m * self.get_m_noise(x) + self.alpha * mask * self.get_noise(x)
return x
return x
class WeightDecay(nn.Module):
def __init__(self, module, weight_decay, name: str = None):
if weight_decay < 0.0:
raise ValueError(
"Regularization's weight_decay should be greater than 0.0, got {}".format(
weight_decay
)
)
super().__init__()
self.module = module
self.weight_decay = weight_decay
self.name = name
self.hook = self.module.register_full_backward_hook(self._weight_decay_hook)
def remove(self):
self.hook.remove()
def _weight_decay_hook(self, *_):
if self.name is None:
for param in self.module.parameters():
if param.grad is None or torch.all(param.grad == 0.0):
param.grad = self.regularize(param)
else:
for name, param in self.module.named_parameters():
if self.name in name and (
param.grad is None or torch.all(param.grad == 0.0)
):
param.grad = self.regularize(param)
def forward(self, *args, **kwargs):
return self.module(*args, **kwargs)
def extra_repr(self) -> str:
representation = "weight_decay={}".format(self.weight_decay)
if self.name is not None:
representation += ", name={}".format(self.name)
return representation
@abc.abstractmethod
def regularize(self, parameter):
pass
class L2(WeightDecay):
r"""Regularize module's parameters using L2 weight decay.
Example::
import torchlayers as tl
# Regularize only weights of Linear module
regularized_layer = tl.L2(tl.Linear(30), weight_decay=1e-5, name="weight")
.. note::
Backward hook will be registered on `module`. If you wish
to remove `L2` regularization use `remove()` method.
Parameters
----------
module : torch.nn.Module
Module whose parameters will be regularized.
weight_decay : float
Strength of regularization (has to be greater than `0.0`).
name : str, optional
Name of parameter to be regularized (if any).
Default: all parameters will be regularized (including "bias").
"""
def regularize(self, parameter):
return self.weight_decay * parameter.data
class L1(WeightDecay):
"""Regularize module's parameters using L1 weight decay.
Example::
import torchlayers as tl
# Regularize all parameters of Linear module
regularized_layer = tl.L1(tl.Linear(30), weight_decay=1e-5)
.. note::
Backward hook will be registered on `module`. If you wish
to remove `L1` regularization use `remove()` method.
Parameters
----------
module : torch.nn.Module
Module whose parameters will be regularized.
weight_decay : float
Strength of regularization (has to be greater than `0.0`).
name : str, optional
Name of parameter to be regularized (if any).
Default: all parameters will be regularized (including "bias").
"""
def regularize(self, parameter):
return self.weight_decay * torch.sign(parameter.data)