-
Notifications
You must be signed in to change notification settings - Fork 4
/
model_search.py
291 lines (240 loc) · 9.57 KB
/
model_search.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
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
from operations import *
from torch.autograd import Variable
from genotypes import NA_PRIMITIVES, SC_PRIMITIVES, LA_PRIMITIVES
def act_map(act):
if act == "linear":
return lambda x: x
elif act == "elu":
return torch.nn.functional.elu
elif act == "sigmoid":
return torch.sigmoid
elif act == "tanh":
return torch.tanh
elif act == "relu":
return torch.nn.functional.relu
elif act == "relu6":
return torch.nn.functional.relu6
elif act == "softplus":
return torch.nn.functional.softplus
elif act == "leaky_relu":
return torch.nn.functional.leaky_relu
else:
raise Exception("wrong activate function")
class NaMixedOp(nn.Module):
def __init__(self, in_dim, out_dim, with_linear):
super(NaMixedOp, self).__init__()
self._ops = nn.ModuleList()
self.with_linear = with_linear
for primitive in NA_PRIMITIVES:
op = NA_OPS[primitive](in_dim, out_dim)
self._ops.append(op)
if with_linear:
self._ops_linear = nn.ModuleList()
op_linear = torch.nn.Linear(in_dim, out_dim)
self._ops_linear.append(op_linear)
def forward(self, x, weights, edge_index, ):
mixed_res = []
if self.with_linear:
for w, op, linear in zip(weights, self._ops, self._ops_linear):
mixed_res.append(w * F.elu(op(x, edge_index)+linear(x)))
else:
for w, op in zip(weights, self._ops):
mixed_res.append(w * F.elu(op(x, edge_index)))
return sum(mixed_res)
class ScMixedOp(nn.Module):
def __init__(self):
super(ScMixedOp, self).__init__()
self._ops = nn.ModuleList()
for primitive in SC_PRIMITIVES:
op = SC_OPS[primitive]()
self._ops.append(op)
def forward(self, x, weights):
mixed_res = []
for w, op in zip(weights, self._ops):
mixed_res.append(w * op(x))
return sum(mixed_res)
class LaMixedOp(nn.Module):
def __init__(self, hidden_size, num_layers=None):
super(LaMixedOp, self).__init__()
self._ops = nn.ModuleList()
for primitive in LA_PRIMITIVES:
op = LA_OPS[primitive](hidden_size, num_layers)
self._ops.append(op)
def forward(self, x, weights):
mixed_res = []
for w, op in zip(weights, self._ops):
mixed_res.append(w * F.relu(op(x)))
return sum(mixed_res)
class Network(nn.Module):
'''
implement this for sane.
Actually, sane can be seen as the combination of three cells, node aggregator, skip connection, and layer aggregator
for sane, we dont need cell, since the DAG is the whole search space, and what we need to do is implement the DAG.
'''
def __init__(self, criterion, in_dim, out_dim, hidden_size, num_layers=3, dropout=0.5, epsilon=0.0, with_conv_linear=False, args=None):
super(Network, self).__init__()
self.in_dim = in_dim
self.out_dim = out_dim
self.hidden_size = hidden_size
self.num_layers = num_layers
self._criterion = criterion
self.dropout = dropout
self.epsilon = epsilon
self.explore_num = 0
self.with_linear = with_conv_linear
self.args = args
#node aggregator op
self.lin1 = nn.Linear(in_dim, hidden_size)
self.layers = nn.ModuleList()
for i in range(self.num_layers):
self.layers.append(NaMixedOp(hidden_size, hidden_size,self.with_linear))
# self.layer1 = NaMixedOp(hidden_size, hidden_size,self.with_linear)
# self.layer2 = NaMixedOp(hidden_size, hidden_size,self.with_linear)
# self.layer3 = NaMixedOp(hidden_size, hidden_size,self.with_linear)
#skip op
self.scops = nn.ModuleList()
for i in range(self.num_layers-1):
self.scops.append(ScMixedOp())
if not self.args.fix_last:
self.scops.append(ScMixedOp())
# self.layer4 = ScMixedOp()
# self.layer5 = ScMixedOp()
# if not self.args.fix_last:
# self.layer6 = ScMixedOp()
#layer aggregator op
self.laop = LaMixedOp(hidden_size, num_layers)
# self.classifier = nn.Linear(hidden_size, out_dim)
self.classifier = nn.Sequential(
nn.Linear(hidden_size, hidden_size),
nn.ReLU(),
nn.Linear(hidden_size, out_dim))
self._initialize_alphas()
def new(self):
model_new = Network(self._criterion, self.in_dim, self.out_dim, self.hidden_size).cuda()
for x, y in zip(model_new.arch_parameters(), self.arch_parameters()):
x.data.copy_(y.data)
return model_new
def forward(self, data, discrete=False):
x, edge_index = data.x, data.edge_index
#prob = float(np.random.choice(range(1,11), 1) / 10.0)
self.na_weights = F.softmax(self.na_alphas, dim=-1)
self.sc_weights = F.softmax(self.sc_alphas, dim=-1)
self.la_weights = F.softmax(self.la_alphas, dim=-1)
#generate weights by softmax
x = self.lin1(x)
x = F.dropout(x, p=self.dropout, training=self.training)
jk = []
for i in range(self.num_layers):
x = self.layers[i](x, self.na_weights[0], edge_index)
x = F.dropout(x, p=self.dropout, training=self.training)
if self.args.fix_last and i == self.num_layers-1:
jk += [x]
else:
jk += [self.scops[i](x, self.sc_weights[i])]
# x1 = self.layer1(x, self.na_weights[0], edge_index)
# x1 = F.dropout(x1, p=self.dropout, training=self.training)
# x2 = self.layer2(x1, self.na_weights[1], edge_index)
# x2 = F.dropout(x2, p=self.dropout, training=self.training)
# x3 = self.layer3(x2, self.na_weights[2], edge_index)
# x3 = F.dropout(x3, p=self.dropout, training=self.training)
# if self.args.fix_last:
# x4 = (x3, self.layer4(x1, self.sc_weights[0]), self.layer5(x2, self.sc_weights[1]))
# else:
# x4 = (self.layer4(x1, self.sc_weights[0]), self.layer5(x2, self.sc_weights[1]), self.layer6(x3, self.sc_weights[2]))
# x5 = self.layer7(x4, self.la_weights[0])
# x5 = F.dropout(x5, p=self.dropout, training=self.training)
merge_feature = self.laop(jk, self.la_weights[0])
merge_feature = F.dropout(merge_feature, p=self.dropout, training=self.training)
logits = self.classifier(merge_feature)
return logits
def _loss(self, data, is_valid=True):
logits = self(data)
if is_valid:
input = logits[data.val_mask].cuda()
target = data.y[data.val_mask].cuda()
else:
input = logits[data.train_mask].cuda()
target = data.y[data.train_mask].cuda()
return self._criterion(input, target)
def _loss_ppi(self, data, is_valid=True):
input = self(data).cuda()
target = data.y.cuda()
return self._criterion(input, target)
def _initialize_alphas(self):
#k = sum(1 for i in range(self._steps) for n in range(2+i))
num_na_ops = len(NA_PRIMITIVES)
num_sc_ops = len(SC_PRIMITIVES)
num_la_ops = len(LA_PRIMITIVES)
#self.alphas_normal = Variable(1e-3*torch.randn(k, num_ops).cuda(), requires_grad=True)
self.na_alphas = Variable(1e-3*torch.randn(3, num_na_ops).cuda(), requires_grad=True)
if self.args.fix_last:
self.sc_alphas = Variable(1e-3*torch.randn(2, num_sc_ops).cuda(), requires_grad=True)
else:
self.sc_alphas = Variable(1e-3*torch.randn(3, num_sc_ops).cuda(), requires_grad=True)
self.la_alphas = Variable(1e-3*torch.randn(1, num_la_ops).cuda(), requires_grad=True)
self._arch_parameters = [
self.na_alphas,
self.sc_alphas,
self.la_alphas,
]
def arch_parameters(self):
return self._arch_parameters
def genotype(self):
def _parse(na_weights, sc_weights, la_weights):
gene = []
na_indices = torch.argmax(na_weights, dim=-1)
for k in na_indices:
gene.append(NA_PRIMITIVES[k])
#sc_indices = sc_weights.argmax(dim=-1)
sc_indices = torch.argmax(sc_weights, dim=-1)
for k in sc_indices:
gene.append(SC_PRIMITIVES[k])
#la_indices = la_weights.argmax(dim=-1)
la_indices = torch.argmax(la_weights, dim=-1)
for k in la_indices:
gene.append(LA_PRIMITIVES[k])
return '||'.join(gene)
gene = _parse(F.softmax(self.na_alphas, dim=-1).data.cpu(), F.softmax(self.sc_alphas, dim=-1).data.cpu(), F.softmax(self.la_alphas, dim=-1).data.cpu())
return gene
def sample_arch(self):
num_na_ops = len(NA_PRIMITIVES)
num_sc_ops = len(SC_PRIMITIVES)
num_la_ops = len(LA_PRIMITIVES)
gene = []
for i in range(3):
op = np.random.choice(NA_PRIMITIVES, 1)[0]
gene.append(op)
for i in range(2):
op = np.random.choice(SC_PRIMITIVES, 1)[0]
gene.append(op)
op = np.random.choice(LA_PRIMITIVES, 1)[0]
gene.append(op)
return '||'.join(gene)
def get_weights_from_arch(self, arch):
arch_ops = arch.split('||')
#print('arch=%s' % arch)
num_na_ops = len(NA_PRIMITIVES)
num_sc_ops = len(SC_PRIMITIVES)
num_la_ops = len(LA_PRIMITIVES)
na_alphas = Variable(torch.zeros(3, num_na_ops).cuda(), requires_grad=True)
sc_alphas = Variable(torch.zeros(2, num_sc_ops).cuda(), requires_grad=True)
la_alphas = Variable(torch.zeros(1, num_la_ops).cuda(), requires_grad=True)
for i in range(3):
ind = NA_PRIMITIVES.index(arch_ops[i])
na_alphas[i][ind] = 1
for i in range(3, 5):
ind = SC_PRIMITIVES.index(arch_ops[i])
sc_alphas[i-3][ind] = 1
ind = LA_PRIMITIVES.index(arch_ops[5])
la_alphas[0][ind] = 1
arch_parameters = [na_alphas, sc_alphas, la_alphas]
return arch_parameters
def set_model_weights(self, weights):
self.na_weights = weights[0]
self.sc_weights = weights[1]
self.la_weights = weights[2]
#self._arch_parameters = [self.na_alphas, self.sc_alphas, self.la_alphas]