-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcustom_layers.py
307 lines (251 loc) · 9.3 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
import numpy as np
from collections import OrderedDict
import torch
import warnings
import re
from torch import nn
class MetaModule(nn.Module):
"""
Base class for PyTorch meta-learning modules. These modules accept an
additional argument `params` in their `forward` method.
Notes
-----
Objects inherited from `MetaModule` are fully compatible with PyTorch
modules from `torch.nn.Module`. The argument `params` is a dictionary of
tensors, with full support of the computation graph (for differentiation).
"""
def __init__(self):
super(MetaModule, self).__init__()
self._children_modules_parameters_cache = dict()
def meta_named_parameters(self, prefix="", recurse=True):
gen = self._named_members(
lambda module: module._parameters.items()
if isinstance(module, MetaModule)
else [],
prefix=prefix,
recurse=recurse,
)
for elem in gen:
yield elem
def meta_parameters(self, recurse=True):
for _, param in self.meta_named_parameters(recurse=recurse):
yield param
def get_subdict(self, params, key=None):
if params is None:
return None
all_names = tuple(params.keys())
if (key, all_names) not in self._children_modules_parameters_cache:
if key is None:
self._children_modules_parameters_cache[(key, all_names)] = all_names
else:
key_escape = re.escape(key)
key_re = re.compile(r"^{0}\.(.+)".format(key_escape))
self._children_modules_parameters_cache[(key, all_names)] = [
key_re.sub(r"\1", k)
for k in all_names
if key_re.match(k) is not None
]
names = self._children_modules_parameters_cache[(key, all_names)]
if not names:
warnings.warn(
"Module `{0}` has no parameter corresponding to the "
"submodule named `{1}` in the dictionary `params` "
"provided as an argument to `forward()`. Using the "
"default parameters for this submodule. The list of "
"the parameters in `params`: [{2}].".format(
self.__class__.__name__, key, ", ".join(all_names)
),
stacklevel=2,
)
return None
return OrderedDict([(name, params[f"{key}.{name}"]) for name in names])
class MetaSequential(nn.Sequential, MetaModule):
__doc__ = nn.Sequential.__doc__
def forward(self, input, params=None):
for name, module in self._modules.items():
if isinstance(module, MetaModule):
input = module(input, params=self.get_subdict(params, name))
elif isinstance(module, nn.Module):
input = module(input)
else:
raise TypeError(
"The module must be either a torch module "
"(inheriting from `nn.Module`), or a `MetaModule`. "
"Got type: `{0}`".format(type(module))
)
return input
def init_weights_normal(m):
if type(m) == BatchLinear or type(m) == nn.Linear:
if hasattr(m, "weight"):
nn.init.kaiming_normal_(m.weight, a=0.0, nonlinearity="relu", mode="fan_in")
class BatchLinear(nn.Linear, MetaModule):
"""A linear meta-layer that can deal with batched weight matrices and biases, as for instance output by a
hypernetwork."""
__doc__ = nn.Linear.__doc__
def forward(self, input, params=None):
if params is None:
params = OrderedDict(self.named_parameters())
bias = params.get("bias", None)
weight = params["weight"]
output = input.matmul(
weight.permute(*[i for i in range(len(weight.shape) - 2)], -1, -2)
)
output += bias.unsqueeze(-2)
return output
class FCLayer(MetaModule):
def __init__(self, in_features, out_features, nonlinearity="relu", norm=None):
super().__init__()
self.net = [BatchLinear(in_features, out_features)]
if norm == "layernorm":
self.net.append(
nn.LayerNorm([out_features], elementwise_affine=True),
)
elif norm == "layernorm_na":
self.net.append(
nn.LayerNorm([out_features], elementwise_affine=False),
)
if nonlinearity == "relu":
self.net.append(nn.ReLU(inplace=True))
elif nonlinearity == "leaky_relu":
self.net.append(nn.LeakyReLU(0.2, inplace=True))
self.net = MetaSequential(*self.net)
self.net.apply(init_weights_normal)
def forward(self, input, params=None):
return self.net(input, params=self.get_subdict(params, "net"))
class FCBlock(MetaModule):
def __init__(
self,
hidden_ch,
num_hidden_layers,
in_features,
out_features,
outermost_linear=False,
norm=None,
activation="relu",
nonlinearity="relu",
):
super().__init__()
self.net = []
self.net.append(
FCLayer(
in_features=in_features,
out_features=hidden_ch,
nonlinearity=nonlinearity,
norm=norm,
)
)
for i in range(num_hidden_layers):
self.net.append(
FCLayer(
in_features=hidden_ch,
out_features=hidden_ch,
nonlinearity=nonlinearity,
norm=norm,
)
)
if outermost_linear:
self.net.append(
BatchLinear(in_features=hidden_ch, out_features=out_features)
)
else:
self.net.append(
FCLayer(
in_features=hidden_ch,
out_features=out_features,
nonlinearity=nonlinearity,
norm=norm,
)
)
self.net = MetaSequential(*self.net)
self.net.apply(init_weights_normal)
def forward(self, input, params=None):
return self.net(input, params=self.get_subdict(params, "net"))
class SineLayer(MetaModule):
def __init__(
self, in_features, out_features, bias=True, is_first=False, omega_0=30
):
super().__init__()
self.omega_0 = float(omega_0)
self.is_first = is_first
self.in_features = in_features
self.linear = BatchLinear(in_features, out_features, bias=bias)
self.init_weights()
def init_weights(self):
with torch.no_grad():
if self.is_first:
self.linear.weight.uniform_(-1 / self.in_features, 1 / self.in_features)
else:
self.linear.weight.uniform_(
-np.sqrt(6 / self.in_features) / self.omega_0,
np.sqrt(6 / self.in_features) / self.omega_0,
)
def forward_with_film(self, input, gamma, beta):
intermed = self.linear(input)
return torch.sin(gamma * self.omega_0 * intermed + beta)
def forward(self, input, params=None):
intermed = self.linear(input, params=self.get_subdict(params, "linear"))
return torch.sin(self.omega_0 * intermed)
class Siren(MetaModule):
def __init__(
self,
in_features,
hidden_features,
hidden_layers,
out_features,
outermost_linear=False,
first_omega_0=30,
hidden_omega_0=30.0,
special_first=True,
):
super().__init__()
self.hidden_omega_0 = hidden_omega_0
layer = SineLayer
self.net = []
self.net.append(
layer(
in_features,
hidden_features,
is_first=special_first,
omega_0=first_omega_0,
)
)
for i in range(hidden_layers):
self.net.append(
layer(
hidden_features,
hidden_features,
is_first=False,
omega_0=hidden_omega_0,
)
)
if outermost_linear:
final_linear = BatchLinear(hidden_features, out_features)
with torch.no_grad():
final_linear.weight.uniform_(
-np.sqrt(6 / hidden_features) / 30.0,
np.sqrt(6 / hidden_features) / 30.0,
)
self.net.append(final_linear)
else:
self.net.append(
layer(
hidden_features,
out_features,
is_first=False,
omega_0=hidden_omega_0,
)
)
self.net = nn.ModuleList(self.net)
def forward(self, coords, params=None):
x = coords
for i, layer in enumerate(self.net):
x = layer(x, params=self.get_subdict(params, f"net.{i}"))
return x
def forward_with_film(self, coords, film):
x = coords
for i, (layer, layer_film) in enumerate(zip(self.net, film)):
if i < len(self.net) - 1:
x = layer.forward_with_film(x, layer_film["gamma"], layer_film["beta"])
else:
x = layer.forward(x)
return x