-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwgan_loss.py
389 lines (366 loc) · 16.7 KB
/
wgan_loss.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
import torch
from torchgan.losses import DiscriminatorLoss, GeneratorLoss
import torch.autograd as autograd
from betaVAE import betaVAE
from sklearn.preprocessing import StandardScaler
def reduce_vae(x, reduction=None):
r"""Applies reduction on a torch.Tensor.
Args:
x (torch.Tensor): The tensor on which reduction is to be applied.
reduction (str, optional): The reduction to be applied. If ``mean`` the mean value of the
Tensor is returned. If ``sum`` the elements of the Tensor will be summed. If none of the
above then the Tensor is returning without any change.
Returns:
As per the above ``reduction`` convention.
"""
if reduction == "mean":
return torch.mean(x)
elif reduction == "sum":
return torch.sum(x)
else:
return x
def wasserstein_generator_loss_vae(fgz, reduction="mean"):
return reduce_vae(-1.0 * fgz, reduction='mean')
def wasserstein_discriminator_loss_vae(fx, fgz, reduction="mean"):
return reduce_vae(fgz - fx, reduction='mean')
def wasserstein_gradient_penalty_vae(interpolate, d_interpolate, reduction="mean"):
grad_outputs = torch.ones_like(d_interpolate)
gradients = autograd.grad(
outputs=d_interpolate,
inputs=interpolate,
grad_outputs=grad_outputs,
create_graph=True,
retain_graph=True,
only_inputs=True,
)[0]
gradient_penalty = (gradients.norm(2) - 1) ** 2
return reduce_vae(gradient_penalty, reduction='mean')
class WassersteinGeneratorLossVAE(GeneratorLoss):
r"""Wasserstein GAN generator loss from
`"Wasserstein GAN by Arjovsky et. al." <https://arxiv.org/abs/1701.07875>`_ paper
The loss can be described as:
.. math:: L(G) = -f(G(z))
where
- :math:`G` : Generator
- :math:`f` : Critic/Discriminator
- :math:`z` : A sample from the noise prior
Args:
reduction (str, optional): Specifies the reduction to apply to the output.
If ``none`` no reduction will be applied. If ``mean`` the mean of the output.
If ``sum`` the elements of the output will be summed.
override_train_ops (function, optional): A function is passed to this argument,
if the default ``train_ops`` is not to be used.
"""
def __init__(self, checkpoint, rna_features, beta=0.005):
super(WassersteinGeneratorLossVAE, self).__init__(
checkpoint, rna_features
)
self.betavae = betaVAE(rna_features, 2048, [6000, 4000, 2048], [4000, 6000], beta=beta)
self.betavae.load_state_dict(torch.load(checkpoint))
self.betavae.eval()
def forward(self, fgz):
r"""Computes the loss for the given input.
Args:
dgz (torch.Tensor) : Output of the Discriminator with generated data. It must have the
dimensions (N, \*) where \* means any number of additional
dimensions.
Returns:
scalar if reduction is applied else Tensor with dimensions (N, \*).
"""
return wasserstein_generator_loss_vae(fgz, self.reduction)
def train_ops(self,
generator,
discriminator,
optimizer_generator,
device,
batch_size,
real_inputs,
labels=None,
):
if labels is None and generator.label_type == "required":
raise Exception("GAN model requires labels for training")
batch_size = real_inputs['image'].size(0)
gene_coding = real_inputs['rna_data']
self.betavae = self.betavae.to(device)
z, _, _ = self.betavae.encode(gene_coding.to(device))
z = z.to(device)
# generate noise in range
noise = torch.FloatTensor(batch_size, generator.encoding_dims).uniform_(-0.3, 0.3)
noise = noise.to(device)
#noise = torch.randn(
# batch_size, generator.encoding_dims, device=device
#)
noise = noise + z
noise = (noise - torch.mean(noise, dim=0)) / torch.std(noise, dim=0)
optimizer_generator.zero_grad()
if generator.label_type == "generated":
label_gen = torch.randint(
0, generator.num_classes, (batch_size,), device=device
)
if generator.label_type == "none":
fake = generator(noise)
elif generator.label_type == "required":
fake = generator(noise, labels)
elif generator.label_type == "generated":
fake = generator(noise, label_gen)
if discriminator.label_type == "none":
dgz = discriminator(fake)
else:
if generator.label_type == "generated":
dgz = discriminator(fake, label_gen)
else:
dgz = discriminator(fake, labels)
loss = self.forward(dgz)
loss.backward()
optimizer_generator.step()
# NOTE(avik-pal): This will error if reduction is is 'none'
return loss.item()
class WassersteinDiscriminatorLossVAE(DiscriminatorLoss):
r"""Wasserstein GAN generator loss from
`"Wasserstein GAN by Arjovsky et. al." <https://arxiv.org/abs/1701.07875>`_ paper
The loss can be described as:
.. math:: L(D) = f(G(z)) - f(x)
where
- :math:`G` : Generator
- :math:`f` : Critic/Discriminator
- :math:`x` : A sample from the data distribution
- :math:`z` : A sample from the noise prior
Args:
reduction (str, optional): Specifies the reduction to apply to the output.
If ``none`` no reduction will be applied. If ``mean`` the mean of the output.
If ``sum`` the elements of the output will be summed.
clip (tuple, optional): Tuple that specifies the maximum and minimum parameter
clamping to be applied, as per the original version of the Wasserstein loss
without Gradient Penalty.
override_train_ops (function, optional): A function is passed to this argument,
if the default ``train_ops`` is not to be used.
"""
def __init__(self, checkpoint, rna_features, beta=0.005, reduction="mean", clip=None, override_train_ops=None):
super(WassersteinDiscriminatorLossVAE, self).__init__(
checkpoint, rna_features
)
if (isinstance(clip, tuple) or isinstance(clip, list)) and len(
clip
) > 1:
self.clip = clip
else:
self.clip = None
self.betavae = betaVAE(rna_features, 2048, [6000, 4000, 2048], [4000, 6000], beta=beta)
self.betavae.load_state_dict(torch.load(checkpoint))
self.betavae.eval()
def forward(self, fx, fgz):
r"""Computes the loss for the given input.
Args:
fx (torch.Tensor) : Output of the Discriminator with real data. It must have the
dimensions (N, \*) where \* means any number of additional
dimensions.
fgz (torch.Tensor) : Output of the Discriminator with generated data. It must have the
dimensions (N, \*) where \* means any number of additional
dimensions.
Returns:
scalar if reduction is applied else Tensor with dimensions (N, \*).
"""
return wasserstein_discriminator_loss_vae(fx, fgz, self.reduction)
def train_ops(
self,
generator,
discriminator,
optimizer_discriminator,
real_inputs,
device,
labels=None,
):
r"""Defines the standard ``train_ops`` used by wasserstein discriminator loss.
The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops
is as follows:
1. Clamp the discriminator parameters to satisfy :math:`lipschitz\ condition`
2. :math:`fake = generator(noise)`
3. :math:`value_1 = discriminator(fake)`
4. :math:`value_2 = discriminator(real)`
5. :math:`loss = loss\_function(value_1, value_2)`
6. Backpropagate by computing :math:`\nabla loss`
7. Run a step of the optimizer for discriminator
Args:
generator (torchgan.models.Generator): The model to be optimized.
discriminator (torchgan.models.Discriminator): The discriminator which judges the
performance of the generator.
optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters``
of the ``discriminator``.
real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``.
device (torch.device): Device on which the ``generator`` and ``discriminator`` is present.
labels (torch.Tensor, optional): Labels for the data.
Returns:
Scalar value of the loss.
"""
if self.clip is not None:
for p in discriminator.parameters():
p.data.clamp_(self.clip[0], self.clip[1])
if labels is None and (
generator.label_type == "required"
or discriminator.label_type == "required"
):
raise Exception("GAN model requires labels for training")
batch_size = real_inputs['image'].size(0)
gene_coding = real_inputs['rna_data']
self.betavae = self.betavae.to(device)
z, _, _ = self.betavae.encode(gene_coding.to(device))
z = z.to(device)
# generate noise in range
noise = torch.FloatTensor(batch_size, generator.encoding_dims).uniform_(-0.3, 0.3)
noise = noise.to(device)
#noise = torch.randn(
# batch_size, generator.encoding_dims, device=device
#)
noise = noise + z
noise = (noise - torch.mean(noise, dim=0)) / torch.std(noise, dim=0)
if generator.label_type == "generated":
label_gen = torch.randint(
0, generator.num_classes, (batch_size,), device=device
)
real_image = real_inputs['image'].to(device)
optimizer_discriminator.zero_grad()
if discriminator.label_type == "none":
dx = discriminator(real_image)
elif discriminator.label_type == "required":
dx = discriminator(real_image, labels)
else:
dx = discriminator(real_image, label_gen)
if generator.label_type == "none":
fake = generator(noise)
elif generator.label_type == "required":
fake = generator(noise, labels)
else:
fake = generator(noise, label_gen)
if discriminator.label_type == "none":
dgz = discriminator(fake.detach())
else:
if generator.label_type == "generated":
dgz = discriminator(fake.detach(), label_gen)
else:
dgz = discriminator(fake.detach(), labels)
loss = self.forward(dx, dgz)
loss.backward()
optimizer_discriminator.step()
# NOTE(avik-pal): This will error if reduction is is 'none'
return loss.item()
class WassersteinGradientPenaltyVAE(DiscriminatorLoss):
r"""Gradient Penalty for the Improved Wasserstein GAN discriminator from
`"Improved Training of Wasserstein GANs
by Gulrajani et. al." <https://arxiv.org/abs/1704.00028>`_ paper
The gradient penalty is calculated as:
.. math: \lambda \times (||\nabla(D(x))||_2 - 1)^2
The gradient being taken with respect to x
where
- :math:`G` : Generator
- :math:`D` : Disrciminator/Critic
- :math:`\lambda` : Scaling hyperparameter
- :math:`x` : Interpolation term for the gradient penalty
Args:
reduction (str, optional): Specifies the reduction to apply to the output.
If ``none`` no reduction will be applied. If ``mean`` the mean of the output.
If ``sum`` the elements of the output will be summed.
lambd (float,optional): Hyperparameter lambda for scaling the gradient penalty.
override_train_ops (function, optional): A function is passed to this argument,
if the default ``train_ops`` is not to be used.
"""
def __init__(self, checkpoint, rna_features, reduction="mean", lambd=10.0, override_train_ops=None, beta=0.005):
super(WassersteinGradientPenaltyVAE, self).__init__(
checkpoint, rna_features
)
self.lambd = lambd
self.override_train_ops = override_train_ops
self.betavae = betaVAE(rna_features, 2048, [6000, 4000, 2048], [4000, 6000], beta=beta)
self.betavae.load_state_dict(torch.load(checkpoint))
self.betavae.eval()
def forward(self, interpolate, d_interpolate):
r"""Computes the loss for the given input.
Args:
interpolate (torch.Tensor) : It must have the dimensions (N, \*) where
\* means any number of additional dimensions.
d_interpolate (torch.Tensor) : Output of the ``discriminator`` with ``interpolate``
as the input. It must have the dimensions (N, \*)
where \* means any number of additional dimensions.
Returns:
scalar if reduction is applied else Tensor with dimensions (N, \*).
"""
# TODO(Aniket1998): Check for performance bottlenecks
# If found, write the backprop yourself instead of
# relying on autograd
return wasserstein_gradient_penalty_vae(
interpolate, d_interpolate, self.reduction
)
def train_ops(
self,
generator,
discriminator,
optimizer_discriminator,
real_inputs,
device,
labels=None,
):
r"""Defines the standard ``train_ops`` used by the Wasserstein Gradient Penalty.
The ``standard optimization algorithm`` for the ``discriminator`` defined in this train_ops
is as follows:
1. :math:`fake = generator(noise)`
2. :math:`interpolate = \epsilon \times real + (1 - \epsilon) \times fake`
3. :math:`d\_interpolate = discriminator(interpolate)`
4. :math:`loss = \lambda loss\_function(interpolate, d\_interpolate)`
5. Backpropagate by computing :math:`\nabla loss`
6. Run a step of the optimizer for discriminator
Args:
generator (torchgan.models.Generator): The model to be optimized.
discriminator (torchgan.models.Discriminator): The discriminator which judges the
performance of the generator.
optimizer_discriminator (torch.optim.Optimizer): Optimizer which updates the ``parameters``
of the ``discriminator``.
real_inputs (torch.Tensor): The real data to be fed to the ``discriminator``.
device (torch.device): Device on which the ``generator`` and ``discriminator`` is present.
batch_size (int): Batch Size of the data infered from the ``DataLoader`` by the ``Trainer``.
labels (torch.Tensor, optional): Labels for the data.
Returns:
Scalar value of the loss.
"""
if labels is None and (
generator.label_type == "required"
or discriminator.label_type == "required"
):
raise Exception("GAN model requires labels for training")
batch_size = real_inputs['image'].size(0)
gene_coding = real_inputs['rna_data']
self.betavae = self.betavae.to(device)
z, _, _ = self.betavae.encode(gene_coding.to(device))
z = z.detach().to(device)
# generate noise in range
noise = torch.FloatTensor(batch_size, generator.encoding_dims).uniform_(-0.3, 0.3)
noise = noise.to(device)
#noise = torch.randn(
# batch_size, generator.encoding_dims, device=device
#)
noise = noise + z
noise = (noise - torch.mean(noise, dim=0)) / torch.std(noise, dim=0)
real_image = real_inputs['image'].to(device)
if generator.label_type == "generated":
label_gen = torch.randint(
0, generator.num_classes, (batch_size,), device=device
)
optimizer_discriminator.zero_grad()
if generator.label_type == "none":
fake = generator(noise)
elif generator.label_type == "required":
fake = generator(noise, labels)
else:
fake = generator(noise, label_gen)
eps = torch.rand(1).item()
interpolate = eps * real_image + (1 - eps) * fake
if discriminator.label_type == "none":
d_interpolate = discriminator(interpolate)
else:
if generator.label_type == "generated":
d_interpolate = discriminator(interpolate, label_gen)
else:
d_interpolate = discriminator(interpolate, labels)
loss = self.forward(interpolate, d_interpolate)
weighted_loss = self.lambd * loss
weighted_loss.backward()
optimizer_discriminator.step()
return loss.item()