咖哩麵包人
  • 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    ###### tags: `ML數學分部` {%hackmd @kk6333/theme-sty1 %} # Backpropagation 反向傳播算法 ## 簡介 反向傳播算法 (BPP) 是拿來計算梯度、更新神經網路參數的方法 在之前我們知道該怎麼更新 linear regression 的參數 也就是計算 loss 的梯度並更新權重 $$w^1 \leftarrow w^0 - \eta * \frac{\partial{L}}{\partial{w}}$$ 那神經網路我們可以看成是由多個 linear regression 所組成 ( 加上 Activation funciton ) ( 詳情可見:[從 Linear Regression 到神經網路](https://hackmd.io/0nN91045TBCTiV1SluiHgQ?view) ) 也就是有很多的權重 w 要計算, 而 Backpropagation 就是一種有效率來計算每個權重梯度的方法 <br> ## 目標 我們最終的目標是算出 Update 參數時的梯度 $\bigtriangledown L(\theta)$ 這邊將 $L(\theta)$ (loss) 定義為 $$L(\theta)=\sum_{n=1}^N{l^n(\theta)}$$ ( 定義 $l$ 的意思是個別資料的 $loss$ ) 那梯度可以寫成下列式 $$\bigtriangledown L(\theta) = \frac{\partial{L}}{\partial{w}} = \sum_{n=1}^N{\frac{\partial{l^n}}{\partial{w}}} $$ 於是我們的目標就是要計算出 $$目標 = \frac{\partial{l}}{\partial{w}}$$ 並將它加總 <br> ## Forward Pass & Backward Pass 這邊使用老師舉的範例來說明 ( 會用到微積分的 Chain Rule ) ![](https://i.imgur.com/LlymUxo.png =500x270) 如果我們神經網路長上述這樣 ( 先不考慮 Activation function ) 那要我的計算目標 $\frac{\partial{l}}{\partial{w}}$ 利用 Chain Rule 可以化簡為以下式子 $$\frac{\partial{l}}{\partial{w}}=\frac{\partial{z}}{\partial{w}}\frac{\partial{l}}{\partial{z}}$$ 在這個階段我們會將任務變為兩個 也就是 BPP 的兩步驟 1. Forward Pass : 使用神經網路進行預測,同時也會計算出 $\frac{\partial{z}}{\partial{w}}$ 2. Backward Pass : 向後傳播計算 $\frac{\partial{l}}{\partial{z}}$ 都算出後就可得到目標值 <br> ## Step1. Forward Pass 向前傳播其實就是讓神經網路取預測 y 那 $\frac{\partial{z}}{\partial{w}}$ 呢 ? :::info 我們將 z 攤開 可以驚訝發現因為 $z=x_1w_1 + x_2w_2+b$ 所以 $\frac{\partial{z}}{\partial{w_1}}=x_1$ 代表 $\frac{\partial{z}}{\partial{w}}$ 的解會是 input ::: 所以 forward 就結束啦 ~ --- <br> ## Step2. Backward Pass 因為 $\frac{\partial{l}}{\partial{z}}$ 無法直接計算,所以要將它攤開 這時可以考慮 Activation function ,神經網路如下 ![](https://i.imgur.com/CimTrDR.png) :::info 將 $\frac{\partial{l}}{\partial{z}}$ 做分解 $$\frac{\partial{l}}{\partial{z}} = \frac{\partial{a}}{\partial{z}}\frac{\partial{l}}{\partial{a}}$$ :::warning 在這邊 $\frac{\partial{a}}{\partial{z}}=\sigma^{'}(z)$ 因為 z 在 forward 就算出來了(不然你 y 怎麼來 ^^ ) 所以 $\sigma^{'}(z)$ 為一常數 (scalar) ::: <br> 我們再將 Activation Func 後面加一層 Layer ![](https://i.imgur.com/NsT3yFx.png) :::info 繼續分解 $\frac{\partial{l}}{\partial{a}}$ $$\frac{\partial{l}}{\partial{a}}=\frac{\partial{z^{'}}}{\partial{a}}\frac{\partial{l}}{\partial{z^{'}}}+\frac{\partial{z^{''}}}{\partial{a}}\frac{\partial{l}}{\partial{z^{''}}}$$ 我們細看 $\frac{\partial{z^{'}}}{\partial{a}}$ 帶入 z 公式且設 x=a 時,對 a 微分後會剩下$w_3$ ($z=aw_3+a_jw_j...$) --- 帶回 $\frac{\partial{l}}{\partial{z}}$ $$\frac{\partial{l}}{\partial{z}}=\sigma^{'}(z)[ w_3\frac{\partial{l}}{\partial{z^{'}}} + w_4\frac{\partial{l}}{\partial{z^{''}}}]$$ 當此 layer 是輸出層時,便可計算 $\frac{\partial{l}}{\partial{z^{'}}}$ 了 $$\frac{\partial{l}}{\partial{z^{'}}}=\frac{\partial{y_1}}{\partial{z^{'}}}\frac{\partial{l}}{\partial{y_1}}$$ <br> ![](https://i.imgur.com/ecQYdeq.png) 最後再將上述結果帶回 $$\frac{\partial{l}}{\partial{z}}=\sigma^{'}(z)[ w_3\frac{\partial{l}}{\partial{z^{'}}} + w_4\frac{\partial{l}}{\partial{z^{''}}}]$$ 即求出 $\frac{\partial{l}}{\partial{z}}$ ::: <br> 有了 $\frac{\partial{l}}{\partial{z}}$ 和 $\frac{\partial{z}}{\partial{w}}$ 我們就算出了梯度 $$\frac{\partial{l}}{\partial{w}}=\frac{\partial{z}}{\partial{w}}\frac{\partial{l}}{\partial{z}} $$ --- :::warning 為啥叫 Backward Pass 呢 ? 將公式列出 (這邊舉例計算 $z=z_1$ 公式 ) $$\frac{\partial{l}}{\partial{z_1}}=\sigma^{'}(z_1)[ w_3\frac{\partial{l}}{\partial{z_{3}}} + w_4\frac{\partial{l}}{\partial{z_{4}}}]$$ $\frac{\partial{l}}{\partial{z_{3}}}$ 又可展開 $$\frac{\partial{l}}{\partial{z_3}}=\sigma^{'}(z_3)[ w_5\frac{\partial{l}}{\partial{z_{5}}} + w_6\frac{\partial{l}}{\partial{z_{6}}}]$$ 同理 $\frac{\partial{l}}{\partial{z_{4}}}$ 並繪製成圖 ![](https://i.imgur.com/3OCt89E.png) 會發現我們是從靠近 y 的參數慢慢計算回來 因為前面參數的偏微分都會需要後面的參數 所以這樣是比較有效率的方法 ( 不然你每次都要重算 TT ) 也稱為 Backward Pass ~ :::

    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