-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathloss.py
More file actions
39 lines (33 loc) · 1.31 KB
/
loss.py
File metadata and controls
39 lines (33 loc) · 1.31 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
"""
Author: Duy-Phuong Dao
Email: [email protected] (or [email protected])
"""
import torch
import torch.nn as nn
class KLDivergence(nn.Module):
"KL divergence between the estimated normal distribution and a prior distribution"
def __init__(self):
super(KLDivergence, self).__init__()
"""
N : the index N spans all dimensions of input
N = H x W x D
"""
self.N = 80*96*80
def forward(self, z_mean, z_log_sigma):
z_log_var = z_log_sigma * 2
#return (1/self.N) * ( (z_mean**2 + z_var**2 - z_log_var**2 - 1).sum() )
return 0.5 * ((z_mean**2 + z_log_var.exp() - z_log_var - 1).sum())
class L2Loss(nn.Module):
"Measuring the `Euclidian distance` between prediction and ground truh using `L2 Norm`"
def __init__(self):
super(L2Loss, self).__init__()
def forward(self, x, y):
N = y.shape[0]*y.shape[1]*y.shape[2]*y.shape[3]*y.shape[4]
return ( (x - y)**2 ).sum() / N
class L1Loss(nn.Module):
"Measuring the `Euclidian distance` between prediction and ground truh using `L1 Norm`"
def __init__(self):
super(L1Loss, self).__init__()
def forward(self, x, y):
N = y.shape[0]*y.shape[1]*y.shape[2]*y.shape[3]*y.shape[4]
return ( (x - y).abs()).sum() / N