-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathoptimize.py
206 lines (139 loc) · 7.1 KB
/
optimize.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
import torch
import torch.nn as nn
import torch.nn.functional as F
def DGDA_loss(res, labels, adj, domain, weights, manipulate=False, dadj=None):
class_weight, recons_weight, beta, ent_weight, d_w, y_w, m_w = weights
# Reconstruction loss
recon_loss = recons_weight * recons_loss(res['a_recons'], adj)
if manipulate:
recon_loss += m_w * recons_loss(res['m_recons'], dadj)
kld = kl_loss(res['dmu'], res['dlv'])
kly = kl_loss(res['ymu'], res['ylv'])
klm = kl_loss(res['mmu'], res['mlv'])
kld = kld + kly + klm
ent_loss = max_entropy(res['d']) + max_entropy(res['y']) + max_entropy(res['m'])
if domain == 0:
class_loss = F.cross_entropy(input=res['cls_output'], target=labels, weight=class_weight)
domain_labels = torch.zeros_like(labels).float()
else:
class_loss = torch.zeros(())
domain_labels = torch.ones_like(labels).float()
domain_loss = F.binary_cross_entropy_with_logits(input=res['dom_output'].view(-1), target=domain_labels)
loss = recon_loss + beta * kld + y_w * class_loss + d_w * domain_loss + ent_weight * ent_loss
loss = torch.maximum(loss, torch.zeros_like(loss))
return loss
def DGDA_m_loss(res, labels, adj, domain, weights):
class_weight, recons_weight, beta, ent_weight, d_w, y_w, m_w = weights
recon_loss = recons_weight * recons_loss(res['a_recons'], adj)
kld = kl_loss(res['dmu'], res['dlv'])
kly = kl_loss(res['ymu'], res['ylv'])
kld = kld + kly
ent_loss = max_entropy(res['d']) + max_entropy(res['y'])
if domain == 0:
class_loss = F.cross_entropy(input=res['cls_output'], target=labels, weight=class_weight)
domain_labels = torch.zeros_like(labels).float()
else:
class_loss = torch.zeros(())
domain_labels = torch.ones_like(labels).float()
domain_loss = F.binary_cross_entropy_with_logits(input=res['dom_output'].view(-1), target=domain_labels)
loss = recon_loss + beta * kld + y_w * class_loss + d_w * domain_loss + ent_weight * ent_loss
loss = torch.maximum(loss, torch.zeros_like(loss))
return loss
def DSR_loss(res, labels, adj, domain, weights):
class_weight, recons_weight, beta, d_w, y_w = weights
# Reconstruction loss
recon_loss = recons_weight * recons_loss(res['a_recons'], adj)
kld = kl_loss(res['dmu'], res['dlv'])
kly = kl_loss(res['ymu'], res['ylv'])
kld = kld + kly
if domain == 0:
domain_labels = torch.zeros_like(labels).float()
sem_cls_loss = F.cross_entropy(input=res['sem_cls'], target=labels, weight=class_weight)
sem_dom_loss = F.binary_cross_entropy_with_logits(input=res['sem_dom'].view(-1), target=domain_labels)
dom_cls_loss = max_entropy(res['dom_cls'])
dom_dom_loss = F.binary_cross_entropy_with_logits(input=res['dom_dom'].view(-1), target=domain_labels)
else:
domain_labels = torch.ones_like(labels).float()
sem_cls_loss = 0
sem_dom_loss = F.binary_cross_entropy_with_logits(input=res['sem_dom'].view(-1), target=domain_labels)
dom_cls_loss = max_entropy(res['dom_cls'])
dom_dom_loss = F.binary_cross_entropy_with_logits(input=res['dom_dom'].view(-1), target=domain_labels)
loss = recon_loss + beta * kld + sem_cls_loss + sem_dom_loss + dom_cls_loss + dom_dom_loss
return loss
def DIVA_loss(res, labels, adj, domain, weights):
class_weight, recons_weight, beta, d_w, y_w = weights
# Reconstruction loss
rl = recons_weight * recons_loss(res['a_recons'], adj)
kld = kl_loss(res['dmu'], res['dlv'])
kly = kl_loss(res['ymu'], res['ylv'])
klm = kl_loss(res['mmu'], res['mlv'])
kld = kld + kly + klm
if domain == 0:
cl = F.cross_entropy(input=res['cls_output'], target=labels, weight=class_weight)
domain_labels = torch.zeros_like(labels).float()
else:
cl = torch.zeros(())
domain_labels = torch.ones_like(labels).float()
dl = F.binary_cross_entropy_with_logits(input=res['dom_output'].view(-1), target=domain_labels)
loss = rl + beta * kld + y_w * cl + d_w * dl
return loss
def DANN_loss(res, labels, domain, class_weight, dw, yw):
if domain == 0:
cl = F.cross_entropy(input=res['cls_output'], target=labels, weight=class_weight)
domain_labels = torch.zeros_like(labels).float()
else:
cl = 0.0
domain_labels = torch.ones_like(labels).float()
dl = F.binary_cross_entropy_with_logits(input=res['dom_output'].view(-1), target=domain_labels)
loss = yw * cl + dw * dl
return loss
def MDD_loss(src_res, tar_res, labels_source, class_weight, src_weight=1.0):
class_criterion = nn.CrossEntropyLoss(weight=class_weight)
# _, outputs, _, outputs_adv = self.c_net(inputs)
n_src = labels_source.size(0)
n_tar = tar_res['cls_output'].size(0)
outputs = torch.cat([src_res['cls_output'], tar_res['cls_output']], dim=0)
# outputs_adv = torch.cat([src_res['adv_output'], tar_res['adv_output']], dim=0)
classifier_loss = class_criterion(input=src_res['cls_output'], target=labels_source)
target_adv = outputs.max(1)[1]
target_adv_src = target_adv.narrow(0, 0, n_src)
target_adv_tgt = target_adv.narrow(0, n_src, n_tar)
classifier_loss_adv_src = class_criterion(input=src_res['adv_output'], target=target_adv_src)
logloss_tgt = torch.log(1.0 - F.softmax(tar_res['adv_output'], dim=1))
classifier_loss_adv_tgt = F.nll_loss(logloss_tgt, target_adv_tgt)
transfer_loss = src_weight * classifier_loss_adv_src + classifier_loss_adv_tgt
total_loss = classifier_loss + transfer_loss
return total_loss
def recons_loss(recons, adjs):
batch_size, n_node, _ = recons.shape
total_node = batch_size * n_node * n_node
n_edges = adjs.sum()
device = adjs.device
if n_edges == 0: # no positive edges
pos_weight = torch.zeros(()).to(device)
else:
pos_weight = float(total_node - n_edges) / n_edges
norm = float(total_node) / (2 * (total_node - n_edges))
rl = norm * F.binary_cross_entropy_with_logits(input=recons, target=adjs, pos_weight=pos_weight, reduction='mean')
rl = torch.maximum(rl, torch.zeros_like(rl))
return rl
def kl_loss(mu, lv):
n_node = mu.shape[1]
kld = -0.5 / n_node * torch.mean(torch.sum(1 + 2 * lv - mu.pow(2) - lv.exp().pow(2), dim=-1))
return kld
def max_entropy(x):
ent = 0.693148 + torch.mean(torch.sigmoid(x) * F.logsigmoid(x))
return ent
def l1_loss(x):
return x.abs().mean()
def learning_rate_adjust(optimizer, lr):
for param_group in optimizer.param_groups:
param_group['lr'] = lr
def learning_rate_decay(optimizer, decay_rate=0.99):
for param_group in optimizer.param_groups:
param_group['lr'] = param_group['lr'] * decay_rate
def clip_gradient(optimizer, grad_clip=0.1):
for group in optimizer.param_groups:
for param in group["params"]:
if param.grad is not None:
param.grad.data.clamp_(-grad_clip, grad_clip)