liveBook · Manning

Encoder

As explained in Learn Generative AI with PyTorch

An encoder is a component that transforms input data into an encoded representation for subsequent processing, reconstruction, comparison, or transmission.

An encoder turns an input into a learned representation that another component can use. In an autoencoder, it compresses an image into a latent vector for a decoder to reconstruct. In an encoder–decoder Transformer, it turns an input sentence into vectors that help a decoder produce a translation. The representation is the encoder’s output; reconstruction or generation is the decoder’s job. section 7.1.1 section 1.3.2

Compress handwritten digits

Start with a 28 × 28 grayscale digit. Flatten its 784 pixels and pass them through two dense layers to obtain a 20-value encoding. A decoder maps that vector back to 784 pixel values. To train both parts, compare the reconstruction with the original image, backpropagate the pixel-level loss, and inspect the reconstructed digits. The 20-value size is a design choice, not a special property of digits. section 7.1.2 section 7.2.2

Figure 7.3 The architecture of an AE and the steps to train one to generate handwritten digits. An AE consists of an encoder (middle left) and a decoder (middle right). In each iteration of training, images of handwritten digits are fed to the encoder (step 1). The encoder compresses the images to deterministic points in the latent space (step 2). The decoder takes the encoded vectors (step 3) from the latent space and reconstructs the images (step 4). The AE adjusts its parameters to minimize the reconstruction loss, the difference between the originals and the reconstructions (step 5).

Follow the digit from image to latent vector to reconstruction, then trace how reconstruction loss updates the encoder and decoder. section 7.1.2

The encoding need not always be one deterministic point. A variational autoencoder (VAE) for color faces uses convolutional layers to reduce a 3 × 256 × 256 image to parameters of a 100-value latent distribution. It samples an encoding for the decoder; training combines reconstruction loss with KL divergence. Convolutions help handle these larger images without relying solely on dense layers. section 7.3.2 section 7.4.1

For translation, the input is a sequence rather than an image. Token embeddings receive positional encodings before entering the Transformer encoder. Each encoder layer applies multihead self-attention and a position-wise feed-forward network, with normalization and residual connections; six such layers are stacked in the implementation. The resulting vectors go to a decoder that predicts French tokens using the encoder output and previously generated tokens. An encoder-only Transformer instead supplies representations for downstream tasks without that decoder. section 9.1.1 section 9.2.2 section 1.3.2

Other encoder roles depend on what the next component needs:

  • A denoising U-Net’s contracting path downsamples a noisy image and extracts features for its expansive path. section 15.1.2
  • CLIP’s image and text encoders place paired inputs in a shared embedding space for comparison. section 15.5.1

How Learn Generative AI with PyTorch covers it

How deep it goes
Works through dense, convolutional, and stacked Transformer encoders with training steps, tensor dimensions, and layer code.
Examples
Uses handwritten-digit reconstruction, face-image generation, English-to-French translation, U-Net denoising, and CLIP image–text pairs.
What it assumes
Builds on word embeddings and earlier convolutional and residual-connection concepts while spelling out encoder inputs and layers.
What you can do after
Build it from scratch in PyTorch as part of digit autoencoders, face VAEs, and encoder–decoder Transformers.
How it connects
Links encoders to decoders, latent spaces, variational autoencoders, self-attention, positional encoding, and contrastive learning.

Also explained in

Compare how 21 books explain encoder →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Learn Generative AI with PyTorch. The section links point to the parts of the book it draws on.