From cd2db0069f6530d537ccd7c9ade9b7bfc845c409 Mon Sep 17 00:00:00 2001 From: Jett Date: Wed, 26 Sep 2018 08:06:39 -0500 Subject: [PATCH] In pytorch 0.5 0-dim tensors will be an error rather than a warning, this replaces 0-dim tensors with item() to get a Python number from the tensors containing a single value. --- GAN/ali_bigan/ali_bigan_pytorch.py | 2 +- GAN/auxiliary_classifier_gan/ac_gan_pytorch.py | 2 +- GAN/boundary_equilibrium_gan/began_pytorch.py | 4 ++-- GAN/boundary_seeking_gan/bgan_pytorch.py | 2 +- GAN/coupled_gan/cogan_pytorch.py | 4 ++-- GAN/disco_gan/discogan_pytorch.py | 2 +- GAN/dual_gan/dualgan_pytorch.py | 2 +- GAN/ebgan/ebgan_pytorch.py | 2 +- GAN/f_gan/f_gan_pytorch.py | 2 +- GAN/generative_adversarial_parallelization/gap_pytorch.py | 2 +- GAN/gibbsnet/gibbsnet_pytorch.py | 3 +-- GAN/least_squares_gan/lsgan_pytorch.py | 2 +- GAN/magan/magan_pytorch.py | 4 ++-- GAN/softmax_gan/softmax_gan_pytorch.py | 2 +- VAE/adversarial_autoencoder/aae_pytorch.py | 2 +- VAE/adversarial_vb/avb_pytorch.py | 2 +- VAE/conditional_vae/cvae_pytorch.py | 2 +- VAE/denoising_vae/dvae_pytorch.py | 2 +- VAE/vanilla_vae/vae_pytorch.py | 2 +- 19 files changed, 22 insertions(+), 23 deletions(-) diff --git a/GAN/ali_bigan/ali_bigan_pytorch.py b/GAN/ali_bigan/ali_bigan_pytorch.py index 708f232..c1e94b3 100644 --- a/GAN/ali_bigan/ali_bigan_pytorch.py +++ b/GAN/ali_bigan/ali_bigan_pytorch.py @@ -99,7 +99,7 @@ def reset_grad(): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) samples = P(z).data.numpy()[:16] diff --git a/GAN/auxiliary_classifier_gan/ac_gan_pytorch.py b/GAN/auxiliary_classifier_gan/ac_gan_pytorch.py index 0a4ace1..ab3ddd1 100644 --- a/GAN/auxiliary_classifier_gan/ac_gan_pytorch.py +++ b/GAN/auxiliary_classifier_gan/ac_gan_pytorch.py @@ -129,7 +129,7 @@ def reset_grad(): samples = G(z, c).data.numpy() print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}; Idx: {}' - .format(it, -D_loss.data[0], -G_loss.data[0], idx)) + .format(it, -D_loss.item(), -G_loss.item(), idx)) fig = plt.figure(figsize=(4, 4)) gs = gridspec.GridSpec(4, 4) diff --git a/GAN/boundary_equilibrium_gan/began_pytorch.py b/GAN/boundary_equilibrium_gan/began_pytorch.py index 3c9bfec..a966d42 100644 --- a/GAN/boundary_equilibrium_gan/began_pytorch.py +++ b/GAN/boundary_equilibrium_gan/began_pytorch.py @@ -81,14 +81,14 @@ def reset_grad(): # Update k, the equlibrium k = k + lam * (gamma*D(X) - D(G(z_G))) - k = k.data[0] # k is variable, so unvariable it so that no gradient prop. + k = k.item() # k is variable, so unvariable it so that no gradient prop. # Print and plot every now and then if it % 1000 == 0: measure = D(X) + torch.abs(gamma*D(X) - D(G(z_G))) print('Iter-{}; Convergence measure: {:.4}' - .format(it, measure.data[0])) + .format(it, measure.item())) samples = G(z_G).data.numpy()[:16] diff --git a/GAN/boundary_seeking_gan/bgan_pytorch.py b/GAN/boundary_seeking_gan/bgan_pytorch.py index 083a901..3223b39 100644 --- a/GAN/boundary_seeking_gan/bgan_pytorch.py +++ b/GAN/boundary_seeking_gan/bgan_pytorch.py @@ -80,7 +80,7 @@ def reset_grad(): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) samples = G(z).data.numpy()[:16] diff --git a/GAN/coupled_gan/cogan_pytorch.py b/GAN/coupled_gan/cogan_pytorch.py index 445d18e..dc90b16 100644 --- a/GAN/coupled_gan/cogan_pytorch.py +++ b/GAN/coupled_gan/cogan_pytorch.py @@ -173,8 +173,8 @@ def sample_x(X, size): print('Iter-{}; D1_loss: {:.4}; G1_loss: {:.4}; ' 'D2_loss: {:.4}; G2_loss: {:.4}' .format( - it, D1_loss.data[0], G1_loss.data[0], - D2_loss.data[0], G2_loss.data[0]) + it, D1_loss.item(), G1_loss.item(), + D2_loss.item(), G2_loss.item()) ) z = Variable(torch.randn(8, z_dim)) diff --git a/GAN/disco_gan/discogan_pytorch.py b/GAN/disco_gan/discogan_pytorch.py index 7054a8e..1876f8b 100644 --- a/GAN/disco_gan/discogan_pytorch.py +++ b/GAN/disco_gan/discogan_pytorch.py @@ -159,7 +159,7 @@ def sample_x(X, size): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) input_A = sample_x(X_train1, size=4) input_B = sample_x(X_train2, size=4) diff --git a/GAN/dual_gan/dualgan_pytorch.py b/GAN/dual_gan/dualgan_pytorch.py index 8c3ed45..23234c3 100644 --- a/GAN/dual_gan/dualgan_pytorch.py +++ b/GAN/dual_gan/dualgan_pytorch.py @@ -154,7 +154,7 @@ def sample_x(X, size): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D1_loss.data[0] + D2_loss.data[0], G_loss.data[0])) + .format(it, D1_loss.item() + D2_loss.item(), G_loss.item())) real1 = X1.data.numpy()[:4] real2 = X2.data.numpy()[:4] diff --git a/GAN/ebgan/ebgan_pytorch.py b/GAN/ebgan/ebgan_pytorch.py index f6acbbe..e0d69a8 100644 --- a/GAN/ebgan/ebgan_pytorch.py +++ b/GAN/ebgan/ebgan_pytorch.py @@ -85,7 +85,7 @@ def reset_grad(): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) samples = G(z).data.numpy()[:16] diff --git a/GAN/f_gan/f_gan_pytorch.py b/GAN/f_gan/f_gan_pytorch.py index 8e3505d..0993293 100644 --- a/GAN/f_gan/f_gan_pytorch.py +++ b/GAN/f_gan/f_gan_pytorch.py @@ -102,7 +102,7 @@ def reset_grad(): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) samples = G(z).data.numpy()[:16] diff --git a/GAN/generative_adversarial_parallelization/gap_pytorch.py b/GAN/generative_adversarial_parallelization/gap_pytorch.py index fc99529..8e5df13 100644 --- a/GAN/generative_adversarial_parallelization/gap_pytorch.py +++ b/GAN/generative_adversarial_parallelization/gap_pytorch.py @@ -113,7 +113,7 @@ def reset_grad(): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) # Pick G randomly G_rand = random.choice([G1_, G2_]) diff --git a/GAN/gibbsnet/gibbsnet_pytorch.py b/GAN/gibbsnet/gibbsnet_pytorch.py index 5743650..cf1cc9c 100644 --- a/GAN/gibbsnet/gibbsnet_pytorch.py +++ b/GAN/gibbsnet/gibbsnet_pytorch.py @@ -59,7 +59,6 @@ def reset_grad(): P.zero_grad() D_.zero_grad() - G_solver = optim.Adam(chain(Q.parameters(), P.parameters()), lr=lr) D_solver = optim.Adam(D_.parameters(), lr=lr) @@ -99,7 +98,7 @@ def reset_grad(): # Print and plot every now and then if it % 100 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) z = Variable(torch.randn(mb_size, z_dim)) diff --git a/GAN/least_squares_gan/lsgan_pytorch.py b/GAN/least_squares_gan/lsgan_pytorch.py index 47cd083..9365993 100644 --- a/GAN/least_squares_gan/lsgan_pytorch.py +++ b/GAN/least_squares_gan/lsgan_pytorch.py @@ -79,7 +79,7 @@ def reset_grad(): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) samples = G(z).data.numpy()[:16] diff --git a/GAN/magan/magan_pytorch.py b/GAN/magan/magan_pytorch.py index 6bff40a..8d7e6b3 100644 --- a/GAN/magan/magan_pytorch.py +++ b/GAN/magan/magan_pytorch.py @@ -68,11 +68,11 @@ def reset_grad(): reset_grad() if it % 1000 == 0: - print('Iter-{}; Pretrained D loss: {:.4}'.format(it, loss.data[0])) + print('Iter-{}; Pretrained D loss: {:.4}'.format(it, loss.item())) # Initial margin, expected energy of real data -m = torch.mean(D(Variable(torch.from_numpy(mnist.train.images)))).data[0] +m = torch.mean(D(Variable(torch.from_numpy(mnist.train.images)))).item() s_z_before = torch.from_numpy(np.array([np.inf], dtype='float32')) diff --git a/GAN/softmax_gan/softmax_gan_pytorch.py b/GAN/softmax_gan/softmax_gan_pytorch.py index 0a7ac7b..9fc48cc 100644 --- a/GAN/softmax_gan/softmax_gan_pytorch.py +++ b/GAN/softmax_gan/softmax_gan_pytorch.py @@ -82,7 +82,7 @@ def reset_grad(): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0])) + .format(it, D_loss.item(), G_loss.item())) samples = G(z).data.numpy()[:16] diff --git a/VAE/adversarial_autoencoder/aae_pytorch.py b/VAE/adversarial_autoencoder/aae_pytorch.py index fc1080c..0e11698 100644 --- a/VAE/adversarial_autoencoder/aae_pytorch.py +++ b/VAE/adversarial_autoencoder/aae_pytorch.py @@ -109,7 +109,7 @@ def sample_X(size, include_y=False): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; D_loss: {:.4}; G_loss: {:.4}; recon_loss: {:.4}' - .format(it, D_loss.data[0], G_loss.data[0], recon_loss.data[0])) + .format(it, D_loss.item(), G_loss.item(), recon_loss.item())) samples = P(z_real).data.numpy()[:16] diff --git a/VAE/adversarial_vb/avb_pytorch.py b/VAE/adversarial_vb/avb_pytorch.py index af2bb8f..967ce82 100644 --- a/VAE/adversarial_vb/avb_pytorch.py +++ b/VAE/adversarial_vb/avb_pytorch.py @@ -106,7 +106,7 @@ def sample_X(size, include_y=False): # Print and plot every now and then if it % 1000 == 0: print('Iter-{}; ELBO: {:.4}; T_loss: {:.4}' - .format(it, -elbo.data[0], -T_loss.data[0])) + .format(it, -elbo.item(), -T_loss.item())) samples = P(z).data.numpy()[:16] diff --git a/VAE/conditional_vae/cvae_pytorch.py b/VAE/conditional_vae/cvae_pytorch.py index 76a9f36..f689955 100644 --- a/VAE/conditional_vae/cvae_pytorch.py +++ b/VAE/conditional_vae/cvae_pytorch.py @@ -103,7 +103,7 @@ def P(z, c): # Print and plot every now and then if it % 1000 == 0: - print('Iter-{}; Loss: {:.4}'.format(it, loss.data[0])) + print('Iter-{}; Loss: {:.4}'.format(it, loss.item())) c = np.zeros(shape=[mb_size, y_dim], dtype='float32') c[:, np.random.randint(0, 10)] = 1. diff --git a/VAE/denoising_vae/dvae_pytorch.py b/VAE/denoising_vae/dvae_pytorch.py index dfa1807..f60e901 100644 --- a/VAE/denoising_vae/dvae_pytorch.py +++ b/VAE/denoising_vae/dvae_pytorch.py @@ -102,7 +102,7 @@ def P(z): # Print and plot every now and then if it % 1000 == 0: - print('Iter-{}; Loss: {:.4}'.format(it, loss.data[0])) + print('Iter-{}; Loss: {:.4}'.format(it, loss.item())) z = Variable(torch.randn(mb_size, Z_dim)) samples = P(z).data.numpy()[:16] diff --git a/VAE/vanilla_vae/vae_pytorch.py b/VAE/vanilla_vae/vae_pytorch.py index fb09577..99dbff5 100644 --- a/VAE/vanilla_vae/vae_pytorch.py +++ b/VAE/vanilla_vae/vae_pytorch.py @@ -100,7 +100,7 @@ def P(z): # Print and plot every now and then if it % 1000 == 0: - print('Iter-{}; Loss: {:.4}'.format(it, loss.data[0])) + print('Iter-{}; Loss: {:.4}'.format(it, loss.item())) samples = P(z).data.numpy()[:16]