蝦想承為工程師
  • NEW!
    NEW!  Connect Ideas Across Notes
    Save time and share insights. With Paragraph Citation, you can quote others’ work with source info built in. If someone cites your note, you’ll see a card showing where it’s used—bringing notes closer together.
    Got it
      • 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
    1
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ## 介紹 Temperature、Top-K 和 Top-P 在自然語言處理(NLP)領域,大型語言模型(LLM, Large Language Models)透過機率計算來選擇下一個最適合的文字,以生成符合語境的輸出,這些模型基於深度學習架構,特別是 Transformer 模型,透過大量文本數據的訓練,學會語言結構與語義關聯。 ![image](https://hackmd.io/_uploads/rJGmxjCiJl.png) 然而,為了讓模型的輸出更具變化性和可控性,研究者引入了一些調控機制,例如 Temperature、Top-K 和 Top-P,這些方法影響模型如何從可能的詞彙集合中選擇下一個字詞,使輸出更具靈活性與自然性,而上圖主要展示的是模型直接選擇具有最高機率的字詞,此作法也稱為「Greedy decoding」,但是單純依據最高機率選擇下一個詞彙可能導致生成的文字過於單調或缺乏創意。 ## Temperature Temperature 是一種直接影響語言模型隨機性的參數。當模型計算每個可能的下一個字的機率時,這些機率可以透過 Temperature 參數進行調整,可以從下圖看出在做 Softmax 時,會將 Temperature 參數作為預測值 $y_i$ 的分母項,進而影響機率分布的平滑度。 ![image](https://hackmd.io/_uploads/rk6mEhRsyx.png =100%x) ### Temperature 數值大小 當 Temperature 值較高(大於 1 或更大)時,機率分佈會變得更加平坦,意味著較低機率的單字也有較高的機會被選中,這使得生成的文本更加多樣,但同時也可能導致較多無關或不合理的詞語出現。相反地,當 Temperature 值較低(小於 1 或更小)時,機率較高的單字會更加突出,導致模型更傾向於選擇那些機率最高的詞,輸出的結果會更加可預測,但可能顯得死板或缺乏創意。當 Temperature 設為 1.0 時,則表示模型維持原始的機率分佈,不對其進行額外調整。 ![image](https://hackmd.io/_uploads/ryYVSn0oJl.png) ## Top-K Top-K 取樣(Top-K Sampling)是一種限制語言模型選擇範圍的方法,透過只考慮機率最高的 K 個單字,確保生成結果不會過於隨機,當模型計算出所有可能的單字及其對應機率後,它會將機率由高到低排序,並僅保留前 K 個單字,然後從這個限制範圍內進行隨機抽取。 例如,假設語言模型在某個步驟中預測下一個字時,有 50,000 個可能的選項,而其中 K 設為 10,那麼模型只會從機率最高的前 10 個字當中選擇,而忽略其餘的 49,990 個選項,這樣可以防止模型選擇過於罕見或不符合語境的字詞,從而提升文本的流暢性和可讀性。然而,K 值的選擇至關重要,如果 K 太小(如 3),可能會讓模型的輸出過於保守,缺乏變化;如果 K 太大(如 100),則可能會讓模型輸出較不連貫或不合語境的內容。 ## Top-P 與 Top-K 不同,Top-P 取樣(Top-P Sampling),也稱為「核取樣」(Nucleus Sampling),採用累積機率的方法來決定候選字詞的範圍,而非僅根據固定數量的 K 個單字來限制選擇範圍。這種方法會根據所有可能的字詞的機率分布,累積最前面的一部分機率總和,直到達到 P(又稱 nucleus probability)的閾值,然後從這些字詞中進行隨機選擇。 舉例來說,假設 P 設為 0.9,模型會從機率由高到低排序的單字中,累積機率值,直到總和達到 90%。在這個範圍內的單字將成為最終的候選單字,而機率最低的部分則會被忽略。這種方式能夠根據當下語境的機率分布自適應地決定選擇範圍,使得模型在不同情境下能夠展現不同程度的創造性與保守性。例如,在某些語境中,較少數的單字可能就能涵蓋 90% 的機率,這時候模型的選擇範圍較小,使輸出更具確定性;但在其他語境中,可能需要較多單字才能達到 90% 的機率總和,使輸出更具多樣性。 ![image](https://hackmd.io/_uploads/SykyJoRoJe.png) ## Temperature、Top-K 和 Top-P 的比較與應用 Temperature、Top-K 和 Top-P 這三種機制都可以控制語言模型的生成方式,但它們的作用方式有所不同。 1. Temperature: 直接影響機率分布的平滑度,適合用來調節輸出的隨機性與創意性。 2. Top-K: 限制了可能選擇的單字數量,使得輸出更加受控,但可能會忽略某些重要但機率較低的單字。 3. Top-P: 根據機率總和來確定選擇範圍,使得模型能夠動態適應不同的語境需求,提供更加靈活的選擇方式。 在應用上,不同的場景可能需要不同的參數設定。例如,在技術文件或數據生成時,通常會選擇較低的 Temperature(如 0.2 到 0.5)並結合較小的 Top-K(如 10),確保輸出的內容精確且符合邏輯。而在創意寫作或詩歌生成時,則可能會提高 Temperature(如 1.0 到 1.5),並結合較高的 Top-P(如 0.9),讓文本更具變化性與創意。此外,某些應用甚至會混合使用 Top-K 和 Top-P,透過設定一個較大的 K 值(如 50),再透過 Top-P 來進一步篩選,使模型的選擇範圍更加靈活。

    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