Creating the Network from TensorFlow 2
Learn how to prepare and build the variational autoencoder model.
We'll cover the following...
Now that we’ve downloaded the CIFAR-10 dataset, split it into test and training data, and reshaped and rescaled it, we are ready to build our VAE model. We’ll use the same Model API from the Keras module in TensorFlow 2. The TensorFlow documentation contains an example of how to implement a VAE using convolutional networks, and we’ll build on this code example. However, for our purposes, we’ll implement simpler VAE networks using MLP layers based on the original VAE paper, “Auto-encoding Variational
In the original article, the authors propose two kinds of models for use in the VAE, both MLP feedforward networks: Gaussian and Bernoulli. These names reflect the probability distribution functions used in the MLP network outputs in their final layers.
Bernoulli MLP
The Bernoulli MLP can be used as the network decoder, generating the simulated image
Where the first line is the cross-entropy function we use to determine if the network generates an approximation of the original image in reconstruction, while
We can easily create this Bernoulli MLP network using the Keras API:
class BernoulliMLP(tf.keras.Model):def __init__(self, input_shape, name='BernoulliMLP', hidden_dim=10, latent_dim=10, **kwargs):super().__init__(name=name, **kwargs)self._h = tf.keras.layers.Dense(hidden_dim, activation='tanh')self._y = tf.keras.layers.Dense(latent_dim, activation='sigmoid')def call(self, x):return self._y(self._h(x)), None, None
Line 4: We define a dense layer with
hidden_dim
neurons and a hyperbolic tangent (tanh) activation function.Line 5: We define the output layer with
latent_dim
neurons and sigmoid activation function.Lines 7–8: We define the
call
method which implements the forward pass of the model. It takes an inputx
and passes it through the hidden layer_h
followed by the output layer_y
. It returns three values:The output of the output layer
_y
, representing the reconstructed data.None
, indicating that no mean vector is produced.None
, indicating that no log variance vector is produced.
We just need to specify the dimensions of the single hidden layer and the latent output z
. We then specify the forward pass as a composition of these two layers. This is because, in our end model, we could use either the BernoulliMLP
or GaussianMLP
as the decoder. If we used the GaussianMLP
, we return three values, as we'll see below. We utilize a binary output and cross-entropy loss, so we can use just the single output, but we want the return signatures for the two decoders to match.
Gaussian MLP
The second network type proposed by the authors in the original VAE paper was a Gaussian MLP, whose formulas are as follows:
This network can be used as either the encoder (generating the latent vector