-
Notifications
You must be signed in to change notification settings - Fork 11
/
vq_vae.py
347 lines (311 loc) · 12.7 KB
/
vq_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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
import warnings
from typing import List, Optional
import numpy as np
import tensorflow as tf
from tensorflow.python import keras
from tensorflow.python.keras.layers import Layer
from tensorflow_probability.python.distributions import (Distribution,
Multinomial)
from tensorflow_probability.python.layers import DistributionLambda
from odin.bay.distributions import VectorQuantized
from odin.bay.helpers import KLdivergence
from odin.bay.vi.autoencoder.beta_vae import BetaVAE
from odin.bay.vi.autoencoder.variational_autoencoder import TrainStep
# ===========================================================================
# Helpers
# ===========================================================================
class VQVAEStep(TrainStep):
def __call__(self):
vae: VQVAE = self.vae
assert isinstance(vae, VQVAE), \
f"VQVAEStep only applied for VQVAE but, given VAE type: {type(vae)}"
pX_Z, qZ_X = vae(self.inputs,
training=self.training,
mask=self.mask,
sample_shape=self.sample_shape,
**self.call_kw)
## Calculate the ELBO
llk, div = self.vae.elbo(self.inputs,
pX_Z,
qZ_X,
training=self.training,
mask=self.mask,
return_components=True,
**self.elbo_kw)
# sum all the components log-likelihood and divergence
llk_sum = tf.constant(0., dtype=self.vae.dtype)
div_sum = tf.constant(0., dtype=self.vae.dtype)
for x in llk.values():
llk_sum += x
for x in div.values():
div_sum += x
elbo = llk_sum - div_sum
if self.iw and tf.rank(elbo) > 1:
elbo = self.vae.importance_weighted(elbo, axis=0)
loss = -tf.reduce_mean(elbo)
# metrics
metrics = llk
metrics.update(div)
## update the codebook
if self.training:
vae.quantizer.update_codebook(qZ_X)
return loss, metrics
class VectorQuantizer(Layer):
r"""
Arguments:
n_codes : int (default=64),
Number of discrete codes in codebook.
input_ndim : int (default=1),
Number of dimension for a single input example.
"""
def __init__(self,
n_codes: int = 64,
commitment_weight: float = 0.25,
distance_metric: str = 'euclidean',
trainable_prior=False,
ema_decay: float = 0.99,
ema_update: bool = False,
epsilon: float = 1e-5,
name: str = "VectorQuantizer"):
super().__init__(name=name)
self.n_codes = int(n_codes)
self.distance_metric = str(distance_metric)
self.trainable_prior = bool(trainable_prior)
self.commitment_weight = tf.convert_to_tensor(commitment_weight,
dtype=self.dtype)
self.ema_decay = tf.convert_to_tensor(ema_decay, dtype=self.dtype)
self.ema_update = bool(ema_update)
self.epsilon = tf.convert_to_tensor(epsilon, dtype=self.dtype)
def build(self, input_shape):
self.input_ndim = len(input_shape) - 2
self.code_size = input_shape[-1]
self.event_size = int(np.prod(input_shape[1:]))
self.codebook: tf.Variable = self.add_weight(
name="codebook",
shape=[self.n_codes, self.code_size],
initializer=tf.initializers.variance_scaling(distribution="uniform"),
dtype=tf.float32,
trainable=not self.ema_update)
# exponential moving average
if self.ema_update:
self.ema_counts = self.add_weight(name="ema_counts",
shape=[self.n_codes],
initializer=tf.initializers.constant(0),
trainable=False)
self.ema_means = self.add_weight(name="ema_means",
initializer=tf.initializers.constant(0),
shape=self.codebook.shape,
trainable=False)
self.ema_means.assign(self.codebook)
# create the prior and posterior
prior_logits = self.add_weight(
name="prior_logits",
shape=input_shape[1:-1] + [self.n_codes],
dtype=self.dtype,
initializer=tf.initializers.constant(0),
trainable=self.trainable_prior,
)
self._prior = Multinomial(total_count=1.0,
logits=prior_logits,
name="VectorQuantizerPrior")
self._posterior = DistributionLambda(
make_distribution_fn=lambda params: VectorQuantized(
codes=params[0],
assignments=params[1],
nearest_codes=params[2],
commitment=self.commitment_weight),
convert_to_tensor_fn=Distribution.sample)
return super().build(input_shape)
@property
def prior(self) -> Distribution:
return self._prior
@property
def posterior(self) -> DistributionLambda:
return self._posterior
def call(self, codes, training=None, *args, **kwargs) -> VectorQuantized:
r""" Uses codebook to find nearest neighbor for each code.
Args:
codes: A `float`-like `Tensor`,
containing the latent vectors to be compared to the codebook.
These are rank-3 with shape `[batch_size, ..., code_size]`.
Returns:
codes_straight_through: A `float`-like `Tensor`,
the nearest entries with stopped gradient to the codebook,
shape `[batch_size, ..., code_size]`.
"""
indices = self.sample_indices(codes, one_hot=False)
nearest_codebook_entries = self.sample_nearest(indices)
dist: VectorQuantized = self.posterior(
(
codes,
tf.one_hot(indices, depth=self.n_codes, axis=-1),
nearest_codebook_entries,
),
training=training,
)
dist.KL_divergence = KLdivergence(posterior=dist,
prior=self.prior,
analytic=True)
# tf.debugging.assert_near(dist,
# nearest_codebook_entries,
# rtol=1e-5,
# atol=1e-5)
return dist
def sample_indices(self, codes, one_hot=True) -> tf.Tensor:
r""" Uses codebook to find nearest neighbor index for each code.
Args:
codes: A `float`-like `Tensor`,
containing the latent vectors to be compared to the codebook.
These are rank-3 with shape `[batch_size, ..., code_size]`.
Returns:
one_hot_assignments: a Tensor with shape `[batch_size, ..., n_codes]`
The one-hot vectors corresponding to the matched codebook entry for
each code in the batch.
"""
tf.assert_equal(tf.shape(codes)[-1], self.code_size)
input_shape = tf.shape(codes)
codes = tf.reshape(codes, [-1, self.code_size])
codebook = tf.transpose(self.codebook)
distances = (tf.reduce_sum(codes**2, 1, keepdims=True) -
2 * tf.matmul(codes, codebook) +
tf.reduce_sum(codebook**2, 0, keepdims=True))
assignments = tf.argmax(-distances, axis=1)
assignments = tf.reshape(assignments, input_shape[:-1])
if one_hot:
assignments = tf.one_hot(assignments, depth=self.n_codes, axis=-1)
return assignments
def sample_nearest(self, indices) -> tf.Tensor:
r""" Sample from the code book the nearest codes based on calculated
one-hot assignments.
Args:
indices: A `int`-like `Tensor` containing the assignment
vectors, shape `[batch_size, ...]`.
Returns:
nearest_codebook_entries: a Tensor with shape `[batch_size, ..., code_size]`
The 1-nearest neighbor in Euclidean distance for each code in the batch.
"""
return tf.nn.embedding_lookup(self.codebook, indices)
def sample(self, sample_shape=(), seed=None) -> tf.Tensor:
r""" Sampling from Multinomial prior """
# [batch_dims, ..., n_codes]
sample_shape = tf.nest.flatten(sample_shape)
if len(sample_shape) == 0:
sample_shape = [1]
samples = self.prior.sample(sample_shape, seed)
codebook = tf.reshape(self.codebook, [1] * (self.input_ndim + 1) +
[self.n_codes, self.code_size])
return tf.reduce_sum(tf.expand_dims(samples, -1) * codebook, axis=-2)
def update_codebook(self, vq_dist: VectorQuantized):
assert isinstance(vq_dist, VectorQuantized), \
("vq_dist must be instance of VectorQuantized distribution, "
f"but given: {type(vq_dist)}")
assert self.ema_update, \
"Exponential moving average update is not enable for VectorQuantizer"
vq_dist.update_codebook(codebook=self.codebook,
counts=self.ema_counts,
means=self.ema_means,
decay=self.ema_decay,
epsilon=self.epsilon)
return self
def __str__(self):
if self.built:
input_shape = self.input_shape[1:]
else:
input_shape = None
return (f"<VectorQuantizer input:{input_shape}"
f" codebook:({self.n_codes}, {self.code_size})"
f" commitment:{self.commitment_weight}"
f" ema:(enable={self.ema_update}, decay={self.decay})"
f" metric:{self.distance_metric}>")
# ===========================================================================
# Main
# ===========================================================================
class VQVAE(BetaVAE):
r"""
Arguments:
commitment_loss : float (default=0.25),
or `beta`, weight for the commitment loss
Reference:
Aaron van den Oord, Oriol Vinyals, Koray Kavukcuoglu. "Neural Discrete
Representation Learning". In _Conference on Neural Information Processing
Systems_, 2017. https://arxiv.org/abs/1711.00937
"""
def __init__(self,
n_codes: int = 64,
commitment_weight: float = 0.25,
distance_metric: str = 'euclidean',
trainable_prior: bool = False,
ema_decay: float = 0.99,
ema_update=False,
beta=1.0,
**kwargs):
latents = kwargs.pop('latents', None)
if latents is not None and not isinstance(latents, VectorQuantizer):
warnings.warn(
f"VQVAE uses VectorQuantizer latents, ignore: {type(latents)}")
latents = VectorQuantizer(n_codes=n_codes,
commitment_weight=commitment_weight,
trainable_prior=trainable_prior,
distance_metric=distance_metric,
ema_decay=ema_decay,
ema_update=ema_update,
name="VQLatents")
analytic = kwargs.pop('analytic', True)
if not analytic:
raise ValueError("VQVAE only support analytic KL-divergence.")
super().__init__(beta=beta, latents=latents, analytic=analytic, **kwargs)
self._quantizer = latents
@property
def quantizer(self) -> VectorQuantizer:
return self._quantizer
@property
def codebook(self) -> tf.Variable:
return self._quantizer.codebook
@property
def ema_update(self) -> bool:
return self._quantizer.ema_update
@property
def ema_counts(self) -> tf.Variable:
return self._quantizer.ema_counts
@property
def ema_means(self) -> tf.Variable:
return self._quantizer.ema_means
def _elbo(self, inputs, pX_Z, qZ_X, analytic, reverse, sample_shape, mask,
training, **kwargs):
llk, div = super()._elbo(inputs,
pX_Z,
qZ_X,
analytic,
reverse,
sample_shape=sample_shape,
mask=mask,
training=training,
**kwargs)
for i, (qzx, name) in enumerate(zip(qZ_X, self.latent_names)):
if isinstance(qzx, VectorQuantized):
qzx: VectorQuantized
if not self.ema_update:
div[f'latents_{name}'] = qzx.latents_loss
div[f'commitment_{name}'] = qzx.commitment_loss
return llk, div
def train_steps(self,
inputs,
training=True,
mask=None,
sample_shape=(),
iw=False,
elbo_kw={},
call_kw={}) -> VQVAEStep:
self.step.assign_add(1)
args = dict(vae=self,
inputs=inputs,
training=training,
mask=mask,
sample_shape=sample_shape,
iw=iw,
elbo_kw=elbo_kw,
call_kw=call_kw)
if self.quantizer.ema_update:
yield VQVAEStep(**args)
else:
yield TrainStep(**args)