-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom_layers.py
198 lines (154 loc) · 6.95 KB
/
custom_layers.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
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import numpy as np
import torch
import torch.nn as nn
class DAIN_LSTM_Layer(nn.Module):
def __init__(self, mode='full', mean_lr=0.00001, gate_lr=0.001, scale_lr=0.00001, input_dim=144):
super(DAIN_LSTM_Layer, self).__init__()
self.first_run = True
self.input_dim = input_dim
self.mode = mode
self.mean_lr = mean_lr
self.gate_lr = gate_lr
self.scale_lr = scale_lr
# Parameters for adaptive average
self.mean_layer = nn.Linear(input_dim, input_dim, bias=False)
self.mean_layer.weight.data = torch.FloatTensor(data=np.eye(input_dim, input_dim))
# Parameters for adaptive std
self.scaling_layer = nn.Linear(input_dim, input_dim, bias=False)
self.scaling_layer.weight.data = torch.FloatTensor(data=np.eye(input_dim, input_dim))
# Parameters for adaptive scaling
self.gating_layer = nn.Linear(input_dim, input_dim)
self.eps = 1e-8
def forward(self, x):
# Expecting (n_samples, dim, n_feature_vectors)
if self.first_run:
with torch.no_grad():
self.scaling_layer.weight.data = torch.eye(self.input_dim)
self.mean_layer.weight.data = torch.eye(self.input_dim)
self.first_run = False
# Nothing to normalize
if self.mode == None:
pass
# Do simple average normalization
elif self.mode == 'avg':
avg = torch.mean(x, 2)
avg = avg.reshape(avg.size(0), avg.size(1), 1)
x = x - avg
# Perform only the first step (adaptive averaging)
elif self.mode == 'adaptive_avg':
avg = torch.mean(x, 2)
adaptive_avg = self.mean_layer(avg)
adaptive_avg = adaptive_avg.reshape(adaptive_avg.size(0), adaptive_avg.size(1), 1)
x = x - adaptive_avg
# Perform the first + second step (adaptive averaging + adaptive scaling )
elif self.mode == 'adaptive_scale':
# Step 1:
avg = torch.mean(x, 2)
adaptive_avg = self.mean_layer(avg)
adaptive_avg = adaptive_avg.reshape(adaptive_avg.size(0), adaptive_avg.size(1), 1)
x = x - adaptive_avg
# Step 2:
std = torch.mean(x ** 2, 2)
std = torch.sqrt(std + self.eps)
adaptive_std = self.scaling_layer(std)
adaptive_std[adaptive_std <= self.eps] = 1
adaptive_std = adaptive_std.reshape(adaptive_std.size(0), adaptive_std.size(1), 1)
x = x / (adaptive_std)
elif self.mode == 'full':
# Step 1:
avg = torch.mean(x, 1)
adaptive_avg = self.mean_layer(avg)
adaptive_avg = adaptive_avg.reshape(adaptive_avg.size(0), 1, adaptive_avg.size(1))
x = x - adaptive_avg
# # Step 2:
std = torch.mean(x ** 2, 1)
std = torch.sqrt(std + self.eps)
adaptive_std = self.scaling_layer(std)
adaptive_std[adaptive_std <= self.eps] = 1
adaptive_std = adaptive_std.reshape(adaptive_std.size(0), 1, adaptive_std.size(1))
x = x / adaptive_std
# Step 3:
avg = torch.mean(x, 1)
gate = torch.sigmoid(self.gating_layer(avg))
gate = gate.reshape(gate.size(0), 1, gate.size(1))
x = x * gate
else:
assert False
return x
# Deep Adaptive Input Normalization
class DAIN_Layer(nn.Module):
def __init__(self, mode='full', mean_lr=0.00001, gate_lr=0.001, scale_lr=0.00001, input_dim=144):
super(DAIN_Layer, self).__init__()
self.first_run = True
self.mode = mode
self.input_dim = input_dim
self.mean_lr = mean_lr
self.gate_lr = gate_lr
self.scale_lr = scale_lr
# self.mask = torch.eye(input_dim)
# Parameters for adaptive average
self.mean_layer = nn.Linear(input_dim, input_dim, bias=False)
# self.mean_layer = prune.custom_from_mask(self.mean_layer, name='weight', mask=self.mask)
# Parameters for adaptive std
self.scaling_layer = nn.Linear(input_dim, input_dim, bias=False)
# self.scaling_layer = prune.custom_from_mask(self.scaling_layer, name='weight', mask=self.mask)
# Parameters for adaptive scaling
self.gating_layer = nn.Linear(input_dim, input_dim)
self.eps = 1e-8
def forward(self, x):
if self.first_run:
with torch.no_grad():
self.scaling_layer.weight.data = torch.eye(self.input_dim)
self.mean_layer.weight.data = torch.eye(self.input_dim)
self.first_run = False
# Expecting (n_samples, dim, n_feature_vectors)
# Nothing to normalize
if self.mode is None:
pass
# Do simple average normalization
elif self.mode == 'avg':
avg = torch.mean(x, 2)
avg = avg.resize(avg.size(0), avg.size(1), 1, avg.size(2))
x = x - avg
# Perform only the first step (adaptive averaging)
elif self.mode == 'adaptive_avg':
x = torch.log(x)
avg = torch.mean(x, 2)
adaptive_avg = self.mean_layer(avg)
adaptive_avg = adaptive_avg.reshape(adaptive_avg.size(0), adaptive_avg.size(1), 1, adaptive_avg.size(2))
x = x - adaptive_avg
# Perform the first + second step (adaptive averaging + adaptive scaling )
elif self.mode == 'adaptive_scale':
# Step 1:
avg = torch.mean(x, 2)
adaptive_avg = self.mean_layer(avg)
adaptive_avg = adaptive_avg.reshape(adaptive_avg.size(0), adaptive_avg.size(1), 1, adaptive_avg.size(2))
x = x - adaptive_avg
# Step 2:
std = torch.mean(x ** 2, 2)
std = torch.sqrt(std + self.eps)
adaptive_std = self.scaling_layer(std)
adaptive_std[adaptive_std <= self.eps] = 1
adaptive_std = adaptive_std.reshape(adaptive_std.size(0), adaptive_std.size(1), 1, adaptive_std.size(2))
x = (x / adaptive_std)
elif self.mode == 'full':
# Step 1:
avg = torch.mean(x, 2)
adaptive_avg = self.mean_layer(avg)
adaptive_avg = adaptive_avg.reshape(adaptive_avg.size(0), adaptive_avg.size(1), 1, adaptive_avg.size(2))
x = x - adaptive_avg
# # Step 2:
std = torch.mean(x ** 2, 2)
std = torch.sqrt(std + self.eps)
adaptive_std = self.scaling_layer(std)
adaptive_std[adaptive_std <= self.eps] = 1
adaptive_std = adaptive_std.reshape(adaptive_std.size(0), adaptive_std.size(1), 1, adaptive_std.size(2))
x = x / adaptive_std
# Step 3:
avg = torch.mean(x, 2)
gate = torch.sigmoid(self.gating_layer(avg))
gate = gate.reshape(gate.size(0), gate.size(1), 1, gate.size(2))
x = x * gate
else:
assert False
return x