Understanding Variational Autoencoders (VAE) Assignment

Download Solution Order New Solution

Assignment Task

Variational auto-encoder (VAE) utilities

I am providing you with some utility methods for training VAEs. This is done to minimize the overhead in getting you up and running and training cool neural networks.

The first function implements the crucial element for variational auto-encoders: The random sampling in the latent space. The sampling function below takes one argument, which is assumed to be a list with two elements: the means and the logarithm of the variances, respectively, of the latent probability density function (p.d.f.) for a given image example. The function then samples random numbers and scales them such that they are effectively drawn from a p.d.f. with means and standard deviations as given by args. These sampled coordinates in the latent space are then returned.

Build a VAE

1. Build the encoder of the VAE

Build an encoder. We will train for 50 epochs so a relatively fast NN may be desiriable. Here you have some freedom but some example structure is given:

  • The input layer has the dimension of the shape
  • Flat the input layer (either with or without convolution beforehand)
  • The 1 2 4 8 16 convolution example worked well for me but is a bit slower. add a few hidden Dense layers (with relu activation function) with 256 dimensions of hidden nodes
  • Add the 2 special layers: z_mean contains the means of the latent space p.d.f. for a given image, and z_log_var contains the logarithm of the assocated variances.
  • Pass these 2 layers into a layer z that just applies the sampling method above. Note: the sampling method is used to perform the reparametrization trick.
  • Lambda has to be used to ensure the gradients to be computed (see Lecture example)
  • Output the 3 layers (z_mean, z_log_var and z) build the encoder model with the Model method.
  • If you used convolution and pooling make sure you complient this with Upsampleling and ConvolutionTranspose.

2. Build the decoder of the VAE

Build a decoder:

  • The input layer has the latent space 2
  • Apply 2 hidden Dense layers with dimension 256 (same number as in the encoder) and relu activation function
  • Output sigmoid activation function such that it ends up with the same shape as the original image, with pixel intensities in the range .
  • Use as dimension of this layer np.prod(shape) , where shape is the latent space. then reshape the output to the original image shape with Reshape method.
  • This step is needed to bring back the data from shape (latent_dim, ) into some shape that can be reshaped into the output shape of the last layer of our encoder. build the decoder model with the Model method

3. Build the VAE model

Then, we can connect the encoder and the decoder.

  • Build the VAE model using Model method using i (encoder) and (decoder)
  • plot the model with the plot_model method

Compile and train the VAE model

The compile_vae methods compiles the VAE model passed as the sole argument. The method computes

1. the image reconstruction loss ( reco_loss ), as well as

2. the Kullback-Leibler (KL) loss ( kl_loss ) for the latent space p.d.f.

The first loss measures how well the VAE is doing at reconstructing the input images; the second loss quantifies how continuous the latent representation is. The two losses are then summed (and added as separate metrics for monitoring), and the VAE model is compiled with the Adam optimiser.

Generative Adversarial networks

Putting together a GAN by yourself is a big task and outside the scope of what we can cover. Instead here we provide the building blocks for creating the GAN on the MNIST dataset. Your task is to go through individual components and show your understanding. The source material can be found.

This IT and Computer Science has been solved by our PHD Experts at My Uni Paper.

Get It Done! Today

Country
Applicable Time Zone is AEST [Sydney, NSW] (GMT+11)
+

Every Assignment. Every Solution. Instantly. Deadline Ahead? Grab Your Sample Now.