Yunchih Chen
    • Create new note
    • Create a note from template
      • Sharing URL Link copied
      • /edit
      • View mode
        • Edit mode
        • View mode
        • Book mode
        • Slide mode
        Edit mode View mode Book mode Slide mode
      • Customize slides
      • Note Permission
      • Read
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Write
        • Only me
        • Signed-in users
        • Everyone
        Only me Signed-in users Everyone
      • Engagement control Commenting, Suggest edit, Emoji Reply
    • Invite by email
      Invitee

      This note has no invitees

    • Publish Note

      Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

      Your note will be visible on your profile and discoverable by anyone.
      Your note is now live.
      This note is visible on your profile and discoverable online.
      Everyone on the web can find and read all notes of this public team.

      Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

      Explore these features while you wait
      Complete general settings
      Bookmark and like published notes
      Write a few more notes
      Complete general settings
      Write a few more notes
      See published notes
      Unpublish note
      Please check the box to agree to the Community Guidelines.
      View profile
    • Commenting
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
      • Everyone
    • Suggest edit
      Permission
      Disabled Forbidden Owners Signed-in users Everyone
    • Enable
    • Permission
      • Forbidden
      • Owners
      • Signed-in users
    • Emoji Reply
    • Enable
    • Versions and GitHub Sync
    • Note settings
    • Note Insights New
    • Engagement control
    • Make a copy
    • Transfer ownership
    • Delete this note
    • Save as template
    • Insert from template
    • Import from
      • Dropbox
      • Google Drive
      • Gist
      • Clipboard
    • Export to
      • Dropbox
      • Google Drive
      • Gist
    • Download
      • Markdown
      • HTML
      • Raw HTML
Menu Note settings Note Insights Versions and GitHub Sync Sharing URL Create Help
Create Create new note Create a note from template
Menu
Options
Engagement control Make a copy Transfer ownership Delete this note
Import from
Dropbox Google Drive Gist Clipboard
Export to
Dropbox Google Drive Gist
Download
Markdown HTML Raw HTML
Back
Sharing URL Link copied
/edit
View mode
  • Edit mode
  • View mode
  • Book mode
  • Slide mode
Edit mode View mode Book mode Slide mode
Customize slides
Note Permission
Read
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Write
Only me
  • Only me
  • Signed-in users
  • Everyone
Only me Signed-in users Everyone
Engagement control Commenting, Suggest edit, Emoji Reply
  • Invite by email
    Invitee

    This note has no invitees

  • Publish Note

    Share your work with the world Congratulations! 🎉 Your note is out in the world Publish Note No publishing access yet

    Your note will be visible on your profile and discoverable by anyone.
    Your note is now live.
    This note is visible on your profile and discoverable online.
    Everyone on the web can find and read all notes of this public team.

    Your account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Your team account was recently created. Publishing will be available soon, allowing you to share notes on your public page and in search results.

    Explore these features while you wait
    Complete general settings
    Bookmark and like published notes
    Write a few more notes
    Complete general settings
    Write a few more notes
    See published notes
    Unpublish note
    Please check the box to agree to the Community Guidelines.
    View profile
    Engagement control
    Commenting
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    • Everyone
    Suggest edit
    Permission
    Disabled Forbidden Owners Signed-in users Everyone
    Enable
    Permission
    • Forbidden
    • Owners
    • Signed-in users
    Emoji Reply
    Enable
    Import from Dropbox Google Drive Gist Clipboard
       Owned this note    Owned this note      
    Published Linked with GitHub
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # AST26: Product Quantization > 這是 AST26 進階儲存系統的課程共筆,歡迎所有同學共同編輯與補充。 # 範例 完整走一遍,數字都簡化。 ### 設定 - 資料庫有 12 筆 vector - IVF 分成 3 個 cluster - PQ 切成 2 段,每段有 4 個 centroid --- ### 建 index 階段 **Step 1:IVF 分群** K-means 把 12 筆資料分成 3 個 cluster: ``` Cluster 0(運動類):vector A, B, C, D Cluster 1(食物類):vector E, F, G, H Cluster 2(科技類):vector I, J, K, L ``` 每個 cluster 有一個 centroid 代表它的中心位置。 **Step 2:PQ 壓縮每筆資料** 每筆 vector 壓縮成 compressed vector 存進自己的 cluster: ``` Cluster 0:A→[2,1], B→[3,0], C→[1,2], D→[0,3] Cluster 1:E→[1,1], F→[2,3], G→[0,2], H→[3,1] Cluster 2:I→[0,1], J→[1,3], K→[2,0], L→[3,2] ``` --- ### 查詢階段 query 進來,假設是「籃球」相關的 vector。 **Step 1:IVF 找最近的 cluster** query 跟 3 個 cluster centroid 算距離: ``` Cluster 0(運動類)centroid → 距離 0.2 ← 最近 Cluster 1(食物類)centroid → 距離 0.8 Cluster 2(科技類)centroid → 距離 0.9 ``` nprobe = 1,所以只看 Cluster 0,跳過 Cluster 1 和 2。 **Step 2:ADC 建 lookup table** query 切成 2 段,各跟 4 個 centroid 算距離: ``` centroid 0 centroid 1 centroid 2 centroid 3 第1段 0.8 0.3 0.5 0.7 第2段 0.6 0.2 0.9 0.4 ``` 這裡的 centroid 0~3 是 PQ codebook 的 centroid, 不是 IVF 分群用的那 3 個 cluster centroid,兩者完全獨立。 **Step 3:查表算 Cluster 0 裡每筆的距離** ``` A [2,1] → 第1段查centroid 2→0.5,第2段查centroid 1→0.2,總距離=0.7 B [3,0] → 第1段查centroid 3→0.7,第2段查centroid 0→0.6,總距離=1.3 C [1,2] → 第1段查centroid 1→0.3,第2段查centroid 2→0.9,總距離=1.2 D [0,3] → 第1段查centroid 0→0.8,第2段查centroid 3→0.4,總距離=1.2 ``` **Step 4:回傳結果** 距離最小的是 A(0.7),回傳 A。 --- ### 總結分工 ``` IVF:12筆 → 只看4筆(跳過8筆) PQ+ADC:4筆 × 2次lookup = 8次查表就算完所有距離 ``` (IVF) + (PQ + ADC)兩個合在一起,又少看資料,又快速計算。 # 再來用幾何的角度重新解釋。 每筆資料的 sub-vector,在子空間裡是一個**點**。 Codebook 的 256 個 centroid,是這個子空間裡的 **256 個代表點**。 ``` 壓縮 = 把這個點「捨入」到最近的代表點 ``` 就像地圖上你的位置是精確座標,但你說「我在台北車站附近」——用一個代表地點取代精確位置。 --- ### ADC 的幾何意義 查詢時真正想算的是: ``` query 的第1段 ←→ 資料A 的第1段 (精確距離) ``` 但資料 A 的第1段已經被壓縮成 centroid 2 了,所以改成算: ``` query 的第1段 ←→ centroid 2 (近似距離) ``` 幾何上就是這樣: ![image](https://hackmd.io/_uploads/r12XYCba-e.png) ### 關鍵幾何概念 資料 A 已經被壓縮成 centroid 2 了,所以: ``` query 到 資料A 的距離 ≈ query 到 centroid 2 的距離 ``` 這個近似成立,是因為資料 A 本來就在 centroid 2 附近(當初就是因為最近才被分到這群)。 而 query 到 centroid 2 的距離,**建 lookup table 的時候就算好了**,直接查就好。 --- ### 所以 lookup table 的幾何意義 > 事先把 query 到每個 centroid 的距離都算好存起來,之後對每筆資料只需要查「它用的是哪個 centroid」,就能知道近似距離。 ---

    Import from clipboard

    Paste your markdown or webpage here...

    Advanced permission required

    Your current role can only read. Ask the system administrator to acquire write and comment permission.

    This team is disabled

    Sorry, this team is disabled. You can't edit this note.

    This note is locked

    Sorry, only owner can edit this note.

    Reach the limit

    Sorry, you've reached the max length this note can be.
    Please reduce the content or divide it to more notes, thank you!

    Import from Gist

    Import from Snippet

    or

    Export to Snippet

    Are you sure?

    Do you really want to delete this note?
    All users will lose their connection.

    Create a note from template

    Create a note from template

    Oops...
    This template has been removed or transferred.
    Upgrade
    All
    • All
    • Team
    No template.

    Create a template

    Upgrade

    Delete template

    Do you really want to delete this template?
    Turn this template into a regular note and keep its content, versions, and comments.

    This page need refresh

    You have an incompatible client version.
    Refresh to update.
    New version available!
    See releases notes here
    Refresh to enjoy new features.
    Your user state has changed.
    Refresh to load new user state.

    Sign in

    Forgot password
    or
    Sign in via Facebook Sign in via X(Twitter) Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    By signing in, you agree to our terms of service.

    Help

    • English
    • 中文
    • Français
    • Deutsch
    • 日本語
    • Español
    • Català
    • Ελληνικά
    • Português
    • italiano
    • Türkçe
    • Русский
    • Nederlands
    • hrvatski jezik
    • język polski
    • Українська
    • हिन्दी
    • svenska
    • Esperanto
    • dansk

    Documents

    Help & Tutorial

    How to use Book mode

    Slide Example

    API Docs

    Edit in VSCode

    Install browser extension

    Contacts

    Feedback

    Discord

    Send us email

    Resources

    Releases

    Pricing

    Blog

    Policy

    Terms

    Privacy

    Cheatsheet

    Syntax Example Reference
    # Header Header 基本排版
    - Unordered List
    • Unordered List
    1. Ordered List
    1. Ordered List
    - [ ] Todo List
    • Todo List
    > Blockquote
    Blockquote
    **Bold font** Bold font
    *Italics font* Italics font
    ~~Strikethrough~~ Strikethrough
    19^th^ 19th
    H~2~O H2O
    ++Inserted text++ Inserted text
    ==Marked text== Marked text
    [link text](https:// "title") Link
    ![image alt](https:// "title") Image
    `Code` Code 在筆記中貼入程式碼
    ```javascript
    var i = 0;
    ```
    var i = 0;
    :smile: :smile: Emoji list
    {%youtube youtube_id %} Externals
    $L^aT_eX$ LaTeX
    :::info
    This is a alert area.
    :::

    This is a alert area.

    Versions and GitHub Sync
    Get Full History Access

    • Edit version name
    • Delete

    revision author avatar     named on  

    More Less

    Note content is identical to the latest version.
    Compare
      Choose a version
      No search result
      Version not found
    Sign in to link this note to GitHub
    Learn more
    This note is not linked with GitHub
     

    Feedback

    Submission failed, please try again

    Thanks for your support.

    On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?

    Please give us some advice and help us improve HackMD.

     

    Thanks for your feedback

    Remove version name

    Do you want to remove this version name and description?

    Transfer ownership

    Transfer to
      Warning: is a public team. If you transfer note to this team, everyone on the web can find and read this note.

        Link with GitHub

        Please authorize HackMD on GitHub
        • Please sign in to GitHub and install the HackMD app on your GitHub repo.
        • HackMD links with GitHub through a GitHub App. You can choose which repo to install our App.
        Learn more  Sign in to GitHub

        Push the note to GitHub Push to GitHub Pull a file from GitHub

          Authorize again
         

        Choose which file to push to

        Select repo
        Refresh Authorize more repos
        Select branch
        Select file
        Select branch
        Choose version(s) to push
        • Save a new version and push
        • Choose from existing versions
        Include title and tags
        Available push count

        Pull from GitHub

         
        File from GitHub
        File from HackMD

        GitHub Link Settings

        File linked

        Linked by
        File path
        Last synced branch
        Available push count

        Danger Zone

        Unlink
        You will no longer receive notification when GitHub file changes after unlink.

        Syncing

        Push failed

        Push successfully