liveBook · Manning

Decode

As explained in Deep Learning with R, Third Edition

A decoder network is a neural-network component that maps a latent representation back to an output such as a reconstructed image.

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

How Deep Learning with R, Third Edition covers it

How deep it goes
Works through GRU-based translation decoding and a convolutional VAE decoder, including architecture, training, and generation examples.
Examples
Uses English-to-Spanish translation and MNIST digits generated from a grid of two-dimensional latent points.
What it assumes
Assumes some familiarity with Keras layers and earlier custom-training workflows while explaining the encoder, sampler, and decoder roles.
What you can do after
Build it from scratch with R/Keras to generate translations or decode latent points into MNIST digit images.
How it connects
Connects decoding to encoders, sequence-to-sequence learning, RNNs, variational autoencoders, latent spaces, and reconstruction.

Also explained in

Compare how 15 books explain decode →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with R, Third Edition. The section links point to the parts of the book it draws on.