Challenge - 5 Problems
VAE Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
🧠 Conceptual
intermediateWhat is the main purpose of the KL divergence term in a VAE loss?
In Variational Autoencoders, the loss function includes a reconstruction loss and a KL divergence term. What does the KL divergence term do?
Attempts:
2 left
💡 Hint
Think about how the latent space is shaped in a VAE.
✗ Incorrect
The KL divergence term forces the learned latent distribution to be close to a prior (usually a standard normal), which helps the model generate meaningful samples and ensures smooth latent space.
❓ Predict Output
intermediateWhat is the shape of the latent vector z in this VAE encoder output?
Given the following TensorFlow code snippet for the encoder part of a VAE, what is the shape of the sampled latent vector z?
TensorFlow
import tensorflow as tf from tensorflow.keras import layers class Encoder(tf.keras.Model): def __init__(self, latent_dim): super().__init__() self.dense1 = layers.Dense(64, activation='relu') self.dense_mean = layers.Dense(latent_dim) self.dense_log_var = layers.Dense(latent_dim) def call(self, x): x = self.dense1(x) mean = self.dense_mean(x) log_var = self.dense_log_var(x) epsilon = tf.random.normal(shape=tf.shape(mean)) z = mean + tf.exp(0.5 * log_var) * epsilon return z encoder = Encoder(latent_dim=16) input_tensor = tf.random.normal(shape=(32, 100)) z = encoder(input_tensor) print(z.shape)
Attempts:
2 left
💡 Hint
The batch size is 32 and latent_dim is 16.
✗ Incorrect
The input batch has 32 samples, and the latent dimension is set to 16, so the output z shape is (32, 16).
❓ Model Choice
advancedWhich model architecture is best suited for image data in a VAE?
You want to build a VAE to generate 28x28 grayscale images. Which encoder architecture is most appropriate?
Attempts:
2 left
💡 Hint
Think about spatial structure in images.
✗ Incorrect
Convolutional layers capture spatial features in images better than fully connected or recurrent layers, making them ideal for image VAEs.
❓ Hyperparameter
advancedWhat effect does increasing the latent dimension size have on a VAE?
If you increase the latent dimension size in a VAE, what is the most likely effect?
Attempts:
2 left
💡 Hint
Think about model capacity and overfitting.
✗ Incorrect
A larger latent space allows encoding more information but can cause overfitting and requires more training time.
❓ Metrics
expertHow to correctly compute total VAE loss in TensorFlow?
Given reconstruction loss computed as mean squared error and KL divergence computed per batch, which code snippet correctly computes the total loss for training a VAE?
TensorFlow
import tensorflow as tf reconstruction_loss = tf.reduce_mean(tf.square(inputs - outputs)) kl_loss = -0.5 * tf.reduce_sum(1 + log_var - tf.square(mean) - tf.exp(log_var), axis=1) # Which of the following computes total loss correctly?
Attempts:
2 left
💡 Hint
Remember to average reconstruction loss over batch and sum KL divergence per sample before averaging.
✗ Incorrect
Reconstruction loss is averaged over batch, KL divergence is summed per sample then averaged over batch. Option A correctly combines these.
