To decode is to turn an intermediate representation into an output. In an image autoencoder, a decoder turns a latent vector back into an image with the original dimensions. In sequence-to-sequence translation, a decoder uses an encoded source sentence and the target words generated so far to predict the next word. section 17.1.2 section 15.2
Decode a latent point into a digit
An MNIST variational autoencoder (VAE) accepts a two-number latent point. In R/Keras, its decoder applies a dense layer, reshapes the result to 7 × 7 × 64, uses two transposed-convolution layers to enlarge it, and produces a 28 × 28 × 1 image with a sigmoid output layer. After training, pass a grid of latent points to predict(vae$decoder, z_grid) to see the digits they produce. section 17.1.3
The VAE’s encoder produces a mean and variance rather than one fixed code. A sampling layer draws a latent point, and the decoder reconstructs the input image from it. Training uses the input images as reconstruction targets; the trained decoder can also turn selected latent points into images. section 17.1.2 section 17.1.3
Listing 17.3 VAE decoder network, mapping latent-space points to images
latent_inputs <- keras_input(shape = c(latent_dim)) #1
decoder_outputs <- latent_inputs |>
layer_dense(7 * 7 * 64, activation = "relu") |> #2
layer_reshape(c(7, 7, 64)) |> #3
layer_conv_2d_transpose(64, 3, activation = "relu",
strides = 2, padding = "same") |> #4
layer_conv_2d_transpose(32, 3, activation = "relu", #4
strides = 2, padding = "same") |> #4
layer_conv_2d(1, 3, activation = "sigmoid", padding = "same") #5
decoder <- keras_model(latent_inputs, decoder_outputs,
name = "decoder")
The R/Keras listing shows the complete latent-vector-to-image decoder, including reshape, transposed convolutions, and final image output. section 17.1.3
Translation needs a different decoding loop. First, the encoder reads the whole source sentence: predicting output while reading only the matching source positions can miss a word at the end that must be translated first. During training, the decoder learns from earlier target tokens and the encoded source. During inference, it starts with [start], feeds each predicted token back in, and stops at [end]. section 15.2.2 section 15.2














