Skip to content
E
Educatum

Autoencoder

By Educatum AI teamLast updated: Original walkthrough ↗
Key Intuition

An autoencoder is a self-supervised neural network made of an encoder that compresses inputs into a lower-dimensional latent bottleneck and a decoder that reconstructs the original input from that representation. Because the input itself acts as the training target, this self-supervised paradigm also underpins masked representation learning and contrastive multimodal pretraining.

Reference Diagram & Materials
Autoencoder matrix calculation diagram

From Prof. Tom Yeh’s Walkthrough

Autoencoder walkthrough diagram

The autoencoder is a foundational self-supervised learning architecture for training models on massive unlabeled datasets—spanning billions of web-scale examples.

At that scale, hiring humans to manually label every training target is infeasible. Instead, self-supervised methods derive training targets directly from the data itself (automatically, from the Greek auto, meaning “self”):

  • Reconstruction (Autoencoders): Use the full input example itself as the target to reconstruct through a compressed bottleneck.
  • Masking (Masked Modeling): Hide parts of the input and predict the missing pieces from the surrounding context—the core objective behind encoder models like BERT (Masked Language Modeling) and Masked Autoencoders (MAE). (Note: modern autoregressive LLMs likewise derive targets automatically from unlabeled text via causal next-token prediction rather than masked reconstruction.)
  • Contrastive Pairing: Pair aligned modalities (such as images and text) so each acts as the matching target for the other. This contrastive learning objective powers dual-encoder models like OpenAI’s CLIP and underpins many multimodal foundation models.

Network Architecture

Encoder

  1. Linear(4, 3)
  2. ReLU
  3. Linear(3, 2)
  4. ReLU

Decoder

  1. Linear(2, 3)
  2. ReLU
  3. Linear(3, 4)

Step-by-Step Walkthrough

[1] Given

  • Four training examples X1,X2,X3,X4X_1, X_2, X_3, X_4, each represented as a 4-dimensional input vector.

[2] Auto (Self-Target)

  • Copy the input training examples to serve as the reconstruction targets (Y′=XY' = X).
  • Because each target is the input itself, the network learns compressed features without external labels.

[3] Encoder: Layer 1 + ReLU

  • Multiply the 4-D inputs by the learned weight matrix and add biases to project into 3 dimensions.
  • Apply ReLU(z)=max⁡(0,z)\text{ReLU}(z) = \max(0, z), zeroing out negative activations (e.g., −1→0-1 \to 0).

[4] Encoder: Layer 2 + ReLU (Bottleneck)

  • Multiply the 3-D hidden features by the second layer’s weights and biases, then apply ReLU.
  • This layer is the bottleneck because its latent output has fewer dimensions (22) than the original input (44), forcing the encoder to retain only the most salient structure.

[5] Decoder: Layer 1 + ReLU

  • Project the 2-D latent codes back up to 3 dimensions with learned weights and biases, followed by ReLU (in the example diagram, all activations at this step are non-negative so none are zeroed).

[6] Decoder: Layer 2 (Reconstruction Output)

  • Apply the final linear layer (Linear(3, 4)) without ReLU so the reconstructed output YY can take arbitrary real values matching the input space.

[7] Loss Gradients & Backpropagation

  • Compute the reconstruction error between outputs YY and targets Y′Y'.
  • Hand-calculation exercises typically use Sum of Squared Errors (keeping arithmetic clean), while training code averages over nn elements with Mean Squared Error:
LSSE=∑i=1n(Yi−Yi′)2,∂LSSE∂Y=2(Y−Y′)LMSE=1n∑i=1n(Yi−Yi′)2,∂LMSE∂Y=2n(Y−Y′)\begin{aligned} \mathcal{L}_{\text{SSE}} &= \sum_{i=1}^{n} (Y_i - Y'_i)^2, & \frac{\partial \mathcal{L}_{\text{SSE}}}{\partial Y} &= 2(Y - Y') \\[6pt] \mathcal{L}_{\text{MSE}} &= \frac{1}{n}\sum_{i=1}^{n} (Y_i - Y'_i)^2, & \frac{\partial \mathcal{L}_{\text{MSE}}}{\partial Y} &= \frac{2}{n}(Y - Y') \end{aligned}
  • Subtract targets from outputs (Y−Y′Y - Y') and scale by 22 (or 2n\frac{2}{n}); these gradients then propagate backward through both the decoder and encoder to update all weights and biases.

More in GenAI by Hand

Try keywords like RoPE, FlashAttention, DPO, or Blind 75.