0Pricing
LangChain / RAG / Vector DBs · บทเรียน

การควอนไทซ์และการบีบอัดเวกเตอร์

ลดพื้นที่จัดเก็บเวกเตอร์และเร่งการค้นหาด้วยการควอนไทซ์แบบสเกลาร์และแบบผลิตภัณฑ์ โดยควบคุมการสูญเสียความแม่นยำ

การควอนไทซ์และการบีบอัดเวกเตอร์ เป็นบทเรียน LangChain / RAG / Vector DBs ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 GB

Scalar 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))  # 165

Dequantization

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.294

Product 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

คำถามที่พบบ่อย

บทเรียน “การควอนไทซ์และการบีบอัดเวกเตอร์” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การควอนไทซ์และการบีบอัดเวกเตอร์” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส LangChain / RAG / Vector DBs ให้อัปเกรดเป็น CoddyKit PRO คอร์ส LangChain / RAG / Vector DBs มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การควอนไทซ์และการบีบอัดเวกเตอร์”

ลดพื้นที่จัดเก็บเวกเตอร์และเร่งการค้นหาด้วยการควอนไทซ์แบบสเกลาร์และแบบผลิตภัณฑ์ โดยควบคุมการสูญเสียความแม่นยำ คุณปฏิบัติ LangChain / RAG / Vector DBs ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน LangChain / RAG / Vector DBs หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน LangChain / RAG / Vector DBs บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การควอนไทซ์และการบีบอัดเวกเตอร์” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน LangChain / RAG / Vector DBs นี้ได้ไหม

ได้ บทเรียน LangChain / RAG / Vector DBs ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. สถาปัตยกรรมการจัดเก็บฐานข้อมูลเวกเตอร์
  2. อัลกอริทึมการค้นหาเพื่อนบ้านใกล้เคียง (HNSW, IVFFlat)
  3. ความคงทนและการรองรับการขยายตัวของฐานข้อมูลเวกเตอร์
  4. การควอนไทซ์และการบีบอัดเวกเตอร์
← กลับไปที่ LangChain / RAG / Vector DBs