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.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') plt.show()
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...