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 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

From Vectors to Meaning

An embedding maps text to a list of numbers in high-dimensional space. Texts with similar meaning land close together. To rank results we need a way to measure that closeness.

Cosine Similarity

Cosine similarity measures the angle between two vectors, ignoring their length. It ranges from -1 (opposite) to 1 (identical direction).

import numpy as np

def cosine(a, b):
    a, b = np.array(a), np.array(b)
    return a.dot(b) / (np.linalg.norm(a) * np.linalg.norm(b))

print(cosine([1, 0], [1, 1]))  # ~0.707

Euclidean Distance

Euclidean (L2) distance is the straight-line distance between two points. Smaller means more similar. Unlike cosine, it is sensitive to magnitude.

import numpy as np

def l2(a, b):
    return np.linalg.norm(np.array(a) - np.array(b))

print(l2([0, 0], [3, 4]))  # 5.0

Dot Product

The dot product multiplies matching dimensions and sums them. For normalized vectors it equals cosine similarity, which is why many stores normalize first.

import numpy as np

def dot(a, b):
    return float(np.array(a).dot(np.array(b)))

print(dot([1, 2, 3], [4, 5, 6]))  # 32.0

Normalization

Dividing a vector by its length gives a unit vector. After normalization, dot product and cosine similarity become equivalent, simplifying the math.

import numpy as np

def normalize(v):
    v = np.array(v, dtype=float)
    return v / np.linalg.norm(v)

print(normalize([3, 4]))  # [0.6 0.8]

Choosing a Metric

Most modern text embedding models are trained for cosine similarity. Use cosine unless your provider documentation recommends otherwise.

  • Cosine: direction matters, length ignored
  • L2: absolute position matters
  • Dot: cosine on normalized data

Similarity vs. Distance

Beware the inversion: higher cosine = more similar, but higher L2 = less similar. Vector stores expose this difference, sometimes returning a score you must interpret.

Why High Dimensions Help

Embeddings often have hundreds or thousands of dimensions. More dimensions give the model room to separate subtle differences in meaning, at the cost of more storage and compute.

Setting Metric in a Store

When creating a collection you declare the metric. Many libraries default to cosine.

import chromadb

client = chromadb.Client()
col = client.create_collection(
    name="docs",
    metadata={"hnsw:space": "cosine"}
)

Ranking Search Results

Search computes the chosen metric between the query embedding and every stored vector, then returns the top-k closest. The metric directly shapes which documents win.

query_vec = embed("refund policy")
scored = [(cosine(query_vec, d.vec), d) for d in docs]
scored.sort(reverse=True)
top3 = scored[:3]

Pitfall: Mixing Models

Vectors from different embedding models live in different spaces and are not comparable. Always embed your query with the same model you used to index the documents.

Quick Check

Test your grasp of similarity metrics.

Recap

You explored how similarity is measured:

  • Cosine compares direction (most common for text)
  • Euclidean compares position
  • Dot product equals cosine on normalized vectors
  • Always query and index with the same model

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

บทเรียน “การวัดความคล้ายคลึงของเวกเตอร์ฝังความหมาย” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การวัดความคล้ายคลึงของเวกเตอร์ฝังความหมาย” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ 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. บทนำสู่ฐานข้อมูลเวกเตอร์
  3. การจัดเก็บและเรียกคืนเวกเตอร์แทนความหมาย
  4. การวัดความคล้ายคลึงของเวกเตอร์ฝังความหมาย
← กลับไปที่ LangChain / RAG / Vector DBs