-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmodules.py
245 lines (194 loc) · 9.05 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
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
# MIT License
# Copyright (c) 2018 Ethan Fetaya, Thomas Kipf
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.autograd import Variable
class MLP(nn.Module):
"""Two-layer fully-connected ELU net with batch norm."""
def __init__(self, n_in, n_hid, n_out, do_prob=0.):
super(MLP, self).__init__()
self.fc1 = nn.Linear(n_in, n_hid)
self.fc2 = nn.Linear(n_hid, n_out)
self.bn = nn.BatchNorm1d(n_out)
self.dropout_prob = do_prob
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal(m.weight.data)
m.bias.data.fill_(0.1)
elif isinstance(m, nn.BatchNorm1d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def batch_norm(self, inputs):
x = inputs.view(inputs.size(0) * inputs.size(1), -1)
x = self.bn(x)
return x.view(inputs.size(0), inputs.size(1), -1)
def forward(self, inputs):
# Input shape: [num_sims, num_things, num_features]
x = F.elu(self.fc1(inputs))
x = F.dropout(x, self.dropout_prob, training=self.training)
x = F.elu(self.fc2(x))
return self.batch_norm(x)
class MLPEncoder(nn.Module):
def __init__(self, n_in, n_hid, n_out, do_prob=0., factor=True,
use_nvil=False, num_edges=None, n=None, num_timesteps=None,
num_dims=None):
super(MLPEncoder, self).__init__()
self.factor = factor
self.mlp1 = MLP(n_in, n_hid, n_hid, do_prob)
self.mlp2 = MLP(n_hid * 2, n_hid, n_hid, do_prob)
self.mlp3 = MLP(n_hid, n_hid, n_hid, do_prob)
if self.factor:
self.mlp4 = MLP(n_hid * 3, n_hid, n_hid, do_prob)
print("Using factor graph MLP encoder.")
else:
self.mlp4 = MLP(n_hid * 2, n_hid, n_hid, do_prob)
print("Using MLP encoder.")
self.fc_out = nn.Linear(n_hid, n_out)
self.use_nvil = use_nvil
if use_nvil:
self.baseline = nn.Sequential(
nn.Flatten(),
nn.Linear(num_edges * n_hid, n_hid),
nn.ReLU(),
nn.Linear(n_hid, 1)
)
self.init_weights()
def init_weights(self):
for m in self.modules():
if isinstance(m, nn.Linear):
nn.init.xavier_normal(m.weight.data)
m.bias.data.fill_(0.1)
def edge2node(self, x, rel_rec, rel_send):
# NOTE: Assumes that we have the same graph across all samples.
incoming = torch.matmul(rel_rec.t(), x)
return incoming / incoming.size(1)
def node2edge(self, x, rel_rec, rel_send):
# NOTE: Assumes that we have the same graph across all samples.
receivers = torch.matmul(rel_rec, x)
senders = torch.matmul(rel_send, x)
edges = torch.cat([senders, receivers], dim=2)
return edges
def forward(self, inputs, rel_rec, rel_send):
# Input shape: [num_sims, num_vertices, num_timesteps, num_dims]
x = inputs.view(inputs.size(0), inputs.size(1), -1)
# New shape: [num_sims, num_vertices, num_timesteps*num_dims]
x = self.mlp1(x) # 2-layer ELU net per node
x = self.node2edge(x, rel_rec, rel_send)
x = self.mlp2(x)
x_skip = x
if self.factor:
x = self.edge2node(x, rel_rec, rel_send)
x = self.mlp3(x)
x = self.node2edge(x, rel_rec, rel_send)
x = torch.cat((x, x_skip), dim=2) # Skip connection
x = self.mlp4(x)
else:
x = self.mlp3(x)
x = torch.cat((x, x_skip), dim=2) # Skip connection
x = self.mlp4(x)
if self.use_nvil:
out = self.fc_out(x)
return out, self.baseline(x).squeeze(1)
else:
return self.fc_out(x), None
class MLPDecoder(nn.Module):
"""MLP decoder module."""
def __init__(self, n_in_node, edge_types, msg_hid, msg_out, n_hid,
do_prob=0., skip_first=False, num_rounds=1):
super(MLPDecoder, self).__init__()
self.msg_fc1 = nn.ModuleList(
[nn.Linear(2 * n_in_node, msg_hid) for _ in range(edge_types)])
self.msg_fc2 = nn.ModuleList(
[nn.Linear(msg_hid, msg_out) for _ in range(edge_types)])
self.msg_out_shape = msg_out
self.skip_first_edge_type = skip_first
self.num_rounds = num_rounds
self.out_fc1 = nn.Linear(n_in_node + msg_out, n_hid)
self.out_fc2 = nn.Linear(n_hid, n_hid)
self.out_fc3 = nn.Linear(n_hid, n_in_node)
print("Using learned interaction net decoder.")
self.dropout_prob = do_prob
def single_step_forward(self, single_timestep_inputs, rel_rec, rel_send,
single_timestep_rel_type):
# single_timestep_inputs has shape
# [batch_size, num_timesteps, num_vertices, num_dims]
# single_timestep_rel_type has shape:
# [batch_size, num_timesteps, num_vertices*(num_vertices-1), num_edge_types]
# Node2edge
receivers = torch.matmul(rel_rec, single_timestep_inputs)
senders = torch.matmul(rel_send, single_timestep_inputs)
pre_msg = torch.cat([senders, receivers], dim=-1)
all_msgs = Variable(torch.zeros(pre_msg.size(0), pre_msg.size(1),
pre_msg.size(2), self.msg_out_shape))
if single_timestep_inputs.is_cuda:
all_msgs = all_msgs.cuda()
if self.skip_first_edge_type:
start_idx = 1
else:
start_idx = 0
# Run separate MLP for every edge type
# NOTE: To exlude one edge type, simply offset range by 1
for i in range(start_idx, len(self.msg_fc2)):
msg = F.relu(self.msg_fc1[i](pre_msg))
msg = F.dropout(msg, p=self.dropout_prob)
msg = F.relu(self.msg_fc2[i](msg))
msg = msg * single_timestep_rel_type[:, :, :, i:i + 1]
all_msgs += msg
# Aggregate all msgs to receiver
agg_msgs = all_msgs.transpose(-2, -1).matmul(rel_rec).transpose(-2, -1)
agg_msgs = agg_msgs.contiguous()
# Skip connection
aug_inputs = torch.cat([single_timestep_inputs, agg_msgs], dim=-1)
# Output MLP
pred = F.dropout(F.relu(self.out_fc1(aug_inputs)), p=self.dropout_prob)
pred = F.dropout(F.relu(self.out_fc2(pred)), p=self.dropout_prob)
pred = self.out_fc3(pred)
# Predict position/velocity difference
return pred + single_timestep_inputs
def forward(self, inputs, rel_type, rel_rec, rel_send, pred_steps=1):
# NOTE: Assumes that we have the same graph across all samples.
inputs = inputs.transpose(1, 2).contiguous()
sizes = [rel_type.size(0), inputs.size(1), rel_type.size(1),
rel_type.size(2)]
rel_type = rel_type.unsqueeze(1).expand(sizes)
time_steps = inputs.size(1)
assert (pred_steps <= time_steps)
preds = []
# Only take n-th timesteps as starting points (n: pred_steps)
last_pred = inputs[:, 0::pred_steps, :, :]
curr_rel_type = rel_type[:, 0::pred_steps, :, :]
# NOTE: Assumes rel_type is constant (i.e. same across all time steps).
# Run n prediction steps
for step in range(0, pred_steps):
for _ in range(self.num_rounds):
last_pred = self.single_step_forward(last_pred, rel_rec, rel_send,
curr_rel_type)
preds.append(last_pred)
sizes = [preds[0].size(0), preds[0].size(1) * pred_steps,
preds[0].size(2), preds[0].size(3)]
output = Variable(torch.zeros(sizes))
if inputs.is_cuda:
output = output.cuda()
# Re-assemble correct timeline
for i in range(len(preds)):
output[:, i::pred_steps, :, :] = preds[i]
pred_all = output[:, :(inputs.size(1) - 1), :, :]
return pred_all.transpose(1, 2).contiguous()