Skip to content

Commit 3243317

Browse files
committed
D( G(z).detach() ), added D means for monitoring
and use os.makedirs py2.7 compatible
1 parent c66216f commit 3243317

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

dcgan/main.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@
3535
opt = parser.parse_args()
3636
print(opt)
3737

38-
os.makedirs(opt.outf, exist_ok=True)
38+
try:
39+
os.makedirs(opt.outf)
40+
except OSError:
41+
pass
3942
opt.manualSeed = random.randint(1, 10000) # fix seed
4043
print("Random Seed: ", opt.manualSeed)
4144
random.seed(opt.manualSeed)
@@ -205,16 +208,17 @@ def forward(self, input):
205208
output = netD(input)
206209
errD_real = criterion(output, label)
207210
errD_real.backward()
211+
D_x = output.data.mean()
208212

209213
# train with fake
210214
noise.data.resize_(batch_size, nz, 1, 1)
211215
noise.data.normal_(0, 1)
212-
fake = netG(noise)
213-
input.data.copy_(fake.data)
216+
fake = netG(noise).detach()
214217
label.data.fill_(fake_label)
215-
output = netD(input)
218+
output = netD(fake)
216219
errD_fake = criterion(output, label)
217220
errD_fake.backward()
221+
D_G_z1 = output.data.mean()
218222
errD = errD_real + errD_fake
219223
optimizerD.step()
220224

@@ -228,11 +232,12 @@ def forward(self, input):
228232
output = netD(fake)
229233
errG = criterion(output, label)
230234
errG.backward()
235+
D_G_z2 = output.data.mean()
231236
optimizerG.step()
232237

233-
print('[%d/%d][%d/%d] Loss_D: %f Loss_G: %f'
238+
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f / %.4f'
234239
% (epoch, opt.niter, i, len(dataloader),
235-
errD.data[0], errG.data[0]))
240+
errD.data[0], errG.data[0], D_x, D_G_z1, D_G_z2))
236241
if i % 100 == 0:
237242
vutils.save_image(real_cpu,
238243
'%s/real_samples.png' % opt.outf)

0 commit comments

Comments
 (0)