Centered Kernel Alignment: How CKA Measures Representation Similarity Across Layers and Architectures

Understanding how deep neural networks represent information across layers, training steps, and disparate architectures has long been a central challenge in machine learning interpretability. When two neural networks are trained on the exact same dataset, even from identical model architectures, their learned weight matrices and individual neuron activations differ completely due to random initialization, data shuffling, and non-convex optimization. Because representations are not aligned to a s

9 min
Centered Kernel Alignment: How CKA Measures Representation Similarity Across Layers and Architectures

Understanding how deep neural networks represent information across layers, training steps, and disparate architectures has long been a central challenge in machine learning interpretability. When two neural networks are trained on the exact same dataset, even from identical model architectures, their learned weight matrices and individual neuron activations differ completely due to random initialization, data shuffling, and non-convex optimization. Because representations are not aligned to a shared canonical basis, direct element-wise or Euclidean comparisons between activation vectors fail to measure whether two networks have learned equivalent latent structures.

To compare neural representations, researchers historically relied on metrics such as Canonical Correlation Analysis (CCA), Singular Vector CCA (SVCCA), and Projection-Weighted CCA (PWCCA). However, as demonstrated by Kornblith et al. (2019), these metrics suffer from severe mathematical pathologies when applied to modern overparameterized neural networks. In response, Kornblith and colleagues introduced Centered Kernel Alignment (CKA) as a mathematically rigorous, computationally tractable similarity index that measures representational similarity across layers, random seeds, and distinct model paradigms.


The Invariance Dilemma in Neural Representations

Let XRn×p1X \in \mathbb{R}^{n \times p_1} and YRn×p2Y \in \mathbb{R}^{n \times p_2} denote the activation matrices obtained by passing nn identical input examples through two neural network layers with widths p1p_1 and p2p_2, respectively. Each row represents the latent activation vector for a specific input example across pp features.

A valid similarity metric s(X,Y)s(X, Y) must satisfy specific invariance properties to provide meaningful comparisons across neural systems:

  1. Invariance to Orthogonal Transformations: If one layer's representation is a rotated or reflected version of another (Y=XQY = X Q where QRp1×p1Q \in \mathbb{R}^{p_1 \times p_1} is an orthogonal matrix such that QTQ=IQ^T Q = I), the underlying geometry of the representation is identical. A valid metric must satisfy s(X,XQ)=s(X,X)s(X, XQ) = s(X, X).
  2. Invariance to Isotropic Scaling: Multiplying all activations by a scalar constant (Y=αXY = \alpha X for α>0\alpha > 0) changes only the magnitude of activations, not the relative geometrical configuration of the samples in latent space. A valid metric must satisfy s(X,αX)=s(X,X)s(X, \alpha X) = s(X, X).
  3. Non-Invariance to Arbitrary Invertible Linear Transformations: If a metric is invariant to any full-rank linear transformation (Y=XAY = X A for any invertible matrix ARp1×p2A \in \mathbb{R}^{p_1 \times p_2}), it becomes incapable of distinguishing meaningful geometric structure from random noise.
+-------------------------------------------------------------------------------+
|                      REPRESENTATIONAL INVARIANCE TRADE-OFFS                    |
+-----------------------------------+--------------------+----------------------+
| Metric                            | Invariance Group   | Failure Mode         |
+-----------------------------------+--------------------+----------------------+
| Euclidean Distance / Procrustes   | Rotations only     | Sensitive to scaling |
| Canonical Correlation (CCA/SVCCA) | Invertible Linear  | Spurious noise fit   |
| Centered Kernel Alignment (CKA)   | Orthogonal + Scale | Structurally robust  |
+-----------------------------------+--------------------+----------------------+

Why Invertible Linear Metrics Fail: The CCA Breakdown

Canonical Correlation Analysis (Hotelling, 1936) finds linear projections uRp1u \in \mathbb{R}^{p_1} and vRp2v \in \mathbb{R}^{p_2} that maximize the Pearson correlation between XuXu and YvYv. Subsequent canonical correlation coefficients ρ1,ρ2,,ρmin(p1,p2)\rho_1, \rho_2, \dots, \rho_{\min(p_1, p_2)} are obtained subject to orthogonality constraints on the projected coordinates.

While CCA is invariant to all invertible affine transformations, this exact property renders it pathological when analyzing high-dimensional neural representations. When the feature dimension pp is larger than or comparable to the sample size nn (pnp \ge n), CCA can find orthogonal directions in the ambient space that fit spurious noise.

                       CANONICAL CORRELATION COLLAPSE (p >= n)
                       
          Activation X (p1 dims)                 Activation Y (p2 dims)
               [Noise Dir 1]                          [Noise Dir 1]
               [Noise Dir 2]      Linear Fit          [Noise Dir 2]
               [Noise Dir 3]  ------------------>     [Noise Dir 3]
               [    ...    ]    Overparameterized     [    ...    ]
               [Noise Dir p]      Spurious Rank       [Noise Dir p]
               
                 Result: CCA correlation = 1.0 on random Gaussian noise

As proved by Kornblith et al. (2019), when XX and YY are independent random Gaussian matrices with p1,p2np_1, p_2 \ge n, CCA yields canonical correlations of ρi=1.0\rho_i = 1.0 for all ii. Even with modified variants like SVCCA (Raghu et al., 2017) and PWCCA (Morcos et al., 2018), which perform singular value truncation or projection weighting, the underlying sensitivity to direction-independent rotations and high-dimensional noise persists.


Mathematical Formulation of Centered Kernel Alignment

Rather than aligning individual neuron coordinates or finding arbitrary projections, Centered Kernel Alignment evaluates the similarity between the pairwise inter-example similarity structures generated by two representations.

Centered Kernel Alignment Architecture and Representation Similarity

1. Gram Matrices and Representational Similarity

Given activation matrices XRn×p1X \in \mathbb{R}^{n \times p_1} and YRn×p2Y \in \mathbb{R}^{n \times p_2} over nn inputs, compute the n×nn \times n Gram matrices (inner product kernel matrices):

K=XXTandL=YYTK = X X^T \quad \text{and} \quad L = Y Y^T

The entry Ki,j=xi,xjK_{i,j} = \langle x_i, x_j \rangle measures the inner product similarity between the representation of input ii and input jj in layer XX.

2. Centering in Feature Space

To ensure that similarity is not dominated by the mean activation vector, the Gram matrices must be centered in feature space. Let HRn×nH \in \mathbb{R}^{n \times n} denote the symmetric centering matrix:

H=In1n11TH = I_n - \frac{1}{n}\mathbf{1}\mathbf{1}^T

The centered Gram matrices are given by:

K=HKHandL=HLHK' = H K H \quad \text{and} \quad L' = H L H

Centering ensures that iKi,j=0\sum_i K'_{i,j} = 0 and jKi,j=0\sum_j K'_{i,j} = 0, aligning the feature vectors to have zero sample mean before computing inner products.

3. The Hilbert-Schmidt Independence Criterion (HSIC)

The alignment between centered Gram matrices is quantified using the Hilbert-Schmidt Independence Criterion (Gretton et al., 2005), which measures the statistical dependence between two sets of variables mapped into reproducing kernel Hilbert spaces (RKHS):

HSIC(K,L)=1(n1)2tr(KHLH)=1(n1)2vec(HKH)Tvec(HLH)\text{HSIC}(K, L) = \frac{1}{(n-1)^2} \text{tr}(K H L H) = \frac{1}{(n-1)^2} \text{vec}(H K H)^T \text{vec}(H L H)

For linear kernels K=XXTK = X X^T and L=YYTL = Y Y^T, HSIC simplifies to the squared Frobenius norm of the cross-covariance matrix:

HSIC(XXT,YYT)=1(n1)2YTHXF2\text{HSIC}(X X^T, Y Y^T) = \frac{1}{(n-1)^2} \|Y^T H X\|_F^2

4. Normalized Linear and Kernel CKA

Because raw HSIC scales quadratically with the norm of activations, CKA normalizes HSIC by the geometric mean of each representation's self-alignment (Cortes et al., 2012):

CKA(K,L)=HSIC(K,L)HSIC(K,K)HSIC(L,L)\text{CKA}(K, L) = \frac{\text{HSIC}(K, L)}{\sqrt{\text{HSIC}(K, K) \cdot \text{HSIC}(L, L)}}

For linear kernels, substituting the Frobenius formulation yields:

Linear CKA(X,Y)=YTXF2XTXFYTYF\text{Linear CKA}(X, Y) = \frac{\|Y^T X\|_F^2}{\|X^T X\|_F \cdot \|Y^T Y\|_F}

Linear CKA is bounded in the interval [0,1][0, 1]. It achieves CKA=1.0\text{CKA} = 1.0 if and only if KK' and LL' are proportional by a positive scalar constant, indicating identical inter-sample geometric relationships.

                           THE CKA COMPUTATIONAL PIPELINE
                           
       Activations X (n x p1)                   Activations Y (n x p2)
                 │                                        │
                 ▼                                        ▼
      Gram Matrix K = X X^T                    Gram Matrix L = Y Y^T
                 │                                        │
                 ▼                                        ▼
    Centered Gram K' = H K H                 Centered Gram L' = H L H
                 │                                        │
                 └───────────────────┬────────────────────┘
                                     │
                                     ▼
                      HSIC(K, L) = tr(K' L') / (n-1)^2
                                     │
                                     ▼
            CKA(K, L) = HSIC(K, L) / sqrt(HSIC(K,K) * HSIC(L,L))

What CKA Revealed About Deep Neural Networks

The introduction of CKA transformed empirical analysis of deep learning systems, disproving several long-held assumptions and revealing consistent structural principles across architectures.

1. Representational Block Structure and Iterative Refinement

When plotting all-to-all layer similarity heatmaps (where entry (i,j)(i, j) represents CKA(Layeri,Layerj)\text{CKA}(\text{Layer}_i, \text{Layer}_j) within the same network), deep residual networks and Transformer backbones do not transition smoothly from input to output.

Instead, they exhibit distinct square "block structures" along the diagonal. Within each block, consecutive layers exhibit CKA scores exceeding 0.90 to 0.95. This demonstrates that deep networks do not continuously construct new representations at each layer; rather, long sequences of residual layers perform iterative refinement on a shared latent subspace (Kornblith et al., 2019; Nguyen et al., 2021).

                       REPRESENTATIONAL SIMILARITY HEATMAP
                       
           Layer  0   5  10  15  20  25  30  32
             0   [██  ░░  ░░  ░░  ░░  ░░  ░░  ░░]  Early feature extraction
             5   [░░  ██  ██  ░░  ░░  ░░  ░░  ░░]
            10   [░░  ██  ██  ░░  ░░  ░░  ░░  ░░]  Block 1: Iterative refinement
            15   [░░  ░░  ░░  ██  ██  ██  ░░  ░░]
            20   [░░  ░░  ░░  ██  ██  ██  ░░  ░░]  Block 2: Core processing
            25   [░░  ░░  ░░  ██  ██  ██  ░░  ░░]
            30   [░░  ░░  ░░  ░░  ░░  ░░  ██  ██]  Task-specific projection
            32   [░░  ░░  ░░  ░░  ░░  ░░  ██  ██]

2. Vision Transformers vs. Convolutional Networks

In a landmark study, Raghu et al. (2021) used CKA to contrast Vision Transformers (ViTs) with Convolutional Neural Networks (ResNets).

Their analysis showed that:

  • CNNs exhibit a strictly hierarchical progression: early layers attend locally and exhibit low CKA similarity to deep layers, with global representations emerging only in the final stages.
  • ViTs leverage global self-attention from layer 1, establishing uniform representation structures where early, intermediate, and late layers maintain high CKA cross-similarity throughout the network.
+------------------------------------+------------------------------------+
| CNN Representation Pattern (ResNet)| ViT Representation Pattern (ViT-B) |
+------------------------------------+------------------------------------+
| Layer 1-8: Local edges, textures   | Layer 1-4: Mixed local/global info |
| Layer 9-24: Mid-level parts/shapes | Layer 5-10: Uniform representation |
| Layer 25-50: Global semantics      | Layer 11-12: Classifier alignment  |
| CKA(Layer 1, Layer 50) ≈ 0.15      | CKA(Layer 1, Layer 12) ≈ 0.65      |
+------------------------------------+------------------------------------+

3. Convergence Across Random Initializations

Prior to CKA, CCA-based metrics indicated that two identical networks trained from different random seeds developed virtually unrelated intermediate representations.

CKA disproved this conclusion: networks with identical architectures trained on identical data converge to nearly identical layer-to-layer similarity structures (CKA>0.85\text{CKA} > 0.85 across corresponding layers), proving that optimization consistently discovers the same geometric manifolds despite differing in exact neuron coordinates.

4. Overparameterization and Capacity Saturation

In excessively deep networks, CKA identifies representational saturation. When a network is deeper than necessary for a given task, the upper layers collapse into a massive monolithic block of near-identical representations (CKA>0.98\text{CKA} > 0.98), indicating that the additional depth contributes zero new functional transformations.


Computational Complexity and Minibatch Implementation

A naive implementation of Linear CKA computes the n×nn \times n Gram matrices K=XXTK = X X^T and L=YYTL = Y Y^T, incurring O(n2(p1+p2))O(n^2 (p_1 + p_2)) time complexity and O(n2)O(n^2) memory storage. When evaluating representations over large datasets (n50,000n \ge 50,000), storing n×nn \times n float32 matrices requires tens of gigabytes of VRAM.

The Matrix Factorization Trick (O(np1p2)O(n p_1 p_2))

Because Linear CKA depends only on the Frobenius norm of cross-covariance matrices, the calculation can be reorganized by centering the feature columns directly:

X~=X1n11TXandY~=Y1n11TY\tilde{X} = X - \frac{1}{n}\mathbf{1}\mathbf{1}^T X \quad \text{and} \quad \tilde{Y} = Y - \frac{1}{n}\mathbf{1}\mathbf{1}^T Y

The linear CKA numerator and denominator can then be computed via feature-dimension matrix multiplications:

HSIC(X~X~T,Y~Y~T)=1(n1)2Y~TX~F2\text{HSIC}(\tilde{X} \tilde{X}^T, \tilde{Y} \tilde{Y}^T) = \frac{1}{(n-1)^2} \|\tilde{Y}^T \tilde{X}\|_F^2

HSIC(X~X~T,X~X~T)=1(n1)2X~TX~F2\text{HSIC}(\tilde{X} \tilde{X}^T, \tilde{X} \tilde{X}^T) = \frac{1}{(n-1)^2} \|\tilde{X}^T \tilde{X}\|_F^2

+-------------------------------------------------------------------------------+
|                      COMPUTATIONAL COMPLEXITY COMPARISON                       |
+-----------------------------------+--------------------+----------------------+
| Method                            | Time Complexity    | Memory Footprint     |
+-----------------------------------+--------------------+----------------------+
| Naive Gram Matrix CKA             | O(n^2 * p)         | O(n^2)               |
| Matrix-Factorized Linear CKA      | O(n * p_1 * p_2)   | O(p_1 * p_2)         |
| Minibatch Unbiased CKA            | O(k * b * p_1*p_2) | O(b * p)             |
+-----------------------------------+--------------------+----------------------+

When nmax(p1,p2)n \gg \max(p_1, p_2), this formulation reduces memory from O(n2)O(n^2) to O(p1p2)O(p_1 p_2), allowing Linear CKA to run on millions of tokens across standard GPU hardware in seconds.

Unbiased Minibatch HSIC

For streaming evaluation or memory-constrained settings, Song et al. (2012) and Kornblith et al. (2019) formulated an unbiased estimator of HSIC over kk independent minibatches of size bb:

HSICunbiased(K,L)=1b(b3)(tr(K~L~)+1TK~11TL~1(b1)(b2)2b21TK~L~1)\text{HSIC}_{\text{unbiased}}(K, L) = \frac{1}{b(b-3)} \left( \text{tr}(\tilde{K} \tilde{L}) + \frac{\mathbf{1}^T \tilde{K} \mathbf{1} \mathbf{1}^T \tilde{L} \mathbf{1}}{(b-1)(b-2)} - \frac{2}{b-2} \mathbf{1}^T \tilde{K} \tilde{L} \mathbf{1} \right)

where K~=Kdiag(K)\tilde{K} = K - \text{diag}(K) sets the diagonal to zero. Computing CKA by averaging the unbiased numerator and denominators across minibatches eliminates sample-size bias without materializing global kernel matrices.


Practical Implementation in Python

Below is an efficient, vectorised PyTorch implementation of Linear CKA utilizing feature-level centering and Frobenius norm reductions:

import torch

def linear_cka(X: torch.Tensor, Y: torch.Tensor) -> float:
    """
    Computes Linear Centered Kernel Alignment (CKA) between two activation matrices.
    
    Args:
        X: Tensor of shape (n_samples, p1_features)
        Y: Tensor of shape (n_samples, p2_features)
        
    Returns:
        float: Linear CKA similarity score in [0.0, 1.0]
    """
    assert X.shape[0] == Y.shape[0], "Sample count n must match"
    
    # Cast to float64 to prevent numerical precision loss in norm calculation
    X = X.to(torch.float64)
    Y = Y.to(torch.float64)
    
    # Mean-center columns across samples: X_centered = X - mean(X)
    X_centered = X - X.mean(dim=0, keepdim=True)
    Y_centered = Y - Y.mean(dim=0, keepdim=True)
    
    # Compute cross-covariance and auto-covariance Frobenius norms
    # ||Y^T X||_F^2 = tr((Y^T X)(Y^T X)^T)
    cross_cov = torch.matmul(Y_centered.T, X_centered)
    hsic_xy = torch.sum(cross_cov ** 2)
    
    auto_cov_x = torch.matmul(X_centered.T, X_centered)
    hsic_xx = torch.sum(auto_cov_x ** 2)
    
    auto_cov_y = torch.matmul(Y_centered.T, Y_centered)
    hsic_yy = torch.sum(auto_cov_y ** 2)
    
    # Normalize HSIC
    denom = torch.sqrt(hsic_xx * hsic_yy)
    if denom == 0.0:
        return 0.0
        
    cka_score = hsic_xy / denom
    return float(cka_score.item())

Common Pitfalls and Best Practices

  1. Activation Pooling in Autoregressive LLMs: When evaluating Transformer language models, activations have shape (batch_size, seq_len, hidden_dim). Flattening tokens directly into n=batch×seq_lenn = \text{batch} \times \text{seq\_len} samples treats all token positions as independent examples. For sequence-level representations, mean-pooling or extracting the final non-padding token representation before computing CKA avoids position-correlation artifacts.
  2. Failure to Mean-Center Features: Omitting centering (H=InH = I_n) conflates the mean activation offset with geometric alignment. Uncentered Gram matrix alignment artificially inflates similarity scores for layers sharing large static bias vectors.
  3. Small Sample Regimes (n<pn < p): While CKA is substantially more robust than CCA when p>np > n, computing Linear CKA on small evaluation batches (n<200n < 200) introduces high variance. A sample size of n5×max(p1,p2)n \ge 5 \times \max(p_1, p_2) or at least n5,000n \ge 5,000 examples is recommended for stable layer-to-layer comparisons.
  4. Linear vs. RBF Kernels: Linear CKA is standard for comparing internal hidden activations because neural network layers perform linear transformations followed by element-wise activations. RBF (Gaussian) CKA is valuable when inspecting nonlinear manifolds or embeddings before non-linear projection heads, but requires calibrating kernel bandwidth σ\sigma (typically chosen as a fraction of the median pairwise distance).

Sources

Written by

More to read

  • LLM Evaluation Arenas in Production: Bradley-Terry Modeling, Active Matchmaking, Style Bias Control, and Bootstrapped Elo Calibration

    LLM Evaluation Arenas in Production: Bradley-Terry Modeling, Active Matchmaking, Style Bias Control, and Bootstrapped Elo Calibration Static benchmarks such as MMLU, GSM8K, and HumanEval face severe limitations in production machine learning environments. Modern foundation models rapidly saturate static multiple-choice questions, training datasets frequently suffer from benchmark contamination, and synthetic test suites fail to capture open-ended, multi-turn user intent. Consequently, engineeri

    1 min
  • Vector Compression in Production Search: Comparing SQ, PQ, and RaBitQ Architecture, Recall Retention, and Memory Economics

    In production Retrieval-Augmented Generation (RAG) systems and enterprise search platforms, storing raw floating-point embedding vectors in RAM quickly encounters hard hardware limits. A dataset of 100 million 1536-dimensional embeddings stored in FP32 requires over 614 GB of high-speed memory solely for vector coordinates, before accounting for index graph structures like HNSW or DiskANN. To scale similarity search to billions of vectors while keeping indices memory-resident, production vector

    1 min
  • Self-Rewarding Language Models: How Iterative DPO and LLM-as-a-Judge Form Autonomous Self-Alignment Loops

    Standard post-training alignment pipelines rely on frozen reward models trained on static human feedback datasets. While Reinforcement Learning from Human Feedback (RLHF) and Direct Preference Optimization (DPO) effectively steer model outputs toward human preferences, they face a fundamental scalability bottleneck: human annotators cannot evaluate superhuman reasoning or generate labels at the scale required for continuous self-improvement. Self-Rewarding Language Models, introduced by Meta AI

    1 min