Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the deprecated torch.Varialbe api into torch.tensor with requires_grad ==true #69

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 15 additions & 17 deletions VAE/vanilla_vae/vae_pytorch.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
import os
from torch.autograd import Variable
from tensorflow.examples.tutorials.mnist import input_data


Expand All @@ -23,19 +22,20 @@
def xavier_init(size):
in_dim = size[0]
xavier_stddev = 1. / np.sqrt(in_dim / 2.)
return Variable(torch.randn(*size) * xavier_stddev, requires_grad=True)
n=np.random.randn(*size)*xavier_stddev
return torch.tensor(n, requires_grad=True,dtype=torch.float32)


# =============================== Q(z|X) ======================================

Wxh = xavier_init(size=[X_dim, h_dim])
bxh = Variable(torch.zeros(h_dim), requires_grad=True)
bxh = torch.zeros(h_dim, requires_grad=True)

Whz_mu = xavier_init(size=[h_dim, Z_dim])
bhz_mu = Variable(torch.zeros(Z_dim), requires_grad=True)
bhz_mu = torch.zeros(Z_dim, requires_grad=True)

Whz_var = xavier_init(size=[h_dim, Z_dim])
bhz_var = Variable(torch.zeros(Z_dim), requires_grad=True)
bhz_var = torch.zeros(Z_dim, requires_grad=True)


def Q(X):
Expand All @@ -46,17 +46,17 @@ def Q(X):


def sample_z(mu, log_var):
eps = Variable(torch.randn(mb_size, Z_dim))
eps = torch.randn(mb_size, Z_dim)
return mu + torch.exp(log_var / 2) * eps


# =============================== P(X|z) ======================================

Wzh = xavier_init(size=[Z_dim, h_dim])
bzh = Variable(torch.zeros(h_dim), requires_grad=True)
bzh = torch.zeros(h_dim, requires_grad=True)

Whx = xavier_init(size=[h_dim, X_dim])
bhx = Variable(torch.zeros(X_dim), requires_grad=True)
bhx = torch.zeros(X_dim, requires_grad=True)


def P(z):
Expand All @@ -74,7 +74,7 @@ def P(z):

for it in range(100000):
X, _ = mnist.train.next_batch(mb_size)
X = Variable(torch.from_numpy(X))
X = torch.from_numpy(X)

# Forward
z_mu, z_var = Q(X)
Expand All @@ -85,23 +85,21 @@ def P(z):
recon_loss = nn.binary_cross_entropy(X_sample, X, size_average=False) / mb_size
kl_loss = torch.mean(0.5 * torch.sum(torch.exp(z_var) + z_mu**2 - 1. - z_var, 1))
loss = recon_loss + kl_loss

# Backward
loss.backward()

# Update
solver.step()

solver.zero_grad()
# Housekeeping
for p in params:
if p.grad is not None:
data = p.grad.data
p.grad = Variable(data.new().resize_as_(data).zero_())
# for p in params:
# if p.grad is not None:
# data = p.grad.data
# p.grad = data.new().resize_as_(data).zero_()

# 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.data.item()))
samples = P(z).data.numpy()[:16]

fig = plt.figure(figsize=(4, 4))
Expand Down