-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathvae.py
More file actions
291 lines (241 loc) · 12.7 KB
/
vae.py
File metadata and controls
291 lines (241 loc) · 12.7 KB
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
from datetime import datetime
import os
import re
import sys
import numpy as np
import tensorflow as tf
from layers import Dense
import plot
from utils import composeAll, print_
class VAE():
"""Variational Autoencoder
see: Kingma & Welling - Auto-Encoding Variational Bayes
(http://arxiv.org/abs/1312.6114)
"""
DEFAULTS = {
"batch_size": 128,
"learning_rate": 1E-3,
"dropout": 1.,
"lambda_l2_reg": 0.,
"nonlinearity": tf.nn.elu,
"squashing": tf.nn.sigmoid
}
RESTORE_KEY = "to_restore"
def __init__(self, architecture=[], d_hyperparams={}, meta_graph=None,
save_graph_def=True, log_dir="./log"):
"""(Re)build a symmetric VAE model with given:
* architecture (list of nodes per encoder layer); e.g.
[1000, 500, 250, 10] specifies a VAE with 1000-D inputs, 10-D latents,
& end-to-end architecture [1000, 500, 250, 10, 250, 500, 1000]
* hyperparameters (optional dictionary of updates to `DEFAULTS`)
"""
self.architecture = architecture
self.__dict__.update(VAE.DEFAULTS, **d_hyperparams)
self.sesh = tf.Session()
if not meta_graph: # new model
self.datetime = datetime.now().strftime(r"%y%m%d_%H%M")
assert len(self.architecture) > 2, \
"Architecture must have more layers! (input, 1+ hidden, latent)"
# build graph
handles = self._buildGraph()
for handle in handles:
tf.add_to_collection(VAE.RESTORE_KEY, handle)
self.sesh.run(tf.initialize_all_variables())
else: # restore saved model
model_datetime, model_name = os.path.basename(meta_graph).split("_vae_")
self.datetime = "{}_reloaded".format(model_datetime)
*model_architecture, _ = re.split("_|-", model_name)
self.architecture = [int(n) for n in model_architecture]
# rebuild graph
meta_graph = os.path.abspath(meta_graph)
tf.train.import_meta_graph(meta_graph + ".meta").restore(
self.sesh, meta_graph)
handles = self.sesh.graph.get_collection(VAE.RESTORE_KEY)
# unpack handles for tensor ops to feed or fetch
(self.x_in, self.dropout_, self.z_mean, self.z_log_sigma,
self.x_reconstructed, self.z_, self.x_reconstructed_,
self.cost, self.global_step, self.train_op) = handles
if save_graph_def: # tensorboard
self.logger = tf.train.SummaryWriter(log_dir, self.sesh.graph)
@property
def step(self):
"""Train step"""
return self.global_step.eval(session=self.sesh)
def _buildGraph(self):
x_in = tf.placeholder(tf.float32, shape=[None, # enables variable batch size
self.architecture[0]], name="x")
dropout = tf.placeholder_with_default(1., shape=[], name="dropout")
# encoding / "recognition": q(z|x)
encoding = [Dense("encoding", hidden_size, dropout, self.nonlinearity)
# hidden layers reversed for function composition: outer -> inner
for hidden_size in reversed(self.architecture[1:-1])]
h_encoded = composeAll(encoding)(x_in)
# latent distribution parameterized by hidden encoding
# z ~ N(z_mean, np.exp(z_log_sigma)**2)
z_mean = Dense("z_mean", self.architecture[-1], dropout)(h_encoded)
z_log_sigma = Dense("z_log_sigma", self.architecture[-1], dropout)(h_encoded)
# kingma & welling: only 1 draw necessary as long as minibatch large enough (>100)
z = self.sampleGaussian(z_mean, z_log_sigma)
# decoding / "generative": p(x|z)
decoding = [Dense("decoding", hidden_size, dropout, self.nonlinearity)
for hidden_size in self.architecture[1:-1]] # assumes symmetry
# final reconstruction: restore original dims, squash outputs [0, 1]
decoding.insert(0, Dense( # prepend as outermost function
"x_decoding", self.architecture[0], dropout, self.squashing))
x_reconstructed = tf.identity(composeAll(decoding)(z), name="x_reconstructed")
# reconstruction loss: mismatch b/w x & x_reconstructed
# binary cross-entropy -- assumes x & p(x|z) are iid Bernoullis
rec_loss = VAE.crossEntropy(x_reconstructed, x_in)
# Kullback-Leibler divergence: mismatch b/w approximate vs. imposed/true posterior
kl_loss = VAE.kullbackLeibler(z_mean, z_log_sigma)
with tf.name_scope("l2_regularization"):
regularizers = [tf.nn.l2_loss(var) for var in self.sesh.graph.get_collection(
"trainable_variables") if "weights" in var.name]
l2_reg = self.lambda_l2_reg * tf.add_n(regularizers)
with tf.name_scope("cost"):
# average over minibatch
cost = tf.reduce_mean(rec_loss + kl_loss, name="vae_cost")
cost += l2_reg
# optimization
global_step = tf.Variable(0, trainable=False)
with tf.name_scope("Adam_optimizer"):
optimizer = tf.train.AdamOptimizer(self.learning_rate)
tvars = tf.trainable_variables()
grads_and_vars = optimizer.compute_gradients(cost, tvars)
clipped = [(tf.clip_by_value(grad, -5, 5), tvar) # gradient clipping
for grad, tvar in grads_and_vars]
train_op = optimizer.apply_gradients(clipped, global_step=global_step,
name="minimize_cost")
# ops to directly explore latent space
# defaults to prior z ~ N(0, I)
with tf.name_scope("latent_in"):
z_ = tf.placeholder_with_default(tf.random_normal([1, self.architecture[-1]]),
shape=[None, self.architecture[-1]],
name="latent_in")
x_reconstructed_ = composeAll(decoding)(z_)
return (x_in, dropout, z_mean, z_log_sigma, x_reconstructed,
z_, x_reconstructed_, cost, global_step, train_op)
def sampleGaussian(self, mu, log_sigma):
"""(Differentiably!) draw sample from Gaussian with given shape, subject to random noise epsilon"""
with tf.name_scope("sample_gaussian"):
# reparameterization trick
epsilon = tf.random_normal(tf.shape(log_sigma), name="epsilon")
return mu + epsilon * tf.exp(log_sigma) # N(mu, I * sigma**2)
@staticmethod
def crossEntropy(obs, actual, offset=1e-7):
"""Binary cross-entropy, per training example"""
# (tf.Tensor, tf.Tensor, float) -> tf.Tensor
with tf.name_scope("cross_entropy"):
# bound by clipping to avoid nan
obs_ = tf.clip_by_value(obs, offset, 1 - offset)
return -tf.reduce_sum(actual * tf.log(obs_) +
(1 - actual) * tf.log(1 - obs_), 1)
@staticmethod
def l1_loss(obs, actual):
"""L1 loss (a.k.a. LAD), per training example"""
# (tf.Tensor, tf.Tensor, float) -> tf.Tensor
with tf.name_scope("l1_loss"):
return tf.reduce_sum(tf.abs(obs - actual) , 1)
@staticmethod
def l2_loss(obs, actual):
"""L2 loss (a.k.a. Euclidean / LSE), per training example"""
# (tf.Tensor, tf.Tensor, float) -> tf.Tensor
with tf.name_scope("l2_loss"):
return tf.reduce_sum(tf.square(obs - actual), 1)
@staticmethod
def kullbackLeibler(mu, log_sigma):
"""(Gaussian) Kullback-Leibler divergence KL(q||p), per training example"""
# (tf.Tensor, tf.Tensor) -> tf.Tensor
with tf.name_scope("KL_divergence"):
# = -0.5 * (1 + log(sigma**2) - mu**2 - sigma**2)
return -0.5 * tf.reduce_sum(1 + 2 * log_sigma - mu**2 -
tf.exp(2 * log_sigma), 1)
def encode(self, x):
"""Probabilistic encoder from inputs to latent distribution parameters;
a.k.a. inference network q(z|x)
"""
# np.array -> [float, float]
feed_dict = {self.x_in: x}
return self.sesh.run([self.z_mean, self.z_log_sigma], feed_dict=feed_dict)
def decode(self, zs=None):
"""Generative decoder from latent space to reconstructions of input space;
a.k.a. generative network p(x|z)
"""
# (np.array | tf.Variable) -> np.array
feed_dict = dict()
if zs is not None:
is_tensor = lambda x: hasattr(x, "eval")
zs = (self.sesh.run(zs) if is_tensor(zs) else zs) # coerce to np.array
feed_dict.update({self.z_: zs})
# else, zs defaults to draw from conjugate prior z ~ N(0, I)
return self.sesh.run(self.x_reconstructed_, feed_dict=feed_dict)
def vae(self, x):
"""End-to-end autoencoder"""
# np.array -> np.array
return self.decode(self.sampleGaussian(*self.encode(x)))
def train(self, X, max_iter=np.inf, max_epochs=np.inf, cross_validate=True,
verbose=True, save=True, outdir="./out", plots_outdir="./png",
plot_latent_over_time=False):
if save:
saver = tf.train.Saver(tf.all_variables())
try:
err_train = 0
now = datetime.now().isoformat()[11:]
print("------- Training begin: {} -------\n".format(now))
if plot_latent_over_time: # plot latent space over log_BASE time
BASE = 2
INCREMENT = 0.5
pow_ = 0
while True:
x, _ = X.train.next_batch(self.batch_size)
feed_dict = {self.x_in: x, self.dropout_: self.dropout}
fetches = [self.x_reconstructed, self.cost, self.global_step, self.train_op]
x_reconstructed, cost, i, _ = self.sesh.run(fetches, feed_dict)
err_train += cost
if plot_latent_over_time:
while int(round(BASE**pow_)) == i:
plot.exploreLatent(self, nx=30, ny=30, ppf=True, outdir=plots_outdir,
name="explore_ppf30_{}".format(pow_))
names = ("train", "validation", "test")
datasets = (X.train, X.validation, X.test)
for name, dataset in zip(names, datasets):
plot.plotInLatent(self, dataset.images, dataset.labels, range_=
(-6, 6), title=name, outdir=plots_outdir,
name="{}_{}".format(name, pow_))
print("{}^{} = {}".format(BASE, pow_, i))
pow_ += INCREMENT
if i%1000 == 0 and verbose:
print("round {} --> avg cost: ".format(i), err_train / i)
if i%2000 == 0 and verbose:# and i >= 10000:
# visualize `n` examples of current minibatch inputs + reconstructions
plot.plotSubset(self, x, x_reconstructed, n=10, name="train",
outdir=plots_outdir)
if cross_validate:
x, _ = X.validation.next_batch(self.batch_size)
feed_dict = {self.x_in: x}
fetches = [self.x_reconstructed, self.cost]
x_reconstructed, cost = self.sesh.run(fetches, feed_dict)
print("round {} --> CV cost: ".format(i), cost)
plot.plotSubset(self, x, x_reconstructed, n=10, name="cv",
outdir=plots_outdir)
if i >= max_iter or X.train.epochs_completed >= max_epochs:
print("final avg cost (@ step {} = epoch {}): {}".format(
i, X.train.epochs_completed, err_train / i))
now = datetime.now().isoformat()[11:]
print("------- Training end: {} -------\n".format(now))
if save:
outfile = os.path.join(os.path.abspath(outdir), "{}_vae_{}".format(
self.datetime, "_".join(map(str, self.architecture))))
saver.save(self.sesh, outfile, global_step=self.step)
try:
self.logger.flush()
self.logger.close()
except(AttributeError): # not logging
continue
break
except(KeyboardInterrupt):
print("final avg cost (@ step {} = epoch {}): {}".format(
i, X.train.epochs_completed, err_train / i))
now = datetime.now().isoformat()[11:]
print("------- Training end: {} -------\n".format(now))
sys.exit(0)