-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss.py
More file actions
104 lines (76 loc) · 2.49 KB
/
loss.py
File metadata and controls
104 lines (76 loc) · 2.49 KB
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
import torch
import math
INV_SQRT_TWO = 1 / math.sqrt(2)
INV_SQRT_TWOPI = 1 / math.sqrt(2 * math.pi)
def big_phi(x):
return 0.5 * (1 + (x * INV_SQRT_TWO).erf())
def trunc_cdf_ava(x, mu, sigma, a=0.5, b=10.5):
def normalize(val):
return (val - mu) / (sigma + 1e-4)
big_phi_x = big_phi(normalize(x))
big_phi_a = big_phi(normalize(torch.tensor(a)))
big_phi_b = big_phi(normalize(torch.tensor(b)))
cdf = (big_phi_x - big_phi_a) / (big_phi_b - big_phi_a + 1e-4)
if torch.sum(torch.isnan(cdf)):
print(cdf)
return cdf
def cjs_loss(y_true, y_pred):
loss = 0
y1s = [
trunc_cdf_ava(i + 1.5, y_true[:, 0], y_true[:, 1]) for i in range(10)
]
y2s = [
trunc_cdf_ava(i + 1.5, y_pred[:, 0], y_pred[:, 1]) for i in range(10)
]
for i in range(10):
y1 = y1s[i]
y2 = y2s[i]
ys = 0.5 * (y1 + y2)
loss += 0.5 * y1 * (y1 / ys + 1).log()
loss += 0.5 * y2 * (y2 / ys + 1).log()
if torch.sum(torch.isnan(loss)):
print(loss)
return loss.mean()
def trunc_cjs_loss_R(y_true, y_pred):
return cjs_loss(y_true, y_pred)
class TruncCJSLossD:
def __init__(self, L1_D):
self.L1_D = L1_D
def __call__(self, y_true, y_pred, mask):
loss = -cjs_loss(y_true, y_pred)
loss += self.L1_D * mask.abs().mean()
return loss
def cjs_loss_10(y_true, y_pred):
loss = 0
y1 = y_true.cumsum(dim=1) + 1e-5
y2 = y_pred.cumsum(dim=1) + 1e-5
ys = 0.5 * (y1 + y2)
loss += 0.5 * y1 * (y1 / ys).log()
loss += 0.5 * y2 * (y2 / ys).log()
return loss.sum(dim=1).mean()
class CJSLoss10R:
def __init__(self, L1_R):
self.L1_R = L1_R
def __call__(self, y_true, y_pred, ill_features):
loss = cjs_loss_10(y_true, y_pred)
# l1_term = ill_features.abs().mean()
# loss += 0.6 * l1_term
return loss
class CJSLoss10D:
def __init__(self, L1_D):
self.L1_D = L1_D
def __call__(self, y_true, y_pred, mask):
loss = -cjs_loss_10(y_true, y_pred)
loss += -4 * loss.detach().item() * mask.abs().mean()
return loss
####
def toy_loss_R(y_true, y_pred):
return ((y_true[:, 0] - y_pred[:, 0])**2).mean()**0.5
# remember to include the L1 penelty
class ToyLossD:
def __init__(self, L1_D):
self.L1_D = L1_D
def __call__(self, y_true, y_pred, mask):
loss = -((y_true[:, 0] - y_pred[:, 0])**2).mean()**0.5
loss += self.L1_D * mask.abs().sum()
return loss