title: Keras variational autoencoder example - usage of latent input
body: <p>I'm new to Keras, and have been struggling with understanding the usage of the variable <code>z</code>  in the variational autoencoder example in their official <a href="https://github.com/keras-team/keras/blob/master/examples/variational_autoencoder.py" rel="nofollow noreferrer">github</a>. I don't understand why <code>z</code> is not being used instead of the variable <code>latent_inputs</code>. I ran the code and it seems to work, but I don't understand if <code>z</code> is being used behind the scenes and what is the mechanism in Keras that is responsible for it.
  Here is the relevant code snippet:
  </p>
  
  <pre><code># VAE model = encoder + decoder
  # build encoder model
  inputs = Input(shape=input_shape, name='encoder_input')
  x = Dense(intermediate_dim, activation='relu')(inputs)
  z_mean = Dense(latent_dim, name='z_mean')(x)
  z_log_var = Dense(latent_dim, name='z_log_var')(x)
  
  # use reparameterization trick to push the sampling out as input
  # note that "output_shape" isn't necessary with the TensorFlow backend
  z = Lambda(sampling, output_shape=(latent_dim,), name='z')([z_mean, z_log_var])
  
  # instantiate encoder model
  encoder = Model(inputs, [z_mean, z_log_var, z], name='encoder')
  encoder.summary()
  plot_model(encoder, to_file='vae_mlp_encoder.png', show_shapes=True)
  
  # build decoder model
  latent_inputs = Input(shape=(latent_dim,), name='z_sampling')
  x = Dense(intermediate_dim, activation='relu')(latent_inputs)
  outputs = Dense(original_dim, activation='sigmoid')(x)
  
  # instantiate decoder model
  decoder = Model(latent_inputs, outputs, name='decoder')
  decoder.summary()
  plot_model(decoder, to_file='vae_mlp_decoder.png', show_shapes=True)
  
  # instantiate VAE model
  outputs = decoder(encoder(inputs)[2])
  vae = Model(inputs, outputs, name='vae_mlp')
  </code></pre>