-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathconcrete_vae.py
165 lines (133 loc) · 5.83 KB
/
concrete_vae.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
from __future__ import print_function
import argparse
import numpy as np
import torch
import torch.utils.data
from torch import nn, optim
from torch.nn import functional as F
from torchvision import datasets, transforms
from torchvision.utils import save_image
import torch.distributions as td
parser = argparse.ArgumentParser(description='VAE MNIST Example')
parser.add_argument('--batch-size', type=int, default=128, metavar='N',
help='input batch size for training (default: 128)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='enables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
args = parser.parse_args()
args.cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if args.cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if args.cuda else {}
train_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=True, download=True,
transform=transforms.ToTensor()),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.MNIST('../data', train=False, transform=transforms.ToTensor()),
batch_size=args.batch_size, shuffle=True, **kwargs)
INITIAL_TEMP = 1.0
ANNEAL_RATE = 0.00003
MIN_TEMP = 0.1
K = 10 # Number of classes
N = 20 # Number of categorical distributions
temp = INITIAL_TEMP
steps = 0
class VAE(nn.Module):
def __init__(self):
super(VAE, self).__init__()
self.fc1 = nn.Linear(784, 400)
self.fc2 = nn.Linear(400, N*K)
self.fc3 = nn.Linear(N*K, 400)
self.fc4 = nn.Linear(400, 784)
def encode(self, x):
h1 = F.relu(self.fc1(x))
return self.fc2(h1).reshape(-1, N, K)
def decode(self, z):
h3 = F.relu(self.fc3(z.view(-1, N*K)))
return torch.sigmoid(self.fc4(h3))
def forward(self, x, temp=1.0, hard=False):
logits = self.encode(x.view(-1, 784))
q_z = td.relaxed_categorical.RelaxedOneHotCategorical(temp, logits=logits) # create a torch distribution
probs = q_z.probs
z = q_z.rsample() # sample with reparameterization
if hard:
# No step function in torch, so using sign instead
z_hard = 0.5 * (torch.sign(z) + 1)
z = z + (z_hard - z).detach()
return self.decode(z), probs
model = VAE().to(device)
optimizer = optim.Adam(model.parameters(), lr=1e-3)
# Reconstruction + KL divergence losses summed over all elements and batch
def loss_function(recon_x, x, probs, eps=1e-10):
BCE = F.binary_cross_entropy(recon_x, x.view(-1, 784), reduction='sum')
# You can also compute p(x|z) as below, for binary output it reduces
# to binary cross entropy error, for gaussian output it reduces to
# mean square error
# p_x = td.bernoulli.Bernoulli(logits=recon_x)
# BCE = -p_x.log_prob(x.view(-1, 784)).sum()
# see Appendix B from VAE paper:
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
# https://arxiv.org/abs/1312.6114
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
# KLD = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
prior = 1 / K
t1 = probs * ((probs + eps) / prior).log()
KLD = torch.sum(t1, dim=-1).sum()
return BCE + KLD
def train(epoch):
global temp, steps
model.train()
train_loss = 0
for batch_idx, (data, _) in enumerate(train_loader):
data = data.to(device)
optimizer.zero_grad()
recon_batch, q_z = model(data, temp=temp)
loss = loss_function(recon_batch, data, q_z)
loss.backward()
train_loss += loss.item()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader),
loss.item() / len(data)))
steps += 1
if steps % 1000 == 0:
temp = max(temp * np.exp(-ANNEAL_RATE * steps), MIN_TEMP)
print('====> Epoch: {} Average loss: {:.4f}'.format(
epoch, train_loss / len(train_loader.dataset)))
def test(epoch):
global temp
model.eval()
test_loss = 0
with torch.no_grad():
for i, (data, _) in enumerate(test_loader):
data = data.to(device)
recon_batch, q_z = model(data, temp=temp)
test_loss += loss_function(recon_batch, data, q_z).item()
if i == 0:
n = min(data.size(0), 8)
comparison = torch.cat([data[:n],
recon_batch.view(args.batch_size, 1, 28, 28)[:n]])
save_image(comparison.cpu(),
'results/reconstruction_' + str(epoch) + '.png', nrow=n)
test_loss /= len(test_loader.dataset)
print('====> Test set loss: {:.4f}'.format(test_loss))
if __name__ == "__main__":
for epoch in range(1, args.epochs + 1):
train(epoch)
test(epoch)
with torch.no_grad():
cat_sample = np.random.randint(K, size=(64, N))
onehot_sample = np.zeros((64, N, K))
onehot_sample[tuple(list(np.indices(onehot_sample.shape[:-1])) + [cat_sample])] = 1
sample = torch.from_numpy(np.float32(onehot_sample)).to(device)
sample = model.decode(sample).cpu()
save_image(sample.view(64, 1, 28, 28),
'results/sample_' + str(epoch) + '.png')