In the Keras VAE Tutorial (https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder.py), they sample digits from the VAE by choosing equally spaced points in the latent space, using the following code:

grid_x = norm.ppf(np.linspace(0.05, 0.95, n))
grid_y = norm.ppf(np.linspace(0.05, 0.95, n))

for i, yi in enumerate(grid_x):
    for j, xi in enumerate(grid_y):
        z_sample = np.array([[xi, yi]])
        x_decoded = generator.predict(z_sample)
        digit = x_decoded[0].reshape(digit_size, digit_size)
        figure[i * digit_size: (i + 1) * digit_size,
               j * digit_size: (j + 1) * digit_size] = digit

plt.figure(figsize=(10, 10))
plt.imshow(figure, cmap='Greys_r')

However, I want to sample points in the latent spaced, and their corresponding decoded values, based on their prior probability. It is clear that after training, the distribution of the z from the latent layer (which is given on line 41 to be z = Lambda(sampling, output_shape=(latent_dim,))([z_mean, z_log_var])) will not be uniform or normal. How would I do this? Right now, I am thinking of randomly sampling training data points, computing their image in the latent layer using the encoder, and then running the decoder on these latent points. Sampling the training data points and then running then through the encoder seems like the only way to approximate the latent distribution after training...


The prior probability $p(z)$ is defined to be a standard normal distribution, so sampling from that would be sufficient.

The distribution of the latent space over images in your dataset as estimated by the network, $q(z) = q(z|x)f(x)$, is a different matter, but in a well-trained VAE, it should approach $p(z)$ -- because it is encouraged to by the KL-divergence loss. (where $f(x)$ is the density of your training data).

However, unless you have some specific reason for wanting the latter, $p(z)$ is probably what you are looking for.

  • $\begingroup$ So does that simply mean z_sample = np.array([np.random.normal(size=latent_dimension)])? $\endgroup$ – ascripter May 31 '18 at 21:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.