-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloss.py
50 lines (34 loc) · 1.48 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
# Markus Enzweiler - [email protected]
import torch
import torch.nn as nn
# We define two different loss functions, one for the real images
# and one for the fake images.
# We use the binary cross entropy loss (BCELoss) for both since
# the discriminator is a binary classifier and has a sigmoid
# activation function in the last layer.
# smoothing class=1 to [0.8, 1.1]
def smooth_real_labels(y, dev):
return y - 0.2 + (torch.rand(y.shape, device=dev) * 0.3)
# smoothing class=0 to [0.0, 0.2]
def smooth_fake_labels(y, dev):
return y + (torch.rand(y.shape, device=dev) * 0.2)
def bce_loss_real(predictions, smooth=False, device=torch.device("cpu")):
criterion = nn.BCELoss()
# we use the label 1 for real images
# Add label smoothing in real loss to
# prevent discriminator becoming too strong too quickly
if smooth:
real_labels = smooth_real_labels(torch.ones_like(predictions), device)
else:
real_labels = torch.ones_like(predictions)
return criterion(predictions, real_labels)
def bce_loss_fake(predictions, smooth=False, device=torch.device("cpu")):
criterion = nn.BCELoss()
# we use the label 0 for fake images
# Add label smoothing in fake loss to
# prevent discriminator becoming too strong too quickly
if smooth:
fake_labels = smooth_fake_labels(torch.zeros_like(predictions), device)
else:
fake_labels = torch.zeros_like(predictions)
return criterion(predictions, fake_labels)