-
Notifications
You must be signed in to change notification settings - Fork 4
/
modules.py
186 lines (140 loc) · 6.25 KB
/
modules.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
# Adapted from code of DIF-Net and SIREN
import math
from collections import OrderedDict
import numpy as np
import torch
import torch.nn.functional as F
from torch import nn
from torchmeta.modules import MetaModule, MetaSequential
from torchmeta.modules.utils import get_subdict
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:
return nn.Linear.forward(self, input)
else:
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 Sine(nn.Module):
def __init(self):
super().__init__()
def forward(self, input):
return torch.sin(30 * input)
class FCBlock(MetaModule):
'''A fully connected neural network that also allows swapping out the weights when used with a hypernetwork.
Can be used just as a normal neural network though, as well.
'''
def __init__(self, in_features, out_features, num_hidden_layers,
hidden_features, outermost_linear=False, nonlinearity='relu',
weight_init=None, use_dropout=True):
super().__init__()
self.first_layer_init = None
# Dictionary that maps nonlinearity name to the respective function, initialization, and, if applicable,
# special first-layer initialization scheme
nls_and_inits = {'sine': (Sine(), sine_init, first_layer_sine_init, last_layer_sine_init),
'relu': (nn.ReLU(inplace=True), init_weights_normal, None, None),
'sigmoid': (nn.Sigmoid(), init_weights_xavier, None, None),
'tanh': (nn.Tanh(), init_weights_xavier, None, None),
'selu': (nn.SELU(inplace=True), init_weights_selu, None, None),
'softplus': (nn.Softplus(), init_weights_normal, None, None),
'elu': (nn.ELU(inplace=True), init_weights_elu, None, None)}
nl, nl_weight_init, first_layer_init, last_layer_init = nls_and_inits[
nonlinearity]
if weight_init is not None: # Overwrite weight init if passed
self.weight_init = weight_init
else:
self.weight_init = nl_weight_init
self.net = []
self.net.append(MetaSequential(
BatchLinear(in_features, hidden_features), nl
))
for i in range(num_hidden_layers):
self.net.append(MetaSequential(
BatchLinear(hidden_features, hidden_features), nl
))
if use_dropout:
self.net.append(nn.Dropout(0.2))
if outermost_linear:
self.net.append(MetaSequential(
BatchLinear(hidden_features, out_features)))
else:
self.net.append(MetaSequential(
BatchLinear(hidden_features, out_features), nl
))
self.net = MetaSequential(*self.net)
if self.weight_init is not None:
self.net.apply(self.weight_init)
# Apply special initialization to first layer, if applicable.
if first_layer_init is not None:
self.net[0].apply(first_layer_init)
if last_layer_init is not None:
self.net[-1].apply(last_layer_init)
def forward(self, coords, params=None, **kwargs):
if params is not None:
params = get_subdict(params, 'net')
output = self.net(coords, params=params)
return output
class SingleBVPNet(MetaModule):
'''A canonical representation network for a BVP.'''
def __init__(self, coord_type, out_features=1, act_type='sine',
in_features=2, mode='mlp', hidden_features=256,
num_hidden_layers=3, use_dropout=True, **kwargs):
super().__init__()
self.mode = mode
self.net = FCBlock(
in_features=in_features, out_features=out_features,
num_hidden_layers=num_hidden_layers,
hidden_features=hidden_features, outermost_linear=True,
nonlinearity=act_type, use_dropout=use_dropout)
self.coord_type = coord_type
def forward(self, model_input, params=None):
# Enables us to compute gradients w.r.t. coordinates
coords_org = model_input[self.coord_type].requires_grad_(True)
coords = coords_org
# various input processing methods for different applications
output = self.net(coords_org, get_subdict(params, 'net'))
return {'model_in': coords_org, 'model_out': output}
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')
def init_weights_selu(m):
if type(m) == BatchLinear or type(m) == nn.Linear:
if hasattr(m, 'weight'):
num_input = m.weight.size(-1)
nn.init.normal_(m.weight, std=1 / math.sqrt(num_input))
def init_weights_elu(m):
if type(m) == BatchLinear or type(m) == nn.Linear:
if hasattr(m, 'weight'):
num_input = m.weight.size(-1)
nn.init.normal_(m.weight, std=math.sqrt(
1.5505188080679277) / math.sqrt(num_input))
def init_weights_xavier(m):
if type(m) == BatchLinear or type(m) == nn.Linear:
if hasattr(m, 'weight'):
nn.init.xavier_normal_(m.weight)
def sine_init(m):
with torch.no_grad():
if hasattr(m, 'weight'):
num_input = m.weight.size(-1)
m.weight.uniform_(-np.sqrt(6 / num_input) / 30,
np.sqrt(6 / num_input) / 30)
def first_layer_sine_init(m):
with torch.no_grad():
if hasattr(m, 'weight'):
num_input = m.weight.size(-1)
m.weight.uniform_(-1 / num_input, 1 / num_input)
def last_layer_sine_init(m):
with torch.no_grad():
if hasattr(m, 'weight'):
num_input = m.weight.size(-1)
nn.init.zeros_(m.weight)
nn.init.zeros_(m.bias)