@@ -64,22 +64,23 @@ def denorm(x):
64
64
# Build mini-batch dataset
65
65
batch_size = images .size (0 )
66
66
images = to_var (images .view (batch_size , - 1 ))
67
+
67
68
# Create the labels which are later used as input for the BCE loss
68
69
real_labels = to_var (torch .ones (batch_size ))
69
70
fake_labels = to_var (torch .zeros (batch_size ))
70
71
71
72
#============= Train the discriminator =============#
72
- # Compute loss with real images
73
+ # Compute BCE_Loss using real images where BCE_Loss(x, y): - y * log(D(x)) - (1-y) * log(1 - D(x))
74
+ # Second term of the loss is always zero since real_labels == 1
73
75
outputs = D (images )
74
- # Apply BCE loss. Second term is always zero since real_labels == 1
75
76
d_loss_real = criterion (outputs , real_labels )
76
77
real_score = outputs
77
78
78
- # Compute loss with fake images
79
+ # Compute BCELoss using fake images
80
+ # First term of the loss is always zero since fake_labels == 0
79
81
z = to_var (torch .randn (batch_size , 64 ))
80
82
fake_images = G (z )
81
83
outputs = D (fake_images )
82
- # Apply BCE loss. First term is always zero since fake_labels == 0
83
84
d_loss_fake = criterion (outputs , fake_labels )
84
85
fake_score = outputs
85
86
@@ -94,11 +95,9 @@ def denorm(x):
94
95
z = to_var (torch .randn (batch_size , 64 ))
95
96
fake_images = G (z )
96
97
outputs = D (fake_images )
97
- # remember that min log(1-D(G(z))) has the same fix point as max log(D(G(z)))
98
- # Here we maximize log(D(G(z))), which is exactly the first term in the BCE loss
99
- # with t=1. (see definition of BCE for info on t)
100
- # t==1 is valid for real_labels, thus we use them as input for the BCE loss.
101
- # Don't get yourself confused by this. It is just convenient to use to the BCE loss.
98
+
99
+ # We train G to maximize log(D(G(z)) instead of minimizing log(1-D(G(z)))
100
+ # For the reason, see the last paragraph of section 3. https://arxiv.org/pdf/1406.2661.pdf
102
101
g_loss = criterion (outputs , real_labels )
103
102
104
103
# Backprop + Optimize
0 commit comments