Rachel P.Y. Liao
    • 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
    --- tags: Knock Knock! Deep Learning --- Day 2 / Deep Learning 簡介 / 細解 Deep Learning(一) —— Prediction === 前篇很精簡的帶過了什麼是 Neural Network,他的前身、大致的架構、及不同的學習方法。有了粗略的概念之後,接下來要來介紹 Deep Learning 在做預測及訓練時的細節。 ## Neural Network 訓練步驟 Neural Network 說到底就是在做函數擬合,試圖找出當前問題的 $f$ 使得 $y = f(x)$。拿預測男女來說,$y$ 是男/女二元結果,$x$ 是對於預測有利的特徵輸入,$x_1 =$ 五官輪廓, $x_2 =$ 聲音高低,等等。 ![neural network blackbox](https://i.imgur.com/Qh8zwJn.png) *—— Neural Network 為函數擬合。* 一個 Neural Network 學習擬合 $f$ 的大致步驟如下: 1. 備好 dataset,也就是好幾筆 **$(x, y) =$(feature, label)** 的 data 2. 對每一筆輸入 feature 進行**prediction(預測)**,得到輸出 $\hat{y}$ 3. 計算預測結果 $\hat{y}$ 與正確答案 $y$ 的 **error(誤差)**,或稱 **loss** 4. 將 loss **backpropogate(反向回饋)** 給 network 裡的 parameters以進行修正,目標是減少下次預測的 loss 所以 Deep Learning 學習的成果,就是能準確預測結果的 model parameters。 我們將關鍵步驟分為兩大步:prediction (2.) 和 training (3. & 4.)。本篇的重點會擺在 prediction 這一步。下面會多一些數學,希望不會太難,也為深入理解 Deep Learning 內含的數學做準備。 ## Prediction 前篇有介紹過 Perceptron 怎麼被訓練的。由於 Neural Network 是由很多類似 Perceptron 的 Neuron 層層連結而成,大致上的步驟也與之相似。 Prediction 這步基本上就是把資訊往 network 裡送,傳到最後一層取得 output,這動作稱為 **feed forward** 或 **forward propagation**,步驟大致如下: 1. 從上層接收 input $x$ 2. 與 weights $w$ 透過 summation and bias 進行加權整合 3. Activation 決定該 neuron 是否被激發 4. 產出 prediction output ### Input & Weights 第一層的輸入訊息是來自大環境的,例如圖片、文字、聲音等各種 feature,而接下來 hidden layer 則是接收前面幾層傳來的分析處理過的訊息來進行整合。 > 你會問,這些層跟層之間的訊息有什麼含意嗎? > > 肯定有,但只有 network 自己懂。人類想理解中途的訊息很困難,因為他是很高程度的 abstraction(抽象概念),而學者們也致力於讓這些訊息變得可理解。這也是為什麼 Neural Network 被稱為**黑盒子**,因為沒人懂他怎麼塞進輸入後就能產生準確預測,也不懂訓練不起來的時候原因出在哪。 讓我們往 engineering 的角度靠近一步。如果圖片、文字、聲音都能當輸入,那我們建立好的 network 要怎麼同時照顧好這麼多不同資料的形式?因此,這些輸入都要先轉化成較為統一的模樣。 #### Vector & Matrix 資料形式 讓我們回顧一下 summation and bias 在做什麼: $$ \sum^{m-1}_{i=0} w_i \cdot x_i + b $$ > 後面的 $b$ 是 bias,前篇有提過是讓 activation 的門檻標準變得可訓練。但他在式子裡好像有點礙眼,因為跟前面 $m$ 個 $w$ 走在不同路上! > > 這時候有個小技巧: > > $$ > \sum^{m-1}_{i=0} w_i \cdot x_i + b = \sum^{m-1}_{i=0} w_i \cdot x_i + b \cdot 1 = \sum^m_{i=0} w_i \cdot x_i > $$ > > 這邊 $w_m=b$,$x_m=1$。 > > 也就是我們把 $b$ 當成 weights 的一份子(因為 weights 和 bias 同為可訓練的 parameters),並將他對應的 $x$ 設為 $1$,即可合併進入 summation。 > > 這個技巧能讓 code 更簡單! 前面的 summation $\sum^{m-1}_{i=0} w_i$,無論是數學上看起來或是 code 寫起來都略顯麻煩。這時候我們可以把他們轉化為 **vector(向量)** 來進行計算: $$ \sum^{m-1}_{i=0} w_i \cdot x_i = \begin{bmatrix} w_0 & w_1 & \dots & w_{m-1} \end{bmatrix}\begin{bmatrix} x_0 \\ x_1 \\ \vdots \\ x_{m-1} \end{bmatrix} = \mathbf{w} \cdot \mathbf{x} $$ 是不是簡單許多? 這是單個 neuron 的輸出,我們甚至可以用 **matrix(矩陣)** 來一次打包一層的 neurons。請參考下面的例子: ![matrix summation](https://i.imgur.com/XkrAdEL.png) *—— Matrix 來進行 summation。* $a$ 是 activation 之後的結果,我們先看 summation 之後的輸出 $z$ 可以怎麼表示: $$ \mathbf{z} = \begin{bmatrix} z_0 \\ z_1 \\ z_2 \end{bmatrix} = \begin{bmatrix} W_{00} & W_{01} & W_{02} \\ W_{10} & W_{11} & W_{12} \\ W_{20} & W_{21} & W_{22} \\ \end{bmatrix}\begin{bmatrix} x_0 \\ x_1 \\ x_2 \end{bmatrix} + \begin{bmatrix} b_0 \\ b_1 \\ b_2 \end{bmatrix} = \mathbf{W}\mathbf{x} + \mathbf{b} $$ 一個簡單的式子就能代表一整層的 feed forward!而利用 matrix 來運算,除了讓數學簡單一點(特別是後面會提到的 gradient descent),也能讓電腦硬體優化運算,訓練更快速。 > 雖然睡前可能不會想做,但有障礙的朋友記得小小複習一下 linear algebra(線性代數),對後面的數學會很有幫助的。 #### Input Pre-processing 回到統一輸入形式這邊。理解 summation and bias 這步基本等於 $\mathbf{W}\mathbf{x} + \mathbf{b}$ 之後,應該就不難理解輸入形式應該統一為 vector $\mathbf{x}$,以利電腦的運算。 也就是在整個 network 開始學習前會進行 pre-processing(前置處理),將圖片轉成 vectors of pixels,文字轉成 word vectors(這個之後會介紹到,將字詞轉化為 vector 形式),聲音轉化成 vectors of frequency 等等,才能進入 network 進行訓練。而怎麼讓轉化為 vectors 的 input 最大限度的保留原本的資訊,也是一件值得深入的話題! Input 和 weights 整合完後,我們代稱結果為 $z$,i.e. $z = wx + b$。 ### Activation Activation 是根據前面處理好的訊息 $z$,決定激發程度。前篇 Perceptron 提到在 activation 時用 step function 進行激發與否的二元標準判斷。而在 Neural Network 裡,每個 neuron 輸出的是激發程度,也就是不只是 0 或 1,而是更細微的訊息。 在這個步驟我們計算的是上面途中的 $a = g(z)$,$g$ 為 activation function。為了學習 non-linear approximation,activation function 通常是 non-linear function(非線性函數)。 除了是 non-linear function,還有些必備條件:**derivative(導數)容易計算**,且 **derivative 適用於訓練**。例如 step function 雖然是 non-linear function,但因為他的導數不適合用來訓練(兩邊導數都是 0,能提供的回饋也是 0 啊⋯⋯),並不適合被用在 hidden layer 的 activation。 > 微積分從腦中消逝的也麻煩找尋一下記憶。 這些條件跟之後 back propagation 需要用到的 gradient descent 有關,之後會詳細解說。 #### 常見 Activation Functions 常用的 Activation function 很多,簡單示範幾個: | Sigmoid | Tanh | ReLU | | -------- | -------- | -------- | | ![](https://i.imgur.com/VwmAqtK.png) | ![](https://i.imgur.com/DVFEw2I.png) |![](https://i.imgur.com/xiNql0m.png) | |$\sigma(z) = \frac{1}{1 + e^{-z}}$|$\tanh(z) = \frac{e^z - e^{-z}}{e^z + e^{-z}}$|$\text{rect}(z) = \max(z, 0)$| $e$ 沒有很可怕,$e$ 只是個常數,$e^x$ 的導數為 $e^x$,很美。 值得注意的是 sigmoid 的輸出範圍介在 0 和 1 之間,呈遞增,且中間為 0.5,很適合把分數化為機率,除了用在 hidden layer 的 activation,也常用在 binary classification 的 output layer 當作預測種類的機率。 而 ReLU 看似簡單,其實經過一些實驗發現效果非常好,而且 derivative 也比 sigmoid 簡單許多,與他的變形們是近幾年的主流選擇。 這些 function 都符合的上述的條件:導數都很優雅(之後會看到他們的 derivative),且適合訓練(例如 ReLU,分數越高激發程度越高,導數能提供的訊息也很清楚)。 #### Softmax Function 最後再介紹一個在 multiclass classification 時,很常用在最後一層的 Softmax function。他的概念是 **normalize(標準化)最後一層得到 $z$ 的分數**,使得可正可負且沒有絕對意義的分數值,變成 probability(機率) 的意義。 例如你在做貓的種類判斷,有三個種類,那麼 Softmax 出來的結果並不是 100% 波斯貓,而是 90% 機率波斯貓、5% 機率英國短毛貓、5% 機率蘇格蘭摺耳貓等等。 Softmax function 表示如下($C$ 為 number of classes(類別數),$z_i$ 為第 $i$ 種類別的分數): $$ \text{softmax}(z_i) = \frac{e^{z_i}}{\sum^C_{c} e^{z_c}} $$ 可以看到分母的 summation 就是在進行 normalization,讓每個類別的分數介於 0,1 之間,且合為 1。而這個分數就代表預測為該類別的機率。所以最後一層要有幾個 neuron 會等於要做判斷的種類數 $C$ 喔! ### Prediction 經由 pre-processing 把 network 的 input 轉化為 vector 形式後,經過一層層的處理,每層統整激發: $\mathbf{a} = f(\mathbf{z}) = f(\mathbf{W}\mathbf{x} + \mathbf{b})$ 並把輸出傳遞下去,直到最後一層激發完後,獲得 prediction $\hat{y}$。Prediction 這步就完成了。 而 predict 完之後,接著把 error 回饋給 network 的參數,便是下一篇的主題。 ## Checkpoint - Neural Network 的終極目標為何? - 訓練 Neural Network 的大致流程為何? - 用 vector 和 matrix 來表示 data 有哪些好處? - 試著從上面 matrix summation 的範例圖,標示每條線對應到 $\mathbf{W}$ 裡的值。 - Activation function 有哪些條件? - Softmax function 常被應用在哪種任務? ## 參考資料 1. [Wikipedia - Activation Function](https://en.wikipedia.org/wiki/Activation_function) 2. [CS224n Lecture Notes Part III - Neural Networks, Backpropagation](http://web.stanford.edu/class/cs224n/readings/cs224n-2019-notes03-neuralnets.pdf) 3. [CS224n Lecture 3 Slides - Word Window Classification, Neural Networks, and PyTorch](http://web.stanford.edu/class/cs224n/slides/cs224n-2020-lecture03-neuralnets.pdf) 4. [👍 CS229 Lecture Notes - Deep Learning](http://cs229.stanford.edu/summer2020/cs229-notes-deep_learning.pdf) ## 延伸閱讀 1. [CS229 Lecture Notes - Linear Algebra Review and Reference](http://cs229.stanford.edu/summer2020/cs229-linalg.pdf)

    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