Exemplo n.º 1
0
X = Variable(X)
z = Variable(z)
label = Variable(label)

for it in range(10):
    for batch_idx, (data, target) in enumerate(train_loader, 0):
        netD.zero_grad()
        mb_size = data.size(0)
        X.data.resize_(data.size()).copy_(data)
        X.data.resize_(mb_size, x_dim)
        z.data.resize_(mb_size, z_dim).normal_(0, 1)
        label.data.resize_(mb_size)

        G_share_sample = netG_share(z)
        G_indep_sample = create_netG_indeps_sample(netG_indeps, G_share_sample)

        ############################
        # (1) Update D network:
        ###########################
        D_real = netD(X)
        D_fake = netD_fake(G_indep_sample, netD)
        D_loss = compute_dloss(D_real, D_fake, label)
        D_exp.add_scalar_value('D_loss',
                               D_loss.data[0],
                               step=batch_idx + it * train_size)
        D_loss.backward(retain_variables=True)
        D_solver.step()

        ############################
        # (2) Update G network:
Exemplo n.º 2
0
        torch.load('./result/vanillgan_mnist_latent100/out/netD_epoch_22.pth'))

for it in range(iter):
    for batch_idx, (data, target) in enumerate(train_loader, 0):
        ################
        #  (0) process data
        ################
        netD.zero_grad()
        mb_size = data.size(0)
        X.data.resize_(data.size()).copy_(data)
        X.data.resize_(mb_size, x_dim)
        z.data.resize_(mb_size, z_dim).normal_(0, 1)
        label.data.resize_(mb_size)
        cnt = batch_idx + it * train_size

        G_indep_sample = create_netG_indeps_sample(netG_indeps, z)
        G_share_sample = create_netG_share_sample(netG_share, G_indep_sample)

        ############################
        # (1) Update D network:  vaillgan loss
        ###########################
        if not netD_continue_trian:
            D_real = netD(X)
            D_fake = netD_fake(G_share_sample, netD)
            D_loss = compute_dloss(D_real, D_fake, label)
            D_exp.add_scalar_value('D_loss', D_loss.data[0], step=cnt)
            D_loss.backward(retain_variables=True)
            D_solver.step()
        else:
            D_real = netD(X).data.numpy()[0][0]
            D_fake = netD_fake(G_share_sample, netD)