-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathloss.py
89 lines (72 loc) · 2.88 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
import torch.nn as nn
import torch as torch
import torch.nn.functional as F
class Loss_Knn(nn.Module):
def __init__(self, margin):
self.margin = margin
super(Loss_Knn, self).__init__()
def forward(self, z, z_nn):
n_batch, n_nn, d = z_nn.shape
z = z.unsqueeze(dim=1).repeat(1, n_nn, 1)
dist = ((z-z_nn)**2).mean(dim=1)
dist = dist.sum(dim=1)
dist = F.relu(dist-self.margin)
loss = dist.sum(dim=0)
return loss
class Dist_KNN(nn.Module):
def __init__(self, margin):
self.margin = margin
super(Dist_KNN, self).__init__()
def forward(self, z, z_nn):
n_batch, n_nn, d = z_nn.shape
z = z.unsqueeze(dim=1).repeat(1, n_nn, 1)
dist = ((z-z_nn)**2).mean(dim=1)
dist = dist.sum(dim=1)
dist = F.relu(dist-self.margin)
return dist
class VAE_LOSS(nn.Module):
def __init__(self):
super(VAE_LOSS, self).__init__()
def forward(self, recon_x, x, mu, logvar, rec_type='MSE', gamma=1):
if rec_type == 'BCE':
BCE = F.binary_cross_entropy(recon_x, x, reduction='sum')
elif rec_type == 'MSE':
BCE = F.mse_loss(recon_x, x, reduction='sum')
# see Appendix B from VAE paper:
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
# https://arxiv.org/abs/1312.6114
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
return BCE + gamma * KLD
class VAE_LOSS_SCORE(nn.Module):
def __init__(self):
super(VAE_LOSS_SCORE, self).__init__()
def forward(self, recon_x, x, mu, logvar, rec_type='MSE'):
if rec_type == 'BCE':
BCE = F.binary_cross_entropy(recon_x, x, reduce=False)
BCE = BCE.sum(dim=1)
elif rec_type == 'MSE':
BCE = F.mse_loss(recon_x, x, reduce=False)
BCE = BCE.sum(dim=1)
# see Appendix B from VAE paper:
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
# https://arxiv.org/abs/1312.6114
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
KLD = -0.5 * (1 + logvar - mu.pow(2) - logvar.exp())
KLD = KLD.sum(dim=1)
return BCE + KLD
class VAE_Outlier_SCORE(nn.Module):
def __init__(self):
super(VAE_Outlier_SCORE, self).__init__()
def forward(self, recon_x, x, mu, logvar, rec_type='MSE'):
if rec_type == 'BCE':
BCE = F.binary_cross_entropy(recon_x, x, reduce=False)
BCE = BCE.sum(dim=1)
elif rec_type == 'MSE':
BCE = F.mse_loss(recon_x, x, reduce=False)
BCE = BCE.sum(dim=1)
# see Appendix B from VAE paper:
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
# https://arxiv.org/abs/1312.6114
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
return BCE