---
title: 'Stacked Quantizers: Hierarchical Vector Quantization'
url: https://www.emergentmind.com/topics/stacked-quantizers-sq
type: topic
---

# Stacked Quantizers: Hierarchical Vector Quantization

Stacked Quantizers (SQ) are a hierarchical approach to compositional vector quantization designed to achieve low reconstruction error comparable to fully dependent quantization schemes, while retaining computational efficiency that approaches methods based on codebook independence. SQ introduces a coarse-to-fine structure in subcodebooks, enabling efficient deterministic encoding, scalable training, and strong empirical performance across multiple descriptor types and benchmarks, particularly in image retrieval, nearest neighbour search, and classification with compressed features [1411.2173].

## 1. Problem Setting and Background

Vector quantization seeks to encode high-dimensional data $X \subset \mathbb{R}^d$ using a compact codebook $C \in \mathbb{R}^{d \times k}$ and one-hot codes $b \in \{0,1\}^k$ (with $\|b\|_0 = \|b\|_1 = 1$) minimizing average reconstruction error:
\[
\min_{C,B}\;\frac{1}{n}\sum_{j=1}^n\Big\lVert x_j - C\,b_j\Big\rVert_2^2
\]
Compositional quantization generalizes traditional schemes by approximating each vector $x \in \mathbb{R}^d$ with a sum over $m$ codes from $m$ smaller subcodebooks:
\[
x \approx \sum_{i=1}^m C_i b_i, \quad b_i \in \{0,1\}^h, \; \|b_i\|_0 = 1
\]
resulting in $h^m$ representable clusters. Key approaches within this framework include Product Quantization (PQ) and Additive Quantization (AQ). PQ imposes strict orthogonality on subcodebooks, yielding efficient encoding at the cost of representational power. AQ removes independence constraints for improved reconstruction fidelity but renders encoding NP-hard, necessitating beam search or other heuristics. SQ establishes a middle ground by exploiting a hierarchical, residual-based structure.

## 2. Hierarchical Structure and Encoding Procedure

Stacked Quantizers construct $m$ subcodebooks $(C_1, C_2, \ldots, C_m)$ arranged in a hierarchy. Encoding proceeds in a greedy, coarse-to-fine manner:
1. Assign $b_1 \gets \arg\min_b \|x - C_1 e_b\|^2$ (select codeword from $C_1$ to minimize residual).
2. Compute residual $r_1 = x - C_1 b_1$.
3. Assign $b_2 \gets \arg\min_b \|r_1 - C_2 e_b\|^2$; update residual $r_2 = r_1 - C_2 b_2$.
4. Repeat through $b_m$, with the final residual $r_m$ representing global quantization error.

Because each encoding step only requires search over $h$ centroids in $\mathbb{R}^d$, per-vector encoding complexity is $\mathcal{O}(m h d)$. This is only a constant factor above PQ but several orders of magnitude faster than AQ's beam search, which requires $\mathcal{O}(m^3 b h d)$ per encoding.

## 3. Codebook Training and Refinement Strategy

Training for Stacked Quantizers comprises two phases:

- **Initialization (Sequential $k$-means):** 
  - Apply standard $k$-means clustering to $X$ to form $C_1$ and corresponding codes $B_1$.
  - Compute residuals $R_1 = X - C_1 B_1$.
  - Iteratively, apply $k$-means to each subsequent residual to yield $C_2$, $C_3$, ..., $C_m$ and their codes, each time recomputing residuals.
  - Total complexity is $\mathcal{O}(m n h d i)$ for $i$ iterations per level.

- **Hierarchical Refinement (Coordinate Descent):**
  - For each codebook $C_i$:
    - Remove $C_i$'s contribution, recompute residuals.
    - Reassign $B_i$ using greedy encoding on these updated residuals.
    - Update codebook $C_i$ via a single $k$-means pass.
  - Each refinement pass operates in $\mathcal{O}(m^2 h d)$, substantially below AQ’s encoding cost.

This scheme maintains the hierarchical structure and allows regularized, top-down codebook improvement. SQ’s codebooks are typically better initialized and refined than AQ, which can result in competitive or superior empirical results despite the hierarchical constraint.

## 4. Complexity Comparison

A comparison of computational complexities across compositional quantization methods is presented below:

| Method | Per-vector Encoding | Training on $n$ Samples |
|--------|---------------------|------------------------|
| PQ     | $\mathcal{O}(h d)$      | $\mathcal{O}(n h d i)$    |
| AQ     | $\mathcal{O}(m^3 b h d)$   | $\mathcal{O}(T m^3 b h d)$  |
| SQ     | $\mathcal{O}(m h d)$    | $\mathcal{O}(m n h d i) + \mathcal{O}(T' m^2 h d)$  |

Here, $b$ (for AQ) is the beam width, $T$ and $T'$ are the number of refinement iterations, and $i$ refers to $k$-means iterations. SQ achieves encoding and training costs within a modest constant factor of PQ, while avoiding the intractability of AQ for large-scale datasets.

## 5. Empirical Evaluation and Results

SQ was evaluated on three million-scale datasets: SIFT1M (128-D hand-crafted), GIST1M (960-D hand-crafted), and ConvNet1M-128 (128-D deep CNN features). For code lengths of 16, 32, 64, and 128 bits ($h=256$ per subcodebook), key findings include:

- **Quantization Error:** SQ matches or improves AQ’s error on SIFT1M and GIST1M, and achieves up to $10\%$ lower error than AQ on ConvNet1M-128 at longer code lengths.
- **Approximate Nearest Neighbours (Recall@$N$):** At 32 bits, SQ provides the highest recall across $N$ for SIFT1M and GIST1M; on ConvNet1M-128, SQ remains competitive with AQ and outperforms PQ/OPQ, particularly as code length increases.
- **Classification with Compressed Features:** On ILSVRC-2012 deep feature compression, both SQ and AQ exhibit more graceful efficacy degradation than PQ/OPQ as code length shrinks; for example, at 32 bits, PQ/OPQ top-5 error can exceed $40\%$, while SQ/AQ remain around $25–30\%$.
- **Running Time (ConvNet1M-128, 8 codebooks = 64 bits):**
    - Training: PQ/OPQ (100 $k$-means iters): $4.8–5.6$ min; SQ (init + 100 refinements): $\approx 42$ min; AQ/APQ (beam search): $\approx 2.7$ h.
    - Database Encoding: PQ/OPQ: $\sim 5$ s; SQ: $\sim 20$ s; AQ/APQ: $\sim 9.2$ h.

These results substantiate SQ’s ability to deliver strong quantization fidelity at near-PQ computational efficiency.

## 6. Practical Considerations and Limitations

Stacked Quantizers offer state-of-the-art quantization error and search accuracy with scalable, deterministic, and easily parallelizable greedy encoding. Training, particularly refinement, is more costly than for PQ, but remains an offline process and maintains feasibility for large datasets (encoding $>1$ million vectors in under one minute). Although the hierarchical structure theoretically may omit certain cross-subcodebook dependencies fully modeled by AQ, in practice, SQ frequently matches or surpasses AQ performance due to better initialization and refinement regimes.

SQ integrates with asymmetric distance computation and supports deployment within inverted-index or multi-index architectures for sublinear approximate nearest neighbour search. 

A plausible implication is that SQ’s hierarchical design strikes a practical balance: it achieves near-optimal trade-offs between computational scalability and quantization error, making it suitable for high-performance, large-scale visual search systems and learning scenarios requiring compressed representations [1411.2173].

Source: https://www.emergentmind.com/topics/stacked-quantizers-sq