蝦想承為工程師
    • 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
    • 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
    • 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
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
  • 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
    ## 介紹詞向量(Word Embedding) 在自然語言處理(NLP)中,電腦無法直接理解人類語言,而是需要將文本轉換為數值表示,這就是詞向量(Word Embedding)的概念。 詞向量的目的是讓模型能夠處理和理解語言的語義與結構,進而應用於機器學習和深度學習模型中。隨著 NLP 的發展,出現了不同的詞向量表示方法,包括 One-Hot Encoding、詞袋模型(Bag of Words, BOW)、TF-IDF(詞頻 - 逆文件頻率) 和 Word2Vec。每種方法都有其特定的應用場景和優缺點,下面將對這些方法進行詳細解析。 ![image](https://hackmd.io/_uploads/ryAPJ4d91x.png) ## One-Hot Encoding:最基礎的詞向量表示 One-Hot Encoding 是 NLP 中最基本的詞表示方式,它的核心思想是,對於一個詞彙表(Vocabulary)中的每個單詞,使用一個與詞彙表大小相同的二進位向量來表示,只有對應單詞的位置為 1,其他位置全部為 0。 舉例來說,假設我們的詞彙表包含三個詞:"apple"、"banana"、"cherry",那麼它們的 One-Hot Encoding 如下: ![image](https://hackmd.io/_uploads/ByKh-mu9ke.png) ### 優點 * 簡單易懂:直接將單詞轉換為唯一的向量,直觀且易於實作。 * 無語義偏差:所有單詞的向量權重相同,沒有語義上的先驗假設。 ### 缺點 * 高維度與稀疏性問題:詞彙表的大小會影響向量的維度,對於大型語料(數十萬個單詞)來說,One-Hot Encoding 會產生高維稀疏矩陣,增加計算成本。 * 無語義信息:不同單詞的向量之間沒有任何關聯性,無法捕捉語義相似性,例如 "apple" 和 "banana" 在語義上相關,但其 One-Hot 向量完全不同。 * 無法處理新詞或未見詞(OOV, Out-of-Vocabulary) ## 詞袋模型(Bag of Words, BOW):基於詞頻的統計方法 詞袋模型(BOW)是一種統計詞頻的方法,它會忽略單詞的出現順序,只關心單詞的出現次數,即每個文檔的向量由詞彙表中的詞頻構成。透過詞頻統計可以得出某些詞的頻率較高,這些詞可能比只出現一次的詞更能代表文本的主題,在新聞分析、社交媒體趨勢分析等領域,這種排序方式可以幫助提取關鍵資訊,提高文本處理的效率。 假設我們有以下兩個句子: 1. "I love apples and bananas." 2. "Bananas are delicious." 並且詞彙表為 ["I", "love", "apples", "and", "bananas", "are", "delicious"],則這兩個句子的 BOW 向量為: ![image](https://hackmd.io/_uploads/B1x9QXuqJl.png) ### 優點 * 簡單且易於計算:只需計算詞頻,無需額外的訓練或詞向量學習。 * 適用於文本分類:如垃圾郵件分類、情感分析等,BOW 可以幫助識別文本中特定詞的頻率特徵。 ### 缺點 * 忽略單詞順序:無法區分「I love apples」與「Apples love I」,它們的向量表示是一樣的,這會影響語意理解。 * 無語義信息:不同的單詞之間沒有關聯性,例如 "apple" 和 "banana" 可能是同類詞,但 BOW 不會反映它們的關聯性。 * 高維度與稀疏性問題:與 One-Hot Encoding 相同,詞彙表的大小會影響向量維度,對大規模語料處理不友好。 * 無法處理新詞或未見詞(OOV, Out-of-Vocabulary) ## TF-IDF:衡量單詞重要性的加權方法 TF-IDF(Term Frequency-Inverse Document Frequency) 是對 BOW 的改進方法,它不僅考慮單詞在單一文件中的頻率(Term Frequency, TF),還考慮該單詞在整個語料庫中的重要性(Inverse Document Frequency, IDF)。 在大多數文本中,一些詞(如 "the"、"is"、"and")幾乎出現在所有文件中。如果僅依賴詞頻(TF)來衡量詞彙的重要性,這些高頻詞將主導計算結果,導致無法有效識別文件的核心內容。例如,在一份關於 AI 技術的文章中,"artificial intelligence" 這個詞組比 "is" 或 "the" 更具代表性,透過 IDF 降低這些常見詞的影響,使真正能夠代表文章主題的詞獲得更高的權重。 ![image](https://hackmd.io/_uploads/SyM1UmuqJg.png) TF:表示某個詞在特定文檔中的出現頻率。 IDF:衡量某個詞在整個語料庫中的重要性,例如某個詞在多數文件中都出現,則它的 IDF 會較低。 ### 案例說明 假設我們有以下三個文件: 文件 1(d₁):"I love apples and bananas." 文件 2(d₂):"Bananas are delicious and I love them." 文件 3(d₃):"Apples and oranges are tasty." Step 1:計算 TF, 'apples' 在文件 1(d₁)出現一次。 $TF('apples', d1​) = 1/5 = 0.2$ Step 2:計算 IDF, 'apples' 只在文件 1(d₁)和文件 3(d₃)中出現。 $IDF('apples', D) = log_e​(3/2) ​= 0.405$ Step3: 計算 TF-IDF $TF-IDF('apples', d, D)=0.2×0.405=0.081$ ### 優點 * 減少常見詞的影響:可有效降低像 "the"、"is" 這類高頻但無用詞的影響。 * 提升關鍵詞影響力:TF-IDF 使得較少出現但重要的詞更具影響力,適合資訊檢索與文本分類。 ### 缺點 * 仍然忽略詞序:如 "not happy" 和 "happy" 可能具有完全不同的語意,但 TF-IDF 會給它們類似的權重。 * 無法捕捉語義關聯:如 "apple" 和 "banana" 仍然無法建立語義上的聯繫。 * 無法處理新詞或未見詞(OOV, Out-of-Vocabulary) ## Word2Vec:語意關聯的詞向量學習方法 Word2Vec 是 Google 在 2013 年提出的一種詞向量學習方法,與傳統的詞袋模型(BOW)和 TF-IDF 不同,Word2Vec 能夠捕捉單詞之間的語義關係,並將單詞映射到一個低維度、密集的連續向量空間。這使得語義相似的單詞在向量空間中距離更近,例如 "king" 和 "queen" 會具有相似的向量表示,而 "apple" 和 "banana" 也會靠近彼此。 Word2Vec 的主要目標是學習一個能夠將單詞轉換為向量的數學模型,使得這些向量能夠反映語義和詞的關係,例如 vector("king") − vector("man") + vector("woman") ≈ vector("queen")。 ![image](https://hackmd.io/_uploads/SJTGRXO5yl.png) Word2Vec 通常使用神經網絡模型來學習詞向量,並且主要有兩種訓練方法: ![image](https://hackmd.io/_uploads/rJJPxNOq1e.png) ### CBOW(Continuous Bag of Words) CBOW 的基本思想是:根據上下文來預測中心詞。給定一個句子,CBOW 會使用周圍的單詞作為輸入,來預測中心詞。 例如,在以下句子中: "I love eating delicious apples in the morning." 如果選擇 "apples" 為目標詞(中心詞),則 CBOW 可能會使用它的上下文詞 "eating"、"delicious"、"in"、"the" 來進行預測: P("apples"∣"eating", "delicious", "in", "the") #### 優點 * 適合小型數據集:CBOW 模型透過上下文來預測單詞,這使得它在較小的數據集上比 Skip-Gram 更穩定。 * 訓練速度較快:由於 CBOW 使用多個上下文詞來預測中心詞,因此在相同的計算資源下,CBOW 模型通常比 Skip-Gram 訓練得更快。 #### 缺點 * 對低頻詞的學習效果較差:由於 CBOW 是基於上下文詞的平均值來學習中心詞的表示,它對於低頻詞的學習能力較弱。 * 可能會模糊多義詞的語境:例如 "bank" 可能表示 "銀行" 或 "河岸",但 CBOW 可能無法區分這些不同的語境。 ### Skip-Gram Skip-Gram 與 CBOW 的主要區別在於 Skip-Gram 是根據中心詞來預測上下文詞,而 CBOW 則是根據上下文詞來預測中心詞。 舉例來說,對於以下句子: "I love eating delicious apples in the morning." 如果選擇 "apples" 作為中心詞,Skip-Gram 會學習預測它的周圍詞 "eating"、"delicious"、"in"、"the",即: P("eating"∣"apples"), P("delicious"∣"apples"), P("in"∣"apples") #### 優點 * 適合大型數據集:Skip-Gram 在處理大規模語料時效果更好,特別是當語料庫包含許多罕見詞時。 * 對低頻詞的學習效果較佳:由於 Skip-Gram 會學習單詞與多個上下文詞之間的關係,因此它能夠更好地學習低頻詞的語義資訊。 * 對多義詞的學習能力更強:如果 "bank" 在不同的上下文中出現,Skip-Gram 會根據不同的上下文學習不同的詞向量,使其在語境理解方面更靈活。 #### 缺點 * 訓練速度較慢:由於 Skip-Gram 需要學習中心詞與每個上下文詞之間的關係,因此它的訓練時間比 CBOW 更長。 * 計算量較大:對於大規模語料庫,Skip-Gram 的計算開銷較大,可能需要更強的計算資源。 * 難以處理未見詞:如果測試語料中出現未見詞,則無法生成對應的向量。 :::info 雖然 Word2Vec 能夠為單詞學習高質量的詞向量,但在許多 NLP 應用中,我們需要處理的是整個句子、段落或文檔,而不是單獨的詞。而 AVG Word2Vec 能夠將任意長度的文本轉換為固定長度的向量,使其能夠更方便地應用於文本分類、資訊檢索、情感分析等任務。 ::: ## Avg Word2Vec AVG Word2Vec(Average Word2Vec)是一種將文本轉換為固定維度向量的技術,它基於 Word2Vec 訓練出的詞嵌入(Word Embedding),透過計算文本中所有詞向量的平均值,來獲得整個文本的語義表示,這種方法能夠將不同長度的文本轉換為固定大小的向量,並保持詞與詞之間的語意關係。 ### 案例說明 假設我們有一個句子:"I love natural language processing",並且我們有一個 3 維的 Word2Vec 詞向量(實際應用中通常是 100 維、300 維或更高維度): ![image](https://hackmd.io/_uploads/ByT5E4O9Jg.png) :::info Word2Vec 與 AVG Word2Vec 的比較 ![image](https://hackmd.io/_uploads/SJZMrVu51g.png) ::: --- :::info 以上就是這篇文章「詞向量(Word Embedding)」的所有內容,第一次看的人會花比較多時間消化吸收,這是很正常的事情,若有任何問題,歡迎在下方與我聯繫、討論,接下來也會繼續分享相關文章,敬請期待。

    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 Google 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