A sampling layer replaces an autoencoder’s single latent vector with learned distribution parameters: a mean and variance generate a new latent vector for the decoder. This lets a variational autoencoder learn input representations and generate new outputs from the learned latent space. section 8.5.1
A practical sampling layer follows this path:
- Flatten the encoder output and pass it through a dense layer. section 8.5.2
- Produce parallel vectors for the latent mean z_mean and log variance z_log_var. section 8.5.2
- Create random noise epsilon, scale it using the variance, and add the mean to obtain z. section 8.5.2
- Feed z into the decoder, while training combines reconstruction loss with a regularizing latent-space loss. section 8.5.2
Two-dimensional latent sampling
With latent_dim = 2, generate a grid of latent coordinates across selected mean and variance ranges, pass each coordinate to the decoder, and arrange the decoded 28×28 outputs into a manifold image. section 8.5.2
Listing 8.23 EDL_8_5_VAE.ipynb: Building latent sampling
x = Flatten()(x) #1
x = Dense(16, activation='relu')(x)
z_mean = Dense(latent_dim, name='z_mean')(x) #2
z_log_var = Dense(latent_dim, name='z_log_var')(x) #2
z = Lambda(
sampling,
output_shape=(latent_dim,),
name='z')([z_mean, z_log_var]) #3
encoder = Model(inputs, [z_mean, z_log_var, z],
name='encoder') #4
encoder.summary()
Inspect the encoder code that creates z_mean, z_log_var, and the custom z sampling layer. section 8.5.2
Listing 8.24 EDL_8_5_VAE.ipynb: The sampling function
def sampling(args):
z_mean, z_log_var = args
batch = K.shape(z_mean)[0] #1
dim = K.int_shape(z_mean)[1] #2
epsilon = K.random_normal(shape=(batch, dim)) #3
return z_mean + K.exp(0.5 * z_log_var) * epsilon #4
Inspect the sampling function’s tensor-shape handling, random-noise creation, and mean-variance transformation. section 8.5.2
