-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathloss.py
executable file
·33 lines (25 loc) · 1.01 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
import torch
import torch.nn.functional as F
def MIL(y_pred, batch_size, is_transformer=0):
loss = torch.tensor(0.).cuda()
loss_intra = torch.tensor(0.).cuda()
sparsity = torch.tensor(0.).cuda()
smooth = torch.tensor(0.).cuda()
if is_transformer==0:
y_pred = y_pred.view(batch_size, -1)
else:
y_pred = torch.sigmoid(y_pred)
for i in range(batch_size):
anomaly_index = torch.randperm(30).cuda()
normal_index = torch.randperm(30).cuda()
y_anomaly = y_pred[i, :32][anomaly_index]
y_normal = y_pred[i, 32:][normal_index]
y_anomaly_max = torch.max(y_anomaly) # anomaly
y_anomaly_min = torch.min(y_anomaly)
y_normal_max = torch.max(y_normal) # normal
y_normal_min = torch.min(y_normal)
loss += F.relu(1.-y_anomaly_max+y_normal_max)
sparsity += torch.sum(y_anomaly)*0.00008
smooth += torch.sum((y_pred[i,:31] - y_pred[i,1:32])**2)*0.00008
loss = (loss+sparsity+smooth)/batch_size
return loss