-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathloss.py
112 lines (82 loc) · 4.17 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
import torch
import torch.nn as nn
class CCCLoss(nn.Module):
def __init__(self):
super(CCCLoss, self).__init__()
def forward(self, y_pred, y_true, seq_lens=None):
if seq_lens is not None:
mask = torch.ones_like(y_true, device=y_true.device)
for i, seq_len in enumerate(seq_lens):
mask[i, seq_len:] = 0
else:
mask = torch.ones_like(y_true, device=y_true.device)
y_true_mean = torch.sum(y_true * mask, dim=1, keepdim=True) / torch.sum(mask, dim=1, keepdim=True)
y_pred_mean = torch.sum(y_pred * mask, dim=1, keepdim=True) / torch.sum(mask, dim=1, keepdim=True)
y_true_var = torch.sum(mask * (y_true - y_true_mean) ** 2, dim=1, keepdim=True) / torch.sum(mask, dim=1,
keepdim=True)
y_pred_var = torch.sum(mask * (y_pred - y_pred_mean) ** 2, dim=1, keepdim=True) / torch.sum(mask, dim=1,
keepdim=True)
cov = torch.sum(mask * (y_true - y_true_mean) * (y_pred - y_pred_mean), dim=1, keepdim=True) \
/ torch.sum(mask, dim=1, keepdim=True)
ccc = torch.mean(2.0 * cov / (y_true_var + y_pred_var + (y_true_mean - y_pred_mean) ** 2), dim=0)
ccc = ccc.squeeze(0)
ccc_loss = 1.0 - ccc
return ccc_loss
class MSEPCCLoss(nn.Module):
def __init__(self):
super().__init__()
self.mse_loss = nn.MSELoss(reduction='mean')
def forward(self, y_pred, y_true, seq_lens=None):
if seq_lens is not None:
mask = torch.ones_like(y_true, device=y_true.device)
for i, seq_len in enumerate(seq_lens):
mask[i, seq_len:] = 0
else:
mask = torch.ones_like(y_true, device=y_true.device)
y_true_mean = torch.sum(y_true * mask, dim=1, keepdim=True) / torch.sum(mask, dim=1, keepdim=True)
y_pred_mean = torch.sum(y_pred * mask, dim=1, keepdim=True) / torch.sum(mask, dim=1, keepdim=True)
y_true_std = y_true.std(dim=1, keepdim=True)
y_pred_std = y_pred.std(dim=1, keepdim=True)
cov = torch.sum(mask * (y_true - y_true_mean) * (y_pred - y_pred_mean), dim=1, keepdim=True) \
/ torch.sum(mask, dim=1, keepdim=True)
pcc = torch.mean(cov / (y_true_std * y_pred_std), dim=0)
pcc = pcc.squeeze(0)
pcc_loss = 1.0 - pcc
mseloss = self.mse_loss(y_pred, y_true)
msepcc_loss = (1/2) * mseloss + (1/2) * pcc_loss
# msepccccc_loss = (1/3)*mseloss + (1/3)*pcc_loss + (1/3)*CCCLoss
return msepcc_loss
# wraps BCEWithLogitsLoss, but constructor accepts (and ignores) argument seq_lens
class WrappedBCEWithLogitsLoss(nn.Module):
def __init__(self):
super().__init__()
self.loss_fn = nn.BCEWithLogitsLoss()
def forward(self, y_pred, y_true, seq_lens=None):
return self.loss_fn(y_pred, y_true)
class WrappedMSELoss(nn.Module):
def __init__(self, reduction):
super().__init__()
self.loss_fn = nn.MSELoss(reduction=reduction)
def forward(self, y_pred, y_true, seq_lens=None):
return self.loss_fn(y_pred, y_true)
# wraps BCELoss, but constructor accepts (and ignores) argument seq_lens
class WrappedBCELoss(nn.Module):
def __init__(self):
super().__init__()
self.loss_fn = nn.BCELoss()
def forward(self, y_pred, y_true, seq_lens=None):
return self.loss_fn(y_pred, y_true)
def get_segment_wise_labels(labels):
# collapse labels to one label per segment (as originally for MuSe-Sent)
segment_labels = []
for i in range(labels.size(0)):
segment_labels.append(labels[i, 0, :])
labels = torch.stack(segment_labels).long()
return labels
def get_segment_wise_logits(logits, feature_lens):
# determines exactly one output for each segment (by taking the last timestamp of each segment)
segment_logits = []
for i in range(logits.size(0)):
segment_logits.append(logits[i, feature_lens[i] - 1, :]) # (batch-size, frames, classes)
logits = torch.stack(segment_logits, dim=0)
return logits