-
Notifications
You must be signed in to change notification settings - Fork 12
/
model.py
302 lines (247 loc) · 10.5 KB
/
model.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
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch_geometric.nn import GCNConv, GATConv, GINConv
from torch.autograd import Variable
import numpy as np
from base_model import *
from inits import glorot, zeros
import pdb
# adopted from: https://github.com/xjtushujun/meta-weight-net
def to_var(x, requires_grad=True):
if torch.cuda.is_available():
x = x.cuda()
return Variable(x, requires_grad=requires_grad)
class MetaModule(nn.Module):
# adopted from: Adrien Ecoffet https://github.com/AdrienLE
def params(self):
for name, param in self.named_params(self):
yield param
def named_leaves(self):
return []
def named_submodules(self):
return []
def named_params(self, curr_module=None, memo=None, prefix=''):
if memo is None:
memo = set()
if hasattr(curr_module, 'named_leaves'):
for name, p in curr_module.named_leaves():
if p is not None and p not in memo:
memo.add(p)
yield prefix + ('.' if prefix else '') + name, p
else:
for name, p in curr_module._parameters.items():
if p is not None and p not in memo:
memo.add(p)
yield prefix + ('.' if prefix else '') + name, p
for mname, module in curr_module.named_children():
submodule_prefix = prefix + ('.' if prefix else '') + mname
for name, p in self.named_params(module, memo, submodule_prefix):
yield name, p
def update_params(self, lr_inner, first_order=False, source_params=None, detach=False):
if source_params is not None:
for tgt, src in zip(self.named_params(self), source_params):
name_t, param_t = tgt
grad = src
if first_order:
grad = to_var(grad.detach().data)
tmp = param_t - lr_inner * grad
self.set_param(self, name_t, tmp)
else:
for name, param in self.named_params(self):
if not detach:
grad = param.grad
if first_order:
grad = to_var(grad.detach().data)
tmp = param - lr_inner * grad
self.set_param(self, name, tmp)
else:
param = param.detach_() # https://blog.csdn.net/qq_39709535/article/details/81866686
self.set_param(self, name, param)
def set_param(self, curr_mod, name, param):
if '.' in name:
n = name.split('.')
module_name = n[0]
rest = '.'.join(n[1:])
for name, mod in curr_mod.named_children():
if module_name == name:
self.set_param(mod, rest, param)
break
else:
setattr(curr_mod, name, param)
def detach_params(self):
for name, param in self.named_params(self):
self.set_param(self, name, param.detach())
def copy(self, other, same_var=False):
for name, param in other.named_params():
if not same_var:
param = to_var(param.data.clone(), requires_grad=True)
self.set_param(name, param)
class MetaEmb(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Embedding(*args, **kwargs)
nn.init.xavier_normal_(ignore.weight)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
def forward(self, x):
return F.embedding(x, self.weight)
def named_leaves(self):
return [('weight', self.weight)]
class MetaGCN(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = GCNConv(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
self.gcn = [GCN(self.weight.shape[0], self.weight.shape[1], self.weight, self.bias)]
def forward(self, n_id, edge_index):
return self.gcn[0](n_id, edge_index)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaGAT(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = GATConv(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
self.register_buffer('att', to_var(ignore.att.data, requires_grad=True))
self.gat= [GAT(ignore.weight.shape[0], ignore.weight.shape[1], self.weight, self.att, self.bias)]
def forward(self, n_id, edge_index):
return self.gat[0](n_id, edge_index)
def named_leaves(self):
return [('weight', self.weight), ('att', self.att), ('bias', self.bias)]
class MetaGIN(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Linear(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
ignore2 = nn.Linear(*args, **kwargs)
self.register_buffer('weight2', to_var(ignore2.weight.data, requires_grad=True))
self.register_buffer('bias2', to_var(ignore2.bias.data, requires_grad=True))
self.gin = [GIN(self.weight, self.bias, self.weight2, self.bias2)]
def forward(self, n_id, edge_index):
return self.gin[0](n_id, edge_index)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias), ('weight2', self.weight2), ('bias2', self.bias2)]
class MetaSGConv(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Linear(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
self.sgc = [SGC(self.weight, self.bias, K=2)]
def forward(self, n_id, edge_index):
return self.sgc[0](n_id, edge_index)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class MetaNetworkGCN(MetaModule):
def __init__(self, args, num_node):
super(MetaNetworkGCN, self).__init__()
self.node_emb = MetaEmb(num_node, args.emb_dim)
conv_layers = []
for _ in range(args.num_layers):
conv_layers.append(MetaGCN(args.emb_dim, args.emb_dim))
self.conv_layers = nn.ModuleList(conv_layers)
def forward(self, n_id, edge_index):
for i, conv in enumerate(self.conv_layers):
if i == 0:
x = conv(self.node_emb(n_id), edge_index.detach())
else:
x = F.relu(x)
x = conv(x, edge_index.detach())
return x
class MetaNetworkGAT(MetaModule):
def __init__(self, args, num_node):
super(MetaNetworkGAT, self).__init__()
self.node_emb = MetaEmb(num_node, args.emb_dim)
conv_layers = []
for _ in range(args.num_layers):
conv_layers.append(MetaGAT(args.emb_dim, args.emb_dim))
self.conv_layers = nn.ModuleList(conv_layers)
def forward(self, n_id, edge_index):
for i, conv in enumerate(self.conv_layers):
if i == 0:
x = conv(self.node_emb(n_id), edge_index)
else:
x = F.relu(x)
x = conv(x, edge_index)
return x
class MetaNetworkGIN(MetaModule):
def __init__(self, args, num_node):
super(MetaNetworkGIN, self).__init__()
self.node_emb = MetaEmb(num_node, args.emb_dim)
conv_layers = []
for _ in range(args.num_layers):
conv_layers.append(MetaGIN(args.emb_dim, args.emb_dim))
self.conv_layers = nn.ModuleList(conv_layers)
def forward(self, n_id, edge_index):
for i, conv in enumerate(self.conv_layers):
if i == 0:
x = conv(self.node_emb(n_id), edge_index)
else:
x = F.relu(x)
x = conv(x, edge_index)
return x
class MetaNetworkSGC(MetaModule):
def __init__(self, args, num_node):
super(MetaNetworkSGC, self).__init__()
self.node_emb = MetaEmb(num_node, args.emb_dim)
conv_layers = []
for _ in range(args.num_layers):
conv_layers.append(MetaSGConv(args.emb_dim, args.emb_dim))
self.conv_layers = nn.ModuleList(conv_layers)
def forward(self, n_id, edge_index):
for i, conv in enumerate(self.conv_layers):
if i == 0:
x = conv(self.node_emb(n_id), edge_index)
else:
x = F.relu(x)
x = conv(x, edge_index)
return x
class MetaLinear(MetaModule):
def __init__(self, *args, **kwargs):
super().__init__()
ignore = nn.Linear(*args, **kwargs)
self.register_buffer('weight', to_var(ignore.weight.data, requires_grad=True))
self.register_buffer('bias', to_var(ignore.bias.data, requires_grad=True))
def forward(self, x):
return F.linear(x, self.weight, self.bias)
def named_leaves(self):
return [('weight', self.weight), ('bias', self.bias)]
class Weight(MetaModule):
def __init__(self, in_channel, hidden, out_channel):
super(Weight, self).__init__()
self.linear1 = MetaLinear(in_channel, hidden)
self.linear2 = MetaLinear(hidden, out_channel)
def forward(self, x):
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
x = F.sigmoid(x)
return x
class Phi(MetaModule):
def __init__(self, in_channel, hidden, out_channel):
super(Phi, self).__init__()
self.linear1 = MetaLinear(in_channel, hidden)
self.linear2 = MetaLinear(hidden, out_channel)
def forward(self, x):
x = self.linear1(x)
x = F.relu(x)
x = self.linear2(x)
return x
class MetaNetworkHUB(MetaModule):
def __init__(self, args):
super(MetaNetworkHUB, self).__init__()
conv_layers = []
for _ in range(args.num_layers):
conv_layers.append(MetaGAT(args.emb_dim, args.emb_dim))
self.conv_layers = nn.ModuleList(conv_layers)
def forward(self, x, edge_index):
for i, conv in enumerate(self.conv_layers):
if i == 0:
x = conv(x, edge_index)
else:
x = F.relu(x)
x = conv(x, edge_index)
return x