-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss.py
26 lines (19 loc) · 805 Bytes
/
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
import torch
from torch import nn
class P2PGeneratorLoss(nn.Module):
def __init__(self, lmd):
super().__init__()
self.lmd = torch.tensor(lmd)
self.loss = nn.BCELoss()
def forward(self, disc_pred, generate_pred, truth):
entorpy_loss = self.loss(disc_pred, torch.ones_like(disc_pred))
l1_loss = torch.mean(torch.abs(generate_pred - truth))
return entorpy_loss + self.lmd * l1_loss
class P2PDiscriminatorLoss(nn.Module):
def __init__(self):
super().__init__()
self.loss = nn.BCELoss()
def forward(self, real_pred, generate_pred):
real_loss = self.loss(real_pred, torch.ones_like(real_pred))
fake_loss = self.loss(generate_pred, torch.zeros_like(generate_pred))
return real_loss + fake_loss