Skip to content

Commit

Permalink
D( G(z).detach() ), added D means for monitoring
Browse files Browse the repository at this point in the history
and use os.makedirs py2.7 compatible
  • Loading branch information
tomsercu committed Jan 27, 2017
1 parent c66216f commit 3243317
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions dcgan/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,10 @@
opt = parser.parse_args()
print(opt)

os.makedirs(opt.outf, exist_ok=True)
try:
os.makedirs(opt.outf)
except OSError:
pass
opt.manualSeed = random.randint(1, 10000) # fix seed
print("Random Seed: ", opt.manualSeed)
random.seed(opt.manualSeed)
Expand Down Expand Up @@ -205,16 +208,17 @@ def forward(self, input):
output = netD(input)
errD_real = criterion(output, label)
errD_real.backward()
D_x = output.data.mean()

# train with fake
noise.data.resize_(batch_size, nz, 1, 1)
noise.data.normal_(0, 1)
fake = netG(noise)
input.data.copy_(fake.data)
fake = netG(noise).detach()
label.data.fill_(fake_label)
output = netD(input)
output = netD(fake)
errD_fake = criterion(output, label)
errD_fake.backward()
D_G_z1 = output.data.mean()
errD = errD_real + errD_fake
optimizerD.step()

Expand All @@ -228,11 +232,12 @@ def forward(self, input):
output = netD(fake)
errG = criterion(output, label)
errG.backward()
D_G_z2 = output.data.mean()
optimizerG.step()

print('[%d/%d][%d/%d] Loss_D: %f Loss_G: %f'
print('[%d/%d][%d/%d] Loss_D: %.4f Loss_G: %.4f D(x): %.4f D(G(z)): %.4f / %.4f'
% (epoch, opt.niter, i, len(dataloader),
errD.data[0], errG.data[0]))
errD.data[0], errG.data[0], D_x, D_G_z1, D_G_z2))
if i % 100 == 0:
vutils.save_image(real_cpu,
'%s/real_samples.png' % opt.outf)
Expand Down

0 comments on commit 3243317

Please sign in to comment.