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
308
309
310
311
312
313
314
315
316
|
import copy
import torch
import torch.nn.functional as F
from algorithms.single_model_algorithm import SingleModelAlgorithm
from models.initializer import initialize_model
from utils import move_to
from models.gnn import GNN_node
from torch_geometric.nn import global_mean_pool
class AbstractDANN(SingleModelAlgorithm):
"""Domain-Adversarial Neural Networks (abstract class)"""
# def __init__(self, input_shape, num_classes, num_train_domains,
# hparams, conditional, class_balance):
def __init__(self, config, d_out, grouper, loss,
metric, n_train_steps, conditional, class_balance):
featurizer, classifier = initialize_model(config, d_out=d_out, is_featurizer=True)
featurizer = featurizer.to(config.device)
classifier = classifier.to(config.device)
model = torch.nn.Sequential(featurizer, classifier).to(config.device)
# initialize module
super().__init__(
config=config,
model=model,
grouper=grouper,
loss=loss,
metric=metric,
n_train_steps=n_train_steps,
)
assert config.num_train_domains <= 1000 # domain space shouldn't be too large
self.featurizer = featurizer
self.classifier = classifier
self.register_buffer('update_count', torch.tensor([0]))
##############################################
self.hparams_lambda = config.dann_lambda
self.conditional = conditional
self.class_balance = class_balance
num_classes = d_out
emb_dim = self.featurizer.d_out
self.discriminator = torch.nn.Sequential(torch.nn.Linear(emb_dim, emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(emb_dim, emb_dim),
torch.nn.ReLU(),
torch.nn.Linear(emb_dim, config.num_train_domains)).to(config.device)
self.class_embeddings = torch.nn.Embedding(num_classes,
self.featurizer.d_out).to(config.device)
# Optimizers
self.disc_opt = torch.optim.Adam(
(list(self.discriminator.parameters()) +
list(self.class_embeddings.parameters())),
lr=config.lr,
weight_decay=0,
betas=(0.5, 0.9))
self.gen_opt = torch.optim.Adam(
(list(self.featurizer.parameters()) +
list(self.classifier.parameters())),
lr=config.lr,
weight_decay=0,
betas=(0.5, 0.9))
def process_batch(self, batch):
"""
Override
"""
# forward pass
x, y_true, metadata = batch
x = x.to(self.device)
y_true = y_true.to(self.device)
g = self.grouper.metadata_to_group(metadata).to(self.device)
features = self.featurizer(x)
outputs = self.classifier(features)
# package the results
results = {
'g': g,
'y_true': y_true,
'y_pred': outputs,
'metadata': metadata,
'features': features,
}
return results
def update(self, batch):
x, y_true, metadata = batch
x = move_to(x, self.device)
y_true = move_to(y_true, self.device)
g = move_to(self.grouper.metadata_to_group(metadata), self.device)
results = {
'g': g,
'y_true': y_true,
'metadata': metadata,
}
self.update_count += 1
z = self.featurizer(x)
if self.conditional:
disc_input = z + self.class_embeddings(y_true)
else:
disc_input = z
disc_out = self.discriminator(disc_input)
# should be the domain label
disc_labels = move_to(metadata[:,0].flatten(), self.device)
if self.class_balance:
y_counts = F.one_hot(y_true).sum(dim=0)
weights = 1. / (y_counts[y_true] * y_counts.shape[0]).float()
disc_loss = F.cross_entropy(disc_out, disc_labels, reduction='none')
disc_loss = (weights * disc_loss).sum()
else:
disc_loss = F.cross_entropy(disc_out, disc_labels)
disc_softmax = F.softmax(disc_out, dim=1)
input_grad = torch.autograd.grad(disc_softmax[:, disc_labels].sum(),
[disc_input], create_graph=True)[0]
grad_penalty = (input_grad**2).sum(dim=1).mean(dim=0)
hparams_gra_penalty = 0
disc_loss += hparams_gra_penalty * grad_penalty
d_steps_per_g = hparam_d_steps_per_g_step = 1
all_preds = self.classifier(z)
results['y_pred'] = all_preds
classifier_loss = self.objective(results)
if (self.update_count.item() % (1+d_steps_per_g) < d_steps_per_g):
self.disc_opt.zero_grad()
disc_loss.backward()
self.disc_opt.step()
else:
gen_loss = (classifier_loss +
(self.hparams_lambda * -disc_loss))
self.disc_opt.zero_grad()
self.gen_opt.zero_grad()
gen_loss.backward()
self.gen_opt.step()
results['objective'] = classifier_loss.item()
self.step_schedulers(
is_epoch=False,
metrics=results,
log_access=False)
# log results
self.update_log(results)
return self.sanitize_dict(results)
def objective(self, results):
return self.loss.compute(results['y_pred'], results['y_true'], return_dict=False)
class OurAbstractDANN(SingleModelAlgorithm):
"""Domain-Adversarial Neural Networks (abstract class)"""
# def __init__(self, input_shape, num_classes, num_train_domains,
# hparams, conditional, class_balance):
def __init__(self, config, d_out, grouper, loss,
metric, n_train_steps):
featurizer, pooler, classifier = initialize_model(config, d_out=d_out, is_featurizer=True, is_pooled=False)
featurizer = featurizer.to(config.device)
classifier = classifier.to(config.device)
model = classifier # fake, useless
# initialize module
super().__init__(
config=config,
model=model,
grouper=grouper,
loss=loss,
metric=metric,
n_train_steps=n_train_steps,
)
assert config.num_train_domains <= 1000 # domain space shouldn't be too large
self.featurizer = featurizer
self.classifier = classifier
self.pooler = pooler
self.register_buffer('update_count', torch.tensor([0]))
##############################################
self.hparams_lambda = config.dann_lambda
num_classes = d_out
emb_dim = self.featurizer.d_out
# GNN type fixed at GIN for the discriminator, layer num fixed at 2
self.discriminator_gnn = GNN_node(num_layer=2, emb_dim=emb_dim, dropout=0, batchnorm=False,
dataset_group=config.model_kwargs['dataset_group']).to(config.device)
self.discriminator_gnn.destroy_node_encoder()
self.discriminator_pool = global_mean_pool
self.discriminator_mlp = torch.nn.Linear(emb_dim, config.num_train_domains).to(config.device)
# self.discriminator_mlp = torch.nn.Sequential(
# torch.nn.Linear(emb_dim, emb_dim),
# torch.nn.BatchNorm1d(emb_dim), torch.nn.ReLU(),
# torch.nn.Linear(emb_dim, config.num_train_domains)
# ).to(config.device)
# Optimizers
self.disc_opt = torch.optim.Adam(
(list(self.discriminator_gnn.parameters()) +
list(self.discriminator_mlp.parameters())),
lr=config.lr,
weight_decay=0,
betas=(0.5, 0.9))
self.gen_opt = torch.optim.Adam(
(list(self.featurizer.parameters()) +
list(self.classifier.parameters())),
lr=config.lr,
weight_decay=0,
betas=(0.5, 0.9))
def process_batch(self, batch):
"""
Override
"""
# forward pass
x, y_true, metadata = batch
x = x.to(self.device)
y_true = y_true.to(self.device)
g = self.grouper.metadata_to_group(metadata).to(self.device)
features = self.pooler(*self.featurizer(x))
outputs = self.classifier(features)
# package the results
results = {
'g': g,
'y_true': y_true,
'y_pred': outputs,
'metadata': metadata,
'features': features,
}
return results
def update(self, batch):
x, y_true, metadata = batch
x = move_to(x, self.device)
y_true = move_to(y_true, self.device)
g = move_to(self.grouper.metadata_to_group(metadata), self.device)
results = {
'g': g,
'y_true': y_true,
'metadata': metadata,
}
self.update_count += 1
disc_input = z = self.featurizer(x)
disc_x = copy.deepcopy(x)
disc_x.x = disc_input[0]
disc_out = self.discriminator_gnn(disc_x)
disc_out = self.discriminator_pool(disc_out, disc_input[1])
disc_out = self.discriminator_mlp(disc_out)
# should be the domain label
disc_labels = move_to(metadata[:,0].flatten(), self.device)
disc_loss = F.cross_entropy(disc_out, disc_labels)
d_steps_per_g = hparam_d_steps_per_g_step = 1
all_preds = self.classifier(self.pooler(*z))
results['y_pred'] = all_preds
classifier_loss = self.objective(results)
if (self.update_count.item() % (1+d_steps_per_g) < d_steps_per_g):
self.disc_opt.zero_grad()
disc_loss.backward()
self.disc_opt.step()
else:
gen_loss = (classifier_loss +
(self.hparams_lambda * -disc_loss))
self.disc_opt.zero_grad()
self.gen_opt.zero_grad()
gen_loss.backward()
self.gen_opt.step()
results['objective'] = classifier_loss.item()
self.step_schedulers(
is_epoch=False,
metrics=results,
log_access=False)
# log results
self.update_log(results)
return self.sanitize_dict(results)
def objective(self, results):
return self.loss.compute(results['y_pred'], results['y_true'], return_dict=False)
class DANN(AbstractDANN):
"""Unconditional DANN"""
def __init__(self, config, d_out, grouper, loss,
metric, n_train_steps):
super(DANN, self).__init__(config, d_out, grouper, loss,
metric, n_train_steps, conditional=False, class_balance=False)
class CDANN(AbstractDANN):
"""Conditional DANN"""
def __init__(self, config, d_out, grouper, loss,
metric, n_train_steps):
super(CDANN, self).__init__(config, d_out, grouper, loss,
metric, n_train_steps, conditional=True, class_balance=True)
class DANNG(OurAbstractDANN):
"""Conditional DANN"""
def __init__(self, config, d_out, grouper, loss,
metric, n_train_steps):
super(DANNG, self).__init__(config, d_out, grouper, loss,
metric, n_train_steps)
|