liveBook · Manning

Sampling layer

As explained in Evolutionary Deep Learning: Genetic algorithms and neural networks

A sampling layer is a neural-network layer that produces a latent representation by sampling from distribution parameters such as a mean and variance.

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

How Evolutionary Deep Learning: Genetic algorithms and neural networks covers it

How deep it goes
Defines the distribution-to-vector mechanism and implements tensor shapes, random noise, decoder wiring, and training loss.
Examples
Demonstrates two-dimensional latent sampling by decoding a grid of coordinates into arranged 28×28 manifold images.
What it assumes
Assumes encoder-decoder autoencoders and latent representations while introducing mean-variance sampling through implementation.
What you can do after
Build it from scratch with dense parameter layers, a custom sampling function, a decoder, and a combined VAE loss.
How it connects
Connects autoencoders, variational autoencoders, latent representations, encoders, decoders, mean, variance, and reconstruction loss.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Evolutionary Deep Learning: Genetic algorithms and neural networks. The section links point to the parts of the book it draws on.