-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathloss.py
More file actions
58 lines (49 loc) · 1.95 KB
/
Copy pathloss.py
File metadata and controls
58 lines (49 loc) · 1.95 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
import torch
from torch import nn
class GANLoss(nn.Module):
"""
A module for measuring the GAN Loss.
"""
def __init__(self, gan_mode='vanilla', real_label=1.0, fake_label=0.0):
"""
Initializes the GANLoss module.
:param real_label: The label value for real samples (default is 1.0).
:type real_label: float
:param fake_label: The label value for fake/generated samples (default is 0.0).
:type fake_label: float
"""
super().__init__()
self.register_buffer('real_label', torch.tensor(real_label))
self.register_buffer('fake_label', torch.tensor(fake_label))
if gan_mode == 'vanilla':
self.loss = nn.BCEWithLogitsLoss()
elif gan_mode == 'lsgan':
self.loss = nn.MSELoss()
def get_labels(self, preds, target_is_real):
"""
Returns the target labels based on whether the target is real or fake.
:param preds:The predictions from the discriminator.
:type preds: torch.Tensor
:param target_is_real:Indicates whether the target is a real sample.
:type target_is_real: bool
:return: The target labels expanded to match the shape of predictions.
:rtype: torch.Tensor
"""
if target_is_real:
labels = self.real_label
else:
labels = self.fake_label
return labels.expand_as(preds)
def __call__(self, preds, target_is_real):
"""
Computes and returns the adversarial loss given the predictions and target labels.
:param preds: The predictions from the discriminator.
:type preds: torch.Tensor
:param target_is_real: Indicates whether the target is a real sample.
:type target_is_real: bool
:return: The computed adversarial loss.
:rtype: torch.Tensor
"""
labels = self.get_labels(preds, target_is_real)
loss = self.loss(preds, labels)
return loss