蝦想承為工程師
    • 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

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

    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.
    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
    ## 什麼是模型? 延續上一篇文章所討論到,模型(model)就是帶有未知數的函式,例如線性模型 $ŷ = w*x + b$,具有兩種不同的未知參數(parameters)分別是 w, b,在監督式學習(Supervised learning)裡定義了 x 為 feature、y 為 label。 :::info 監督式學習(Supervised learning):從帶有標籤(label)的訓練資料進行學習,每個資料點(x)都與相應的目標或輸出標籤(y)相關聯,常見的類型包括分類和迴歸。 非監督式學習(Unsupervised learning):從無標籤的資料中學習,旨在發現資料中的模式、關係或結構,而無需預先定義目標變數,常見的類型包括聚類和降維。 ::: 但是,線性模型在大多數的狀況下過於簡單,無法完全代表訓練資料間的關係,真實的資料分布可能較接近拋物線或是其他更複雜的形狀,此種由於模型過於簡單而不足以充分地描述資料間的關係,稱為「Model bias」。 若欲解決 Model bias 的問題,可以嘗試增加 x(features)的數量和模型參數量,也就是擴大模型的彈性,使得模型能夠更貼近資料分布,從下圖可看到,從原本簡單的線性模型 $ŷ = w*x₁ + b$ 到非線性模型 $ŷ = σ(z)$,大幅降低發生 Model bias 的可能性。 ![image](https://hackmd.io/_uploads/ryNC32nKkg.png) ## 欲找到理想函式前,先挑選誤差函數(Loss function) 什麼是誤差函數呢?誤差函數是在選定其中一個模型(model)後,根據訓練資料的輸入(或是 features) x 丟到模型中,模型會預測結果 ŷ,而誤差函數的目的為計算模型的預測結果(ŷ)與正確答案(或是輸出標籤 y)間的落差,該落差越小越好。 常見誤差函數的計算方式有平均絕對誤差(Mean Absolute Error, MAE)、均方誤差(Mean Square Error, MSE)、以及交叉熵(Cross-Entropy)。 ![image](https://hackmd.io/_uploads/r1h5oQzBxg.png =70%x) :::info 特別解釋交叉熵(Cross-Entropy),在分類問題中,我們希望模型可以區隔不同類別的資料,交叉熵就是用來衡量模型預測的概率分布與實際的概率分布之間的差異,其中 yᵢ 是實際的類別概率,ŷᵢ 是預測的類別概率。 ::: ## 選定模型以及誤差函數後,就剩最佳化(Optimization) 最佳化(Optimization),簡單來說就是找到一組最佳的參數,讓誤差函數(Loss function)產生的數值越小越好。 還記得前面我們說的嗎?兩種參數分別是 w, b,但這兩種參數目前都是未知數,對於一組模型來說,可能有成千上萬的參數量, 更不用說是深度學習的架構,以 GPT-3 的參數量就高達 1750 億個。 那怎麼找到一組好的參數,讓誤差函數的數值變小呢? 以下介紹三種最佳化方法 ### 1. Gradient descent(警告,有數學公式出沒,請小心服用!) 如果把 loss function 的數值呈現在二維座標上,橫軸為模型參數 w、縱軸為 loss 數值,可以從下圖看到會是一條高高低低的曲線,目的是希望曲線上的那顆球能夠順利地滾到最低點(Global minima),獲得到最小的數值,這告訴我們一件事,相信你也想到了,就是要找到偏微分 = 0 的地方,在全域最大/小值或是局部最大/小值時,切線斜率為零,運用這個技巧,你就可以找到讓 loss 為最小值的參數,但是請特別再留意一件事,切線斜率為零的地方,可能是局部最小值,而不是全域最小值喔。 ![image](https://hackmd.io/_uploads/S1zVrPMHxl.png) 隨著 loss 變小的方向,不斷更新參數值,這裡有一個新的符號叫做 η(Learning rate),負責控制更新一次的步伐大小,η 越大能夠走的就越大步,而前面有負號是因為切線斜率為負,負負得正,才能使得 w 可以向右更新,那什麼時候不再更新參數值呢?沒錯,就是偏微分為零(也就是斜率為零)的地方。 ![image](https://hackmd.io/_uploads/BkIujqxNC.png) 最佳化(Optimization)的目的就是為了找到一組參數,使得 loss function 產生的數值越小越好,從下圖看到這組數值分別是 w*, b*,便成功利用 gradient descent 找到模型(model)的最佳參數,使得機器預測的結果(ŷ)與正確答案(或是輸出標籤 y)之間的落差達到理想上的最小值。 ![image](https://hackmd.io/_uploads/HybKoqxN0.png =50%x) ### 2. Stochastic gradient descent(SGD) 以下會著重探討 Stochastic gradient descent(SGD) 與 Gradient descent(GD) 兩者間的差異。 其中,最顯著的差異在於更新參數的頻率不同,GD 是看完了所有的訓練資料才更新參數值一次,而 SGD 是看了一筆訓練資料就更新一次,也就是說如果有 1000 筆資料,利用 GD 的方式更新參數只會讓參數值向前邁出一步(更新一次),而 SGD 的方式會讓參數值向前邁出一千步(更新一千次)。 這裡有需要學習的新名詞,看完一次所有的訓練資料,稱之為一個「epoch」,而將資料切分成好幾等分,例如總共有 1000 筆資料,切分成十等分,每一等分裡就有 100 筆資料,這 100 筆資料稱為一個「batch」,因此總共會有十個 batch,當然要切分成多大(例如每一等分 200 筆資料)或多小(每一等分 20 筆資料)可以自行決定,稱之為「batch size」。 ![image](https://hackmd.io/_uploads/BkU-bO-4R.png) ### 3. Mini-batch SGD GD 是看完所有訓練資料後更新一次參數,SGD 是看了一筆訓練資料後就更新一次參數,有沒有不要那麼極端的呢?有,就是接下來要解釋的 Mini-batch SGD。 從下圖看到,將資料切分成好幾個 batch,每一個 batch 裡頭有 B 筆資料稱為 batch size,看完了 B 筆資料後才更新一次參數,是與上述兩個最大不同之處。 ![image](https://hackmd.io/_uploads/r1Bx8OZEC.png) 現在肯定會有一個疑惑,到底該用哪一個最佳化(Optimization)方法比較好呢?(紅色標記文字為優勢處) | | Small bacth | Large batch | | -------- | -------- | -------- | | 參數更新速度(無平行運算) | 較快 | 較慢 | | 參數更新速度(有平行運算) | 相等 | 相等(batch size 不能太大) | | 看完所有資料(1 epoch)需要的時間 | 較慢 | <font color="#f00">較快</font> | | Gradient | <font color="#f00">較雜亂</font> | 較穩定 | | Optimization | <font color="#f00">較好</font> | 較差 | | Generalization(泛化能力) | <font color="#f00">較好</font> | 較差 | 左圖為 Large batch,而右圖為 Small batch 在更新參數時的效果展示,可以再比對上面表格的比較,會更加清晰。 ![image](https://hackmd.io/_uploads/HJe6doxVR.png) 很顯然地,Small batch 在訓練資料上會有較好的表現,較能夠讓參數繼續往 loss 的最小值靠近,這也說明每次更新雖然較雜亂卻在最佳化上是有幫助的,你還記得嗎?更新參數時最害怕遇到的是,偏微分為零(也就是斜率為零的地方),但是 loss 的數值卻不是全域最小值(global minima),而是卡在一個較高的數值,這樣訓練出來的模型,產生的誤差就會非常地大。 ![image](https://hackmd.io/_uploads/S1eQZY-4A.png) ## 總結,訓練好模型的三步驟 機器如何在訓練過程中學習呢?就是自行找到一個理想的函式,讓我們可以將手上的資料輸入至模型中,得到一個還不錯的預測結果!在訓練模型的過程中,需要思考模型(model)的彈性度,如果彈性度不佳,沒有辦法充分地描述資料間的分布關係,這就是「modle bias」,接下來根據輸入與輸出的資料結構、特性,挑選適合的誤差函數(loss function),最後透過最佳化處理(optimization)不斷地更新參數,最終獲得一組最佳參數使得 loss 達到理想上的最小值! --- :::info 以上就是這篇文章「如何訓練出好模型呢?」的所有內容,第一次看的人會花比較多時間消化吸收,這是很正常的事情,若有任何問題,歡迎在下方與我聯繫、討論,接下來也會繼續分享相關文章,敬請期待。

    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

    By clicking below, you agree to our terms of service.

    Sign in via Facebook Sign in via Twitter Sign in via GitHub Sign in via Dropbox Sign in with Wallet
    Wallet ( )
    Connect another wallet

    New to HackMD? Sign up

    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