Complete the code to define the encoder input layer for the VAE.
encoder_inputs = tf.keras.Input(shape=[1])The input shape for the encoder is (28, 28, 1) because the VAE processes grayscale images of size 28x28 pixels.
Complete the code to sample the latent vector z using the reparameterization trick.
z = [1](mu, log_var)The function reparameterize is used to sample z from the latent distribution using mu and log_var.
Fix the error in the loss function by completing the KL divergence term.
kl_loss = -0.5 * tf.reduce_sum(1 + [1] - tf.square(mu) - tf.exp(log_var), axis=1)
The KL divergence term requires the log variance log_var to compute the loss correctly.
Fill both blanks to complete the decoder output layer and activation.
decoder_outputs = tf.keras.layers.Dense([1], activation=[2])(decoder_hidden)
The decoder output layer should have 784 units (28*28) with a sigmoid activation to output pixel probabilities.
Fill all three blanks to complete the training step function for the VAE.
with tf.GradientTape() as tape: reconstructed, mu, log_var = model(x) reconstruction_loss = tf.reduce_mean(tf.keras.losses.binary_crossentropy(x, reconstructed)) kl_loss = -0.5 * tf.reduce_mean(1 + [1] - tf.square([2]) - tf.exp([3])) total_loss = reconstruction_loss + kl_loss gradients = tape.gradient(total_loss, model.trainable_variables) optimizer.apply_gradients(zip(gradients, model.trainable_variables))
The KL loss uses log_var and mu to compute the divergence. The third blank is also log_var for the exponential term.
