Deep Learning

Chapter 9
Representation and Self-Supervised Learning

Post Graduate Program in AI and ML
Deep Learning   •   Post Graduate

Representation and Self-Supervised Learning

Introduction

Deep learning systems rarely succeed because a final classifier is intrinsically intelligent. Their performance depends heavily on the quality of the representation presented to that classifier. A representation is a transformation of raw observations—such as pixels, waveforms, text, transactions, or sensor streams—into features that preserve information useful for a downstream task while discarding irrelevant variation.

In supervised learning, representations are shaped by labelled examples. However, labels are expensive, incomplete, subjective, and often unavailable in the required quantity. Representation learning addresses this limitation by learning useful features directly from data. Self-supervised learning goes further: it creates supervisory signals from the structure of the data itself. Examples include predicting a missing word, identifying whether two image crops came from the same image, reconstructing corrupted input, or forecasting a future segment of a time series.

This chapter develops embeddings, autoencoders, metric-learning systems, contrastive learning, and modern self-supervised workflows. It also examines retrieval, anomaly detection, evaluation, deployment, and the limitations of learned representations.

Learning Objectives

By the end of this chapter, a student should be able to:

  1. Explain what a representation and embedding are and why invariance matters.
  2. Design and analyse autoencoders, variational autoencoders, and denoising models.
  3. Formulate metric-learning objectives, including contrastive and triplet losses.
  4. Explain contrastive and non-contrastive self-supervised learning.
  5. Select appropriate evaluation measures for embeddings and retrieval systems.
  6. Build a representation-learning pipeline for limited-label and anomaly-detection settings.
  7. Critically assess bias, leakage, collapse, robustness, privacy, and deployment risks.

1. Representations, Embeddings, and Invariance

Let an observation be x ∈ X, where X is the input space. An encoder maps it into a representation:

z = f_θ(x), z ∈ ℝd

Here, f_θ is a neural network with parameters θ, z is the embedding, and d is the representation dimension. A downstream predictor may then calculate ŷ = g_φ(z). The encoder and predictor can be trained jointly or the encoder can be frozen and reused.

A useful representation should satisfy four often competing properties:

  • Discriminativeness: examples from different relevant classes should be separable.
  • Invariance: transformations that do not change semantic identity should have limited effect.
  • Equivariance: transformations whose effects matter should be reflected predictably in the representation. For example, a spatial translation may appropriately change an object-detection representation.
  • Transferability: features should support tasks beyond the objective used during pretraining.

An embedding is not automatically meaningful merely because it has low dimension. Its quality depends on the task, the data distribution, the geometry induced by the distance function, and the evaluation protocol.

1.1 Similarity and distance

For vectors u and v, Euclidean distance is

d(u,v) = √(∑j=1d (uj − vj)2)

Cosine similarity is

scos(u,v) = (u · v) / (||u||₂ ||v||₂)

where u · v is the dot product and ||u||₂ is the Euclidean norm. Cosine similarity is defined when both vectors are nonzero and is insensitive to their magnitudes. If embeddings are L2-normalized, cosine similarity equals their dot product. Euclidean distance and cosine similarity then have a fixed relationship:

||u − v||₂² = 2 − 2(u · v)

This identity assumes ||u||₂ = ||v||₂ = 1. Choosing a metric is therefore a modelling decision, not a cosmetic implementation detail.

2. Autoencoders and Reconstruction-Based Learning

An autoencoder consists of an encoder and decoder:

z = f_θ(x), x̂ = h_ψ(z)

The decoder attempts to reconstruct the input. For a dataset of N examples, a common objective is mean squared reconstruction error:

Lrec(θ,ψ) = (1/N) ∑i=1N ||xi − h_ψ(f_θ(xi))||₂²

This loss is suitable for continuous, appropriately scaled features and corresponds to a conditional Gaussian observation model with constant variance. For binary or normalized pixel data, binary cross-entropy may be more suitable. For a categorical sequence, token-level cross-entropy is generally preferable. The output likelihood and data representation must be aligned; otherwise, the loss may encourage distorted reconstructions.

2.1 Bottlenecks and capacity

If the latent dimension d is smaller than the input dimension, the encoder must compress information. Yet a bottleneck alone does not guarantee semantic features. A sufficiently powerful autoencoder can learn shortcuts, memorize training examples, or preserve nuisance information. Regularization, architectural constraints, corruption, and task-aware evaluation are therefore important.

A denoising autoencoder receives a corrupted input ẋ but reconstructs the clean input x:

LDAE = 𝔼x,ẋ[ℓ(x, h_ψ(f_θ(ẋ)))]

The expectation is over data samples and a corruption process. Typical corruption includes masking, additive noise, cropping, or token deletion. Denoising encourages locally stable representations and is particularly useful when corruption resembles operational noise.

A sparse autoencoder adds a penalty encouraging most latent units to be inactive. A generic form is

L = Lrec + λ Ω(z)

where λ ≥ 0 controls the regularization strength and Ω may be an L1 penalty or a divergence between average activation and a desired sparsity level. Excessive sparsity can remove useful information, so λ must be validated rather than assumed.

2.2 Variational autoencoders

A variational autoencoder (VAE) represents each input with a distribution rather than a single point. The encoder estimates q_φ(z|x), usually parameterized by a mean vector and diagonal standard deviations. The decoder defines p_θ(x|z). Training maximizes the evidence lower bound (ELBO):

ELBO(x) = 𝔼q_φ(z|x)[log p_θ(x|z)] − KL(q_φ(z|x) || p(z))

The first term rewards reconstruction under the assumed likelihood; the Kullback–Leibler divergence regularizes the latent distribution toward the prior p(z), commonly a standard normal distribution. Equivalently, the minimized negative ELBO is reconstruction loss plus KL regularization.

The reparameterization trick permits backpropagation through sampling:

z = μ_φ(x) + σ_φ(x) ⊙ ε, ε ∼ 𝒩(0,I)

Here, μ_φ(x) and σ_φ(x) are encoder outputs, ⊙ denotes elementwise multiplication, and I is the identity matrix. VAEs are useful for generative modelling, interpolation, and uncertainty-aware latent analysis, but their reconstructions may be blurry and their latent variables are not automatically disentangled.

3. Metric Learning

Metric learning directly shapes distances or similarities so that semantically related examples are close and unrelated examples are separated. It is valuable for face verification, product retrieval, speaker identification, few-shot classification, and duplicate detection.

3.1 Contrastive loss for pairs

For a pair (xi,xj), let yij=1 denote a positive pair and yij=0 a negative pair. With distance Dij=||zi−zj||₂, a margin-based contrastive loss is

Lpair = yijDij² + (1−yij) max(0, m−Dij)²

where m>0 is the negative-pair margin. Positive pairs are pulled together, whereas negative pairs are penalized only when closer than the margin. The margin has the same units as the embedding distance; it should be selected with regard to normalization and observed distance distributions.

3.2 Triplet loss

A triplet contains an anchor a, a positive example p, and a negative example n. The standard triplet objective is

Ltriplet = max(0, d(za,zp) − d(za,zn) + α)

where α>0 is the required separation margin. The loss is zero when the negative is sufficiently farther from the anchor than the positive. Random triplets are often uninformative, so semi-hard or hard-negative mining is used carefully. Very hard negatives may be false negatives or mislabeled examples and can destabilize training.

3.3 Batch-based objectives

Modern systems often compare every example with many others in a batch. For normalized embeddings and temperature τ>0, an InfoNCE-style loss for an anchor i and its positive p(i) is

Li = −log [ exp(s(zi,zp(i))/τ) / ∑k∈Ai exp(s(zi,zk)/τ) ]

where Ai is the set of candidates, including the positive, and s is commonly cosine similarity or a dot product. The temperature controls the concentration of the softmax distribution. Small values emphasize hard similarity differences but may increase optimization sensitivity.

The quality of negatives is central. A negative that shares the same class or semantic identity is a false negative. Large batches, memory queues, or cross-device gathering increase negative diversity but also increase computational and privacy costs.

4. Self-Supervised Learning Paradigms

Self-supervised learning constructs a pretext task from unlabelled data. The pretext task is useful only insofar as solving it produces transferable features.

4.1 Predictive and generative objectives

In language, masked-token modelling hides parts of a sequence and predicts them from context. In vision, masked-image modelling reconstructs missing patches. In speech and time series, the model may predict future or masked segments. These objectives teach contextual structure, but the masking strategy affects what information is learned.

An autoregressive objective for a sequence x1:T factorizes likelihood as

log p(x1:T) = ∑t=1T log p(xt | x<t)

where x<t denotes preceding elements. This objective is directional; bidirectional masked objectives provide a different information geometry.

4.2 Contrastive learning

Contrastive learning creates two augmented views of the same instance. Let t and t′ be stochastic transformations. The positive pair is (t(x),t′(x)), while views from other instances commonly supply negatives. The augmentations encode assumptions about invariance. Cropping may assert that object identity survives partial viewing; colour distortion may assert colour is irrelevant. If an augmentation destroys class information, the objective teaches the wrong invariance.

A practical pipeline is:

  1. Sample an observation.
  2. Generate two or more valid views.
  3. Encode each view and optionally apply a projection head.
  4. Compute a batch contrastive objective.
  5. Discard or retain the projection head according to validation results.
  6. Fine-tune or evaluate the encoder on downstream tasks.

Projection heads often improve the contrastive objective while allowing the encoder representation to retain information useful for transfer.

4.3 Non-contrastive learning

Methods such as redundancy-reduction and teacher–student approaches avoid explicit negative examples. A predictor may be trained to match a slowly changing target network, with stop-gradient operations preventing trivial mutual adaptation. These methods reduce dependence on large negative sets but remain vulnerable to representational collapse if architectural asymmetry, normalization, or variance constraints are inadequate.

Collapse occurs when many inputs map to nearly the same representation. Low training loss does not prove that collapse has been avoided. Variance, covariance rank, nearest-neighbour diversity, and downstream performance should be monitored.

5. Learning with Limited Labels

A common workflow is self-supervised pretraining followed by supervised fine-tuning. Given unlabelled data Du and labelled data Dl, one may first optimize LSSL on Du, then optimize a task loss Ltask on Dl. A joint objective can be written as

Ltotal = Ltask + λssl LSSL

where λssl controls the auxiliary objective. The terms must be measured on compatible scales, and validation must determine whether the auxiliary task helps rather than interferes.

Important design choices include:

  • Pretraining domain: in-domain unlabelled data usually provides stronger transfer than unrelated data.
  • Data splitting: near-duplicates, users, devices, or time periods must not cross train and test boundaries when that would create leakage.
  • Fine-tuning strategy: freeze the encoder for a linear probe, fine-tune selected layers, or fine-tune the entire model.
  • Label efficiency: plot performance against the number of labels, not only against the full labelled set.
  • Calibration: a useful embedding can still produce poorly calibrated class probabilities.

A linear probe evaluates whether information is readily accessible in the frozen representation, but a poor linear-probe result does not prove that information is absent; it may be encoded nonlinearly.

6. Retrieval and Anomaly Detection

6.1 Retrieval systems

A retrieval system maps a query and corpus items into a common space, computes similarity, and returns the nearest candidates. For a query q, the top-k result set is

Topk(q) = arg topkx∈C s(f(q), f(x))

where C is the corpus and s is a similarity function. Exact search may be expensive for millions of vectors, so approximate nearest-neighbour indexes trade a small amount of recall for lower latency and memory use.

Common metrics include precision at k, recall at k, mean reciprocal rank, and normalized discounted cumulative gain. For binary relevance, precision at k is

Precision@k = (number of relevant items among top k) / k

Recall at k divides the relevant retrieved items by the total number of relevant items. Evaluation must reflect the actual task: a medical search engine may value recall, whereas a costly recommendation slot may prioritize precision.

6.2 Anomaly detection

An autoencoder can assign reconstruction anomaly score

Arec(x) = ℓ(x, x̂)

A high score indicates poor reconstruction, but not necessarily fraud or danger. A model may reconstruct a systematic but harmful minority pattern well, or assign high error to benign distribution shifts.

A practical threshold can be selected from validation data. For a desired false-positive rate α, choose threshold t_α such that

P(Arec(x) > t_α | x is normal) ≈ α

This requires representative normal data and stable operating conditions. Quantile thresholds are not guarantees under distribution shift. Embedding-based anomaly detection may use distance from a class centroid or local density, but dimensionality, normalization, and contamination assumptions must be examined.

7. Worked Example: A Small Retrieval Calculation

Suppose normalized query and document embeddings have cosine similarities 0.82, 0.61, 0.76, and 0.48. The ranked order is therefore document 1, document 3, document 2, and document 4. If documents 1 and 3 are relevant, then:

Precision@2 = 2/2 = 1.00

If there are four relevant documents in the entire corpus, then:

Recall@2 = 2/4 = 0.50

The system has perfect precision in its first two results but retrieves only half of all relevant material. Which metric matters depends on the application. The example also illustrates why ranking quality cannot be summarized by a single unqualified “accuracy” value.

8. Illustrative Case Study: Industrial Sensor Monitoring

Background

Consider a fictional manufacturer operating motors with vibration, temperature, and acoustic sensors. Only a small number of failures have been labelled, while months of normal operation are available.

Problem

The company wants early warning of unusual operating behaviour and a retrieval interface that finds historically similar incidents. A supervised classifier trained only on known failures would have poor coverage of novel failure modes.

Analysis

The engineering team first removes duplicated windows from adjacent train and test partitions and splits by machine and time. It scales continuous channels using training-set statistics. It observes that operating load changes the normal vibration distribution, so load is retained as contextual information rather than treated as noise.

Approach

A masked sequence encoder is pretrained on unlabelled sensor windows. Two views are created by modest temporal masking and small amplitude perturbations; transformations that alter frequency signatures substantially are rejected. A denoising autoencoder is also trained as a baseline. The final embedding is evaluated using:

  • a linear probe for known fault categories;
  • retrieval of similar maintenance events;
  • anomaly detection on a temporally held-out normal period;
  • false alarms per machine-day and detection lead time.

The team uses a hybrid anomaly score combining normalized reconstruction error and distance to a density model in embedding space. Thresholds are selected separately for machine families because their normal operating regimes differ.

Outcome

The self-supervised encoder improves retrieval and detects several patterns not represented in the labelled fault set. However, performance decreases after a sensor replacement changes the signal distribution. Monitoring detects this drift, and the system is recalibrated with new normal data. Maintenance engineers retain final authority because an anomaly is an alert for investigation, not proof of imminent failure.

Learning points

The case demonstrates that self-supervision is not a substitute for domain knowledge. Augmentations, data splitting, threshold selection, and operational metrics determine usefulness. It also shows why deployment monitoring and human oversight are part of representation-learning design.

9. Evaluation, Failure Modes, and Governance

9.1 Evaluation principles

Representation quality should be assessed at several levels:

  • Intrinsic: clustering structure, neighbour consistency, uniformity, variance, and covariance spectrum.
  • Extrinsic: downstream classification, regression, retrieval, ranking, or anomaly detection.
  • Robustness: sensitivity to noise, transformations, missing modalities, and distribution shift.
  • Efficiency: inference latency, memory, index size, and energy consumption.
  • Fairness: performance and error rates across relevant demographic, geographic, or operational groups.

A representation can score well intrinsically and poorly in a practical task. Conversely, a task-specific representation may be highly useful without producing visually intuitive clusters.

9.2 Common failure modes

Shortcut learning occurs when the model uses background, device identity, acquisition artefacts, or metadata instead of the intended signal. False negatives corrupt contrastive learning when semantically similar examples are treated as unrelated. Information leakage produces inflated results when augmentations, duplicates, or temporal neighbours cross evaluation boundaries. Collapse removes diversity. Over-compression discards rare but important information. Spurious invariance results when augmentations erase a feature needed for deployment.

9.3 Ethical and governance issues

Embeddings can encode sensitive attributes even when those attributes are not explicitly provided. Vector databases may enable identity inference, membership inference, or unintended linkage across datasets. Access control, encryption, retention limits, audit logs, and deletion procedures are therefore necessary. If a user requests removal of data, deleting the original record may not remove its influence from a pretrained encoder or index; retraining and index regeneration may be required.

In high-impact applications, document data provenance, consent and lawful basis, intended use, known limitations, subgroup performance, threshold rationale, and human escalation procedures. Do not present anomaly scores as objective truth. Governance should address who may act on an alert and who is accountable for errors.

10. Best-Practice Design Pattern

A robust representation-learning project typically follows this sequence:

  1. Define the downstream decision and the cost of errors.
  2. Audit provenance, duplicates, sensitive attributes, and temporal structure.
  3. Specify valid and invalid augmentations using domain knowledge.
  4. Establish supervised, heuristic, and simple embedding baselines.
  5. Pretrain with an objective matched to the data and deployment task.
  6. Tune temperature, margin, latent dimension, corruption rate, and regularization only on validation data.
  7. Evaluate label efficiency, subgroup behaviour, robustness, retrieval, and operational latency.
  8. Stress-test false negatives, distribution shift, and threshold stability.
  9. Version the encoder, preprocessing, index, and evaluation data.
  10. Monitor drift and maintain a process for human review and model retirement.

Contemporary Directions

Current research increasingly combines masked prediction, multimodal alignment, retrieval augmentation, and parameter-efficient adaptation. Foundation encoders can transfer across tasks, but their scale does not eliminate domain shift or governance obligations. Multimodal models raise additional alignment questions: a shared embedding may make text and images retrievable together, yet the alignment can reflect unequal data quality or social stereotypes.

Research also explores online and continual self-supervision, where representations adapt to changing streams. This creates a tension between plasticity and stability: adaptation can improve current performance while erasing previously learned capabilities. Evaluation should therefore include historical tasks, rollback procedures, and explicit drift criteria.

Chapter Summary

Representation learning transforms raw data into features intended to preserve useful structure. Autoencoders learn through reconstruction; denoising and sparsity impose useful constraints, while VAEs learn probabilistic latent spaces through the ELBO. Metric learning shapes distances using pairs, triplets, or batch-based contrastive objectives. Self-supervised learning derives labels from masking, prediction, augmentation, or teacher–student relationships and is especially valuable when labelled data is scarce.

Embeddings support retrieval, similarity search, transfer learning, and anomaly detection, but their performance depends on metric choice, negative sampling, augmentation validity, data splitting, and evaluation design. Collapse, shortcut learning, false negatives, leakage, drift, privacy risks, and subgroup disparities require explicit monitoring. A successful system combines objective design with domain knowledge, operational evaluation, governance, and human oversight.

Key Terms

Representation: A transformed form of data that preserves information useful for analysis or prediction.

Embedding: A numerical vector representation, usually in a continuous low-dimensional space.

Encoder: A function that maps an input to a latent representation.

Decoder: A function that maps a latent representation back toward the input space.

Autoencoder: A neural network trained to reconstruct its input through a latent bottleneck.

Denoising autoencoder: An autoencoder trained to reconstruct clean data from corrupted input.

Variational autoencoder: A probabilistic autoencoder that regularizes an approximate latent distribution toward a prior.

Metric learning: Learning a distance or similarity structure in which semantic relationships are reflected geometrically.

Contrastive learning: Learning by bringing positive examples together and separating or ranking negative examples.

Triplet loss: A loss that enforces a margin between anchor-positive and anchor-negative distances.

InfoNCE: A softmax-based contrastive objective comparing a positive pair against candidate negatives.

Positive pair: Two views or examples expected to share relevant semantic identity.

False negative: An example treated as negative although it is semantically related to the anchor.

Representation collapse: A failure mode in which diverse inputs receive nearly identical representations.

Self-supervised learning: Learning from supervisory signals constructed from unlabelled data.

Linear probe: A linear predictor trained on frozen representations to assess accessible information.

Approximate nearest-neighbour search: Efficient retrieval that approximates exact vector-neighbour search.

Anomaly score: A numerical measure indicating how unusual an observation appears under a model.

Distribution shift: A change between training and deployment data distributions.

Data leakage: Unintended use of information from evaluation data during training or model selection.

Review Questions

Short Answer Questions

  1. Distinguish an embedding from a general representation.
  2. What role does the bottleneck play in an autoencoder?
  3. State the purpose of the margin in contrastive or triplet loss.
  4. Why can a false negative harm contrastive learning?
  5. What does a linear probe measure?

Long Answer Questions

  1. Explain the architecture and objectives of denoising autoencoders and variational autoencoders. Compare their assumptions and applications.
  2. Derive and interpret the triplet loss, including the effects of margin selection and negative mining.
  3. Compare predictive, contrastive, and non-contrastive self-supervised learning paradigms.
  4. Design a representation-learning workflow for a limited-label industrial monitoring problem.
  5. Discuss the evaluation, privacy, fairness, and governance requirements for an embedding-based retrieval system.

Analytical / Application Questions

  1. A retrieval system returns five results, of which three are relevant. There are eight relevant items in the corpus. Calculate Precision@5 and Recall@5, and explain what the two values imply.
  2. You are given an image dataset in which colour is diagnostically important. Which common augmentations might create harmful invariance, and how would you redesign the self-supervised task?
  3. An anomaly detector performs well in validation but generates many alerts after a sensor upgrade. Analyse possible causes and propose a technically and operationally safe response.

Multiple Choice Questions with Answers

Question 1

Which property means that a representation changes little under a transformation that does not alter semantic identity?

A. Invariance

B. Equivariance

C. Calibration

D. Quantization

Correct Answer: A. Invariance

Question 2

In a denoising autoencoder, the model is trained to reconstruct:

A. The corrupted input from itself

B. The clean input from a corrupted version

C. A random negative example

D. The class label from the decoder

Correct Answer: B. The clean input from a corrupted version

Question 3

In the triplet loss, the anchor, positive, and negative are used to encourage:

A. The negative to be closer than the positive

B. All embeddings to have zero norm

C. The positive to be closer to the anchor than the negative by a margin

D. The decoder to reproduce the input exactly

Correct Answer: C. The positive to be closer to the anchor than the negative by a margin

Question 4

What is a principal risk of using false negatives in contrastive learning?

A. They make all input features binary

B. They force semantically related examples apart

C. They eliminate the need for augmentation

D. They guarantee representation collapse

Correct Answer: B. They force semantically related examples apart

Question 5

For L2-normalized vectors, cosine similarity is equal to their:

A. Dot product

B. Manhattan distance

C. Squared Euclidean distance plus two

D. Variance

Correct Answer: A. Dot product

Question 6

The KL-divergence term in a standard variational autoencoder primarily:

A. Regularizes the approximate latent distribution toward a prior

B. Guarantees perfectly sharp reconstructions

C. Replaces the encoder

D. Measures retrieval precision

Correct Answer: A. Regularizes the approximate latent distribution toward a prior

Question 7

If five retrieved items contain three relevant items, Precision@5 is:

A. 0.20

B. 0.375

C. 0.60

D. 1.67

Correct Answer: C. 0.60

Question 8

A linear probe is normally trained to evaluate:

A. The storage cost of an index

B. Whether downstream information is accessible with a linear predictor

C. Whether the decoder is probabilistic

D. The number of false negatives in the raw dataset

Correct Answer: B. Whether downstream information is accessible with a linear predictor

Question 9

Which situation is most likely to cause data leakage in a temporal sensor experiment?

A. Using training-set scaling statistics

B. Keeping a versioned preprocessing pipeline

C. Placing near-duplicate windows from the same event in both train and test sets

D. Evaluating on a future time period

Correct Answer: C. Placing near-duplicate windows from the same event in both train and test sets

Question 10

A high reconstruction error from an autoencoder should generally be interpreted as:

A. Definitive proof of fraud or failure

B. Evidence that the observation is unusual under the learned model

C. Proof that the input label is correct

D. Evidence that the embedding has no information

Correct Answer: B. Evidence that the observation is unusual under the learned model

Deep Learning
Chapter 9 of 15