16.03.2021 Views

Advanced Deep Learning with Keras

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Variational Autoencoders (VAEs)

VAEs in Keras

The structure of VAE bears a resemblance to a typical autoencoder. The

difference is mainly on the sampling of the Gaussian random variables in the

reparameterization trick. Listing 8.1.1 shows the encoder, decoder, and VAE which

are implemented using MLP. This code has also been contributed to the official

Keras GitHub repository. For simplicity of the discussion, the latent vector z is 2-dim.

The encoder is just a two-layer MLP with the second layer generating the mean and

log variance. The use of log variance is for simplicity in the computation of KL Loss

and reparameterization trick. The third output of the encoder is the sampling of z

using the reparameterization trick. We should note that in the sampling function,

2 2

e0.5logσ = σ = σ since σ > 0 given that it's the standard deviation of the Gaussian

distribution.

The decoder is also a two-layer MLP that takes samples of z to approximate

the inputs. Both the encoder and the decoder use an intermediate dimension

with a size of 512.

The VAE network is simply both the encoder and the decoder joined together.

Figures 8.1.3 to 8.1.5 show the encoder, decoder, and VAE models. The loss function

is the sum of both the Reconstruction Loss and KL Loss. The VAE network has good

results on the default Adam optimizer. The total number of parameters of the VAE

network is 807,700.

The Keras code for VAE MLP has pretrained weights. To test, we need to run:

$ python3 vae-mlp-mnist-8.1.1.py --weights=vae_mlp_mnist.h5

The complete code can be found on the following link: https://

github.com/PacktPublishing/Advanced-Deep-Learningwith-Keras.

Listing 8.1.1, vae-mlp-mnist-8.1.1.py shows us the Keras code of VAE using MLP

layers:

# reparameterization trick

# instead of sampling from Q(z|X), sample eps = N(0,I)

# z = z_mean + sqrt(var)*eps

def sampling(args):

z_mean, z_log_var = args

batch = K.shape(z_mean)[0]

# K is the keras backend

dim = K.int_shape(z_mean)[1]

# by default, random_normal has mean=0 and std=1.0

[ 244 ]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!