-
Notifications
You must be signed in to change notification settings - Fork 4
/
losses.py
209 lines (163 loc) · 6.89 KB
/
losses.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
import torch
import torch.nn.functional as F
# DCGAN loss
def loss_dcgan_dis(dis_fake, dis_real):
L1 = torch.mean(F.softplus(-dis_real))
L2 = torch.mean(F.softplus(dis_fake))
return L1, L2
def loss_dcgan_gen(dis_fake):
loss = torch.mean(F.softplus(-dis_fake))
return loss
def loss_dcgan_dis_new(dis_fake, dis_real, dis_real_fake) :
L1 = torch.mean(F.softplus(-dis_real))
L2 = torch.mean(F.softplus(dis_fake))
L_real_fake = torch.mean(F.softplus(dis_real_fake))
return L1, L2, L_real_fake
# Hinge Loss
# def loss_hinge_dis(dis_fake, dis_real, ratio):
def loss_hinge_dis(dis_fake, dis_real):
"""
fixed to take in density ratio estimates
"""
# properly match up dimensions, and only reweight real examples
# weighted = F.relu(1. - dis_real) * ratio.unsqueeze(1)
weighted = F.relu(1. - dis_real)
loss_real = torch.mean(weighted)
# loss_real = torch.mean(F.relu(1. - dis_real))
loss_fake = torch.mean(F.relu(1. + dis_fake))
return loss_real, loss_fake
# def loss_hinge_dis(dis_fake, dis_real): # This version returns a single loss
# loss = torch.mean(F.relu(1. - dis_real))
# loss += torch.mean(F.relu(1. + dis_fake))
# return loss
def loss_hinge_analysis(dis_real):
"""
fixed to take in density ratio estimates
"""
# properly match up dimensions, and only reweight real examples
# weighted = F.relu(1. - dis_real) * ratio.unsqueeze(1)
weighted = F.relu(1. - dis_real)
loss_real = weighted
return loss_real
def loss_hinge_dis_new(dis_fake, dis_real, dis_real_fake):
loss_real = torch.mean(F.relu(1. - dis_real))
loss_fake = torch.mean(F.relu(1. + dis_fake))
loss_real_fake = torch.mean(F.relu(1. + dis_real_fake))
return loss_real, loss_fake, loss_real_fake
def loss_hinge_dis_new_fake(dis_fake, dis_real, dis_real_fake, dis_fake_fake):
loss_real = torch.mean(F.relu(1. - dis_real))
loss_fake = torch.mean(F.relu(1. + dis_fake))
loss_real_fake = torch.mean(F.relu(1. + dis_real_fake))
loss_fake_fake = torch.mean(F.relu(1. + dis_fake_fake))
return loss_real, loss_fake, loss_real_fake, loss_fake_fake
def loss_hinge_gen(dis_fake):
# with torch.no_grad():
# dis_fake_norm = torch.exp(dis_fake).mean()
# dis_fake_ratio = torch.exp(dis_fake) / dis_fake_norm
# dis_fake = dis_fake * dis_fake_ratio
loss = -torch.mean(dis_fake)
return loss
def loss_kl_dis(dis_fake, dis_real):
loss_real = torch.mean(F.relu(1. - dis_real))
with torch.no_grad():
dis_fake_m = dis_fake - dis_fake.mean()
dis_fake_m = torch.clamp(dis_fake_m, min=-10.0, max=10.0)
dis_fake_norm = torch.exp(dis_fake_m).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake_m) + 1e-8) / dis_fake_norm
dis_fake = dis_fake * dis_fake_ratio
loss_fake = torch.mean(F.relu(1. + dis_fake))
return loss_real, loss_fake
def loss_kl_dis_new(dis_fake, dis_real, dis_real_fake):
loss_real = torch.mean(F.relu(1. - dis_real))
with torch.no_grad():
dis_fake_m = dis_fake - dis_fake.mean()
dis_fake_m = torch.clamp(dis_fake_m, min=-10.0, max=10.0)
dis_fake_norm = torch.exp(dis_fake_m).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake_m) + 1e-8) / dis_fake_norm
dis_fake = dis_fake * dis_fake_ratio
loss_fake = torch.mean(F.relu(1. + dis_fake))
with torch.no_grad():
dis_fake_m = dis_real_fake - dis_real_fake.mean()
dis_fake_m = torch.clamp(dis_fake_m, min=-10.0, max=10.0)
dis_fake_norm = torch.exp(dis_fake_m).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake_m) + 1e-8) / dis_fake_norm
dis_real_fake = dis_real_fake * dis_fake_ratio
loss_real_fake = torch.mean(F.relu(1. + dis_real_fake))
return loss_real, loss_fake, loss_real_fake
def loss_kl_gen(dis_fake):
with torch.no_grad():
dis_fake_m = dis_fake - dis_fake.mean()
dis_fake_m = torch.clamp(dis_fake_m, min=-10.0, max=10.0)
dis_fake_norm = torch.exp(dis_fake_m).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake_m) + 1e-8) / dis_fake_norm
dis_fake = dis_fake * dis_fake_ratio
loss = -torch.mean(dis_fake)
return loss
def loss_kl_grad_dis(dis_fake, dis_real):
dis_fake_m = dis_fake - dis_fake.mean()
dis_fake_m = torch.clamp(dis_fake_m, min=-10.0, max=10.0)
dis_fake_norm = torch.exp(dis_fake_m).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake_m) + 1e-8) / dis_fake_norm
dis_fake = dis_fake * dis_fake_ratio
loss_real = torch.mean(F.relu(1. - dis_real))
loss_fake = torch.mean(F.relu(1. + dis_fake))
return loss_real, loss_fake
def loss_kl_grad_gen(dis_fake):
dis_fake_m = dis_fake - dis_fake.mean()
dis_fake_m = torch.clamp(dis_fake_m, min=-10.0, max=10.0)
dis_fake_norm = torch.exp(dis_fake_m).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake_m) + 1e-8) / dis_fake_norm
dis_fake = dis_fake * dis_fake_ratio
loss = -torch.mean(dis_fake)
return loss
# maybe modify for this
# restricted kl-fgan
def loss_f_kl_dis(dis_fake, dis_real):
import ipdb
ipdb.set_trace()
loss_real = torch.mean(F.relu(1.0 - dis_real))
loss_fake = torch.mean(torch.exp(dis_fake - 1.0))
return loss_real, loss_fake
def loss_f_kl_gen(dis_fake):
import ipdb
ipdb.set_trace()
loss = -torch.mean(torch.exp(dis_fake - 1.0))
return loss
def loss_dv_dis(dis_fake, dis_real):
loss_real = torch.mean(F.relu(1.0 - dis_real))
loss_fake = -torch.logsumexp(dis_fake) / dis_fake.size(0)
return loss_real, loss_fake
def loss_dv_gen(dis_fake):
loss = torch.logsumexp(dis_fake) / dis_fake.size(0)
return loss
# chi^2
def loss_chi_dis(dis_fake, dis_real):
dis_fake = torch.clamp(dis_fake, -1.0, 1.0)
dis_real = torch.clamp(dis_real, -1.0, 1.0)
loss_real = torch.mean(- dis_real)
dis_fake_mean = torch.mean(dis_fake)
loss_fake = torch.mean(dis_fake * (dis_fake - dis_fake_mean + 2)) / 2.0
return loss_real, loss_fake
def loss_chi_gen(dis_fake):
dis_fake = torch.clamp(dis_fake, -1.0, 1.0)
dis_fake_mean = torch.mean(dis_fake)
loss_fake = -torch.mean(dis_fake * (dis_fake - dis_fake_mean + 2)) / 2.0
return loss_fake
def loss_dv_dis(dis_fake, dis_real):
loss_real = torch.mean(F.relu(1. - dis_real))
dis_fake_norm = torch.exp(dis_fake).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake) + 1e-8) / dis_fake_norm
dis_fake = dis_fake * dis_fake_ratio
loss_fake = torch.mean(F.relu(1. + dis_fake)) + \
torch.mean(dis_fake_ratio * torch.log(dis_fake_ratio))
return loss_real, loss_fake
def loss_dv_gen(dis_fake):
dis_fake_norm = torch.exp(dis_fake).mean() + 1e-8
dis_fake_ratio = (torch.exp(dis_fake) + 1e-8) / dis_fake_norm
dis_fake = dis_fake * dis_fake_ratio
loss = -torch.mean(dis_fake) - \
torch.mean(dis_fake_ratio * torch.log(dis_fake_ratio))
return loss
# Default to hinge loss
generator_loss = loss_hinge_gen
discriminator_loss = loss_hinge_dis