ベクトルの量子化と圧縮
スカラー量子化と積量子化を使ってベクトルの保存容量を削減し、精度の低下を抑えながら検索を高速化します。
「ベクトルの量子化と圧縮」はCoddyKit上の無料LangChain / RAG / Vector DBsレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはLangChain / RAG / Vector DBs学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 LangChain / RAG / Vector DBsコースには全4レッスンが含まれています。
このレッスンの一部はまだ翻訳されておらず、英語で表示されています。
The Memory Problem
A million 1536-dimension vectors stored as 32-bit floats need about 6 GB of RAM. Quantization compresses vectors so they fit in far less memory and search faster.
Float32 Baseline
By default each dimension is a 4-byte float. Storage equals vectors x dims x 4 bytes. Reducing the bytes per dimension is the path to compression.
vectors = 1_000_000
dims = 1536
bytes_total = vectors * dims * 4
print(bytes_total / 1e9, "GB") # ~6.14 GBScalar Quantization
Scalar quantization maps each float to an 8-bit integer using the min and max range of that dimension. This is a 4x reduction with modest accuracy loss.
def quantize(x, lo, hi):
span = hi - lo
return round((x - lo) / span * 255)
print(quantize(0.3, -1.0, 1.0)) # 165Dequantization
To compare vectors you can dequantize back to an approximate float, or compute distances directly in integer space for speed.
def dequantize(q, lo, hi):
return lo + (q / 255) * (hi - lo)
print(round(dequantize(165, -1.0, 1.0), 3)) # ~0.294Product Quantization (PQ)
PQ splits each vector into sub-vectors and replaces each with the id of its nearest centroid from a small learned codebook. Compression can reach 16x or more.
How PQ Encodes
For 8 sub-vectors with 256 centroids each, every vector becomes 8 bytes regardless of original dimension. Distances are estimated from precomputed centroid tables.
Binary Quantization
The most aggressive option keeps only the sign of each dimension: positive becomes 1, negative becomes 0. A 1536-dim vector fits in 192 bytes and uses fast Hamming distance.
def binarize(vec):
return [1 if v > 0 else 0 for v in vec]
print(binarize([0.4, -0.1, 0.9, -2.0])) # [1, 0, 1, 0]The Accuracy Tradeoff
More compression means more approximation error. Measure recall against an uncompressed baseline to ensure the quality loss is acceptable for your use case.
Rescoring with Full Vectors
A common pattern: search fast with quantized vectors to get a candidate set, then rescore the top candidates using the original float vectors for precision.
candidates = quantized_search(query, k=100)
rescored = sorted(
candidates,
key=lambda c: exact_distance(query, full_vec[c]),
)[:10]Configuring in a Store
Production stores expose quantization as a collection setting. You pick the type and any rescore depth at index creation.
# pseudo-config
collection.create(
vectors={"size": 1536, "distance": "Cosine"},
quantization={"scalar": {"type": "int8"}},
)Choosing a Strategy
Start with scalar quantization for an easy 4x win. Move to PQ or binary only when memory is critical and you can afford rescoring to recover accuracy.
Quick Check
Test your understanding of vector compression.
Recap
You explored vector compression:
- Scalar quantization: float to int8, 4x smaller
- PQ: codebook ids, big compression
- Binary: sign bits, Hamming distance
- Rescore with full vectors to regain accuracy
よくある質問
「ベクトルの量子化と圧縮」レッスンは無料ですか?
はい。「ベクトルの量子化と圧縮」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、LangChain / RAG / Vector DBsコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 LangChain / RAG / Vector DBsコースには全4レッスンが含まれています。
「ベクトルの量子化と圧縮」で何を学びますか?
スカラー量子化と積量子化を使ってベクトルの保存容量を削減し、精度の低下を抑えながら検索を高速化します。 ブラウザで直接実行するハンズオンコードでLangChain / RAG / Vector DBsを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。
LangChain / RAG / Vector DBsを始めるのに経験は必要ですか?
事前経験は必要ありません。CoddyKitのLangChain / RAG / Vector DBsは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。
「ベクトルの量子化と圧縮」レッスンにはどのくらい時間がかかりますか?
ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。
このLangChain / RAG / Vector DBsレッスンでコードを書いて実行できますか?
はい。すべてのLangChain / RAG / Vector DBsレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。