-
Notifications
You must be signed in to change notification settings - Fork 1
/
loss.py
146 lines (95 loc) · 3.77 KB
/
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
# Copyright (c) 2018-present, Royal Bank of Canada.
# All rights reserved.
#
# This source code is licensed under the license found in the
# LICENSE file in the root directory of this source tree.
import torch
import torch.nn as nn
class Loss(object):
def __init__(self):
self.zeros_by_batch_size = {}
self.ones_by_batch_size = {}
def g_loss_fn(self, fake_score):
raise NotImplementedError()
def d_loss_fn(self, real_score, fake_score):
raise NotImplementedError()
def get_ones(self, ref_data):
batch_size = len(ref_data)
device = ref_data.device
return self.ones_by_batch_size.setdefault(
(batch_size, device.type, device.index),
torch.ones((batch_size, 1), device=device)
)
def get_zeros(self, ref_data):
batch_size = len(ref_data)
device = ref_data.device
return self.zeros_by_batch_size.setdefault(
(batch_size, device.type, device.index),
torch.zeros((batch_size, 1), device=device)
)
class JSLoss(Loss):
def __init__(self):
super().__init__()
self._loss_fn = nn.BCEWithLogitsLoss()
def g_loss_fn(self, fake_score):
return self._loss_fn(fake_score, self.get_ones(fake_score))
def d_loss_fn(self, real_score, fake_score):
return (self._loss_fn(real_score, self.get_ones(real_score)) +
self._loss_fn(fake_score, self.get_zeros(fake_score)))
class TVLoss(Loss):
def __init__(self):
super().__init__()
def _loss_fn(score):
return torch.mean(torch.sigmoid(score))
self._loss_fn = _loss_fn
def g_loss_fn(self, fake_score):
return -self._loss_fn(fake_score)
def d_loss_fn(self, real_score, fake_score):
return -self._loss_fn(real_score) + self._loss_fn(fake_score)
class KLLoss(Loss):
def __init__(self):
super().__init__()
def g_loss_fn(self, fake_score):
return -torch.mean(torch.exp(fake_score))
def d_loss_fn(self, real_score, fake_score):
return -torch.mean(real_score + 1) + torch.mean(torch.exp(fake_score))
class RKLLoss(Loss):
def __init__(self):
super().__init__()
def g_loss_fn(self, fake_score):
return -torch.mean(fake_score - 1)
def d_loss_fn(self, real_score, fake_score):
return (-torch.mean(-torch.exp(-real_score))
+ torch.mean(fake_score - 1))
class SHLoss(Loss):
def __init__(self):
super().__init__()
def g_loss_fn(self, fake_score):
return -torch.mean(torch.exp(fake_score) - 1)
def d_loss_fn(self, real_score, fake_score):
return (-torch.mean(1 - torch.exp(-real_score))
+ torch.mean(torch.exp(fake_score) - 1))
class WrongSHLoss(Loss):
def __init__(self):
super().__init__()
def g_loss_fn(self, fake_score):
return -torch.mean(fake_score * torch.exp(fake_score))
def d_loss_fn(self, real_score, fake_score):
return (-torch.mean(1 - torch.exp(-real_score))
+ torch.mean(fake_score * torch.exp(fake_score)))
class WSLoss(Loss):
def __init__(self):
super().__init__()
def g_loss_fn(self, fake_score):
return -torch.mean(fake_score)
def d_loss_fn(self, real_score, fake_score):
return -torch.mean(real_score) + torch.mean(fake_score)
class QuarterWSLoss(Loss):
def __init__(self):
super().__init__()
def g_loss_fn(self, fake_score):
return -0.25 * torch.mean(fake_score)
def d_loss_fn(self, real_score, fake_score):
# print(real_score.max().abs().item(), fake_score.max().abs().item())
# print(real_score.abs().mean().item(), fake_score.abs().mean().item())
return -0.25 * torch.mean(real_score) + 0.25 * torch.mean(fake_score)