Bird
Raised Fist0
TensorFlowml~10 mins

Variational Autoencoders (VAE) in TensorFlow - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to define the encoder input layer for the VAE.

TensorFlow
encoder_inputs = tf.keras.Input(shape=[1])
Drag options to blanks, or click blank then click option'
A(28, 28, 1)
B(32, 32, 3)
C(100,)
D(10,)
Attempts:
3 left
💡 Hint
Common Mistakes
Using a shape for color images like (32, 32, 3) instead of grayscale.
Using a flat vector shape instead of image dimensions.
2fill in blank
medium

Complete the code to sample the latent vector z using the reparameterization trick.

TensorFlow
z = [1](mu, log_var)
Drag options to blanks, or click blank then click option'
Aencode
Breparameterize
Clatent_sample
Dsample_latent
Attempts:
3 left
💡 Hint
Common Mistakes
Using a function name that does not exist or is unrelated.
Confusing encoding with sampling.
3fill in blank
hard

Fix the error in the loss function by completing the KL divergence term.

TensorFlow
kl_loss = -0.5 * tf.reduce_sum(1 + [1] - tf.square(mu) - tf.exp(log_var), axis=1)
Drag options to blanks, or click blank then click option'
Az
Bmu
Clog_var
Dsigma
Attempts:
3 left
💡 Hint
Common Mistakes
Using mean (mu) instead of log variance.
Using the sampled latent vector z instead of log variance.
4fill in blank
hard

Fill both blanks to complete the decoder output layer and activation.

TensorFlow
decoder_outputs = tf.keras.layers.Dense([1], activation=[2])(decoder_hidden)
Drag options to blanks, or click blank then click option'
A784
Bsigmoid
Crelu
D256
Attempts:
3 left
💡 Hint
Common Mistakes
Using relu activation in the output layer.
Setting output units to a hidden layer size like 256.
5fill in blank
hard

Fill all three blanks to complete the training step function for the VAE.

TensorFlow
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))
Drag options to blanks, or click blank then click option'
Alog_var
Bmu
Dreconstructed
Attempts:
3 left
💡 Hint
Common Mistakes
Using reconstructed instead of log_var in KL loss.
Mixing up mu and log_var positions.

Practice

(1/5)
1. What is the main purpose of the encoder in a Variational Autoencoder (VAE)?
easy
A. To learn the mean and variance of the latent space distribution
B. To reconstruct the input data from latent variables
C. To generate new data samples directly
D. To calculate the loss function during training

Solution

  1. Step 1: Understand the encoder role in VAE

    The encoder processes input data and outputs parameters of a probability distribution, typically mean and variance, representing the latent space.
  2. Step 2: Differentiate encoder from decoder

    The decoder uses sampled latent variables to reconstruct data, not the encoder.
  3. Final Answer:

    To learn the mean and variance of the latent space distribution -> Option A
  4. Quick Check:

    Encoder outputs mean and variance [OK]
Hint: Encoder outputs mean and variance, decoder reconstructs [OK]
Common Mistakes:
  • Confusing encoder with decoder
  • Thinking encoder generates final output
  • Assuming encoder calculates loss
2. Which of the following is the correct way to sample latent variables in a VAE using the reparameterization trick in TensorFlow?
easy
A. z = tf.random.normal(shape) + mean * variance
B. z = mean + tf.random.normal(shape) * variance
C. z = mean * tf.random.normal(shape) + variance
D. z = mean + tf.random.normal(shape) * tf.sqrt(variance)

Solution

  1. Step 1: Recall reparameterization formula

    The latent variable z is sampled as z = mean + epsilon * stddev, where epsilon ~ N(0,1) and stddev = sqrt(variance).
  2. Step 2: Match formula to options

    z = mean + tf.random.normal(shape) * tf.sqrt(variance) correctly uses tf.sqrt(variance) to get stddev and multiplies by random normal noise.
  3. Final Answer:

    z = mean + tf.random.normal(shape) * tf.sqrt(variance) -> Option D
  4. Quick Check:

    Sample = mean + noise * stddev [OK]
Hint: Multiply noise by sqrt(variance), not variance directly [OK]
Common Mistakes:
  • Using variance instead of its square root
  • Multiplying mean by noise instead of adding
  • Mixing order of operations
3. Given the following TensorFlow code snippet for a VAE encoder output, what is the shape of the sampled latent vector z?
mean = tf.constant([[0.0, 0.0], [1.0, 1.0]])
log_var = tf.constant([[0.0, 0.0], [0.0, 0.0]])
stddev = tf.exp(0.5 * log_var)
epsilon = tf.random.normal(shape=tf.shape(mean))
z = mean + epsilon * stddev
medium
A. (2, 2)
B. (2,)
C. (1, 2)
D. (2, 1)

Solution

  1. Step 1: Check shapes of mean and log_var

    Both mean and log_var have shape (2, 2), meaning 2 samples with 2 latent dimensions each.
  2. Step 2: Determine shape of epsilon and z

    epsilon is sampled with shape matching mean, so (2, 2). z is computed element-wise, so it also has shape (2, 2).
  3. Final Answer:

    (2, 2) -> Option A
  4. Quick Check:

    Output shape matches input batch and latent dims [OK]
Hint: Sample shape matches mean shape exactly [OK]
Common Mistakes:
  • Assuming latent vector is 1D
  • Ignoring batch dimension
  • Confusing log variance shape
4. Identify the error in this VAE decoder code snippet and select the correct fix:
def decode(z):
    x_recon = tf.keras.layers.Dense(784, activation='sigmoid')(z)
    return x_recon

# Usage
z = tf.random.normal(shape=(32, 2))
x_reconstructed = decode(z)
medium
A. The Dense layer should be created inside the function, not called directly on z
B. The Dense layer should be defined once outside the function and called inside
C. Activation should be 'relu' instead of 'sigmoid' for reconstruction
D. The input z shape is incorrect; it should be (784, 2)

Solution

  1. Step 1: Understand Keras layer usage

    Keras layers should be defined once (e.g., in __init__) and then called multiple times. Defining inside function creates new layers each call, which is inefficient and breaks training.
  2. Step 2: Identify correct fix

    The Dense layer should be defined once outside the function and called inside suggests defining the Dense layer once outside and calling it inside decode, which is the proper pattern.
  3. Final Answer:

    The Dense layer should be defined once outside the function and called inside -> Option B
  4. Quick Check:

    Define layers once, call repeatedly [OK]
Hint: Define layers once, call inside functions [OK]
Common Mistakes:
  • Defining layers inside functions repeatedly
  • Confusing activation functions for reconstruction
  • Misunderstanding input shapes
5. You want to train a VAE on image data with 28x28 pixels. Which loss function combination correctly balances reconstruction and latent space regularization?
hard
A. Mean squared error reconstruction loss only
B. KL divergence only without reconstruction loss
C. Binary cross-entropy reconstruction loss + KL divergence between latent and standard normal
D. Categorical cross-entropy reconstruction loss + L2 regularization on weights

Solution

  1. Step 1: Understand VAE loss components

    VAE loss combines reconstruction loss (measuring how well output matches input) and KL divergence (regularizing latent space to be close to standard normal).
  2. Step 2: Match loss to image data and VAE theory

    For 28x28 images with pixel values in [0,1], binary cross-entropy is suitable for reconstruction. KL divergence is essential for latent regularization.
  3. Final Answer:

    Binary cross-entropy reconstruction loss + KL divergence between latent and standard normal -> Option C
  4. Quick Check:

    Reconstruction + KL divergence = correct VAE loss [OK]
Hint: Use reconstruction loss + KL divergence for VAE [OK]
Common Mistakes:
  • Ignoring KL divergence term
  • Using only reconstruction loss
  • Using wrong loss for image data
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard