aquachieh
    • 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
    • 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 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
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    # [2025李宏毅ML] 第4講:Transformer 的時代要結束了嗎?介紹 Transformer 的競爭者們 :::success :+1: 筆記全集Book 請至: https://hackmd.io/@4j/r1U_UJ_pye/ ::: :::info 2025生成式AI時代下的機器學習_李宏毅 課程網站 https://speech.ee.ntu.edu.tw/~hylee/ml/2025-spring.php video: https://www.youtube.com/watch?v=gjsdVi90yQo [[ppt]](https://docs.google.com/presentation/d/1Xw59SuuOBwF9ea71hJ_8sdoWW2Jl9Dk6/edit#slide=id.p1) [[pdf]](https://speech.ee.ntu.edu.tw/~hylee/ml/ml2025-course-data/mamba.pdf) (20250323) ::: :::spoiler 目錄 [TOC] ::: **每一種架構的存在都有一個理由!!** ![image](https://hackmd.io/_uploads/BykXnSThkl.png =500x) 競爭者 mamba 跟 transformer 其實是相像的 ![image](https://hackmd.io/_uploads/rJUHnSp21e.png =500x) ## 1. CNN 存在的理由是什麼? ![image](https://hackmd.io/_uploads/BkBvGvla1l.png =500x) -fully connected layer --> receptive field: 拿掉一些不需要的 weight -->> parameter sharing: 使一些 weight 的參數相同 **==> 專門為影像設計, 減少不必要參數, 避免overfitting** 複習 CNN [[ref]](https://youtu.be/OP5HcXJg2Aw?si) ## 2. Residual Connection 存在的理由是什麼? ![image](https://hackmd.io/_uploads/B16n3S631g.png =300x) ![image](https://hackmd.io/_uploads/Bkjrprp21g.png =300x) 發現類神經網路"深layer"架構 在測試跟 **"訓練"** 都表現不好 所以設計此 使 Optimization 更容易, 讓更深的網路可以訓練好 (如示意圖 error surface 較平坦, 不易卡在 local minimun) ## 3. Transformer 存在的理由是什麼? ![image](https://hackmd.io/_uploads/B1RU6r62ke.png =500x) 取代演化表: RNN(LSTM) >>> self-attention layer >>> Mamba ![image](https://hackmd.io/_uploads/HyiP6ra2kx.png =500x) ▪ **要解的問題** 輸入 vector seq, layer混合資訊, 輸出 vector seq (輸出 yi 時只能看到之前的資訊) ## 3-1. RNN-Style RNN 流派的解法 ![image](https://hackmd.io/_uploads/S1IupHThJe.png =500x) Hidden state 混合資訊 ![image](https://hackmd.io/_uploads/BJlK6BTnkg.png =500x) **- Ht**: Hidden state 存的資訊, 由前一時間點的 Hidden state 及 當下的輸入 xt 所合成 (可以是向量或大矩陣) Ht 再通過 fC 得到 yt **- fA, fB, fC**: 訓練出的函數 ![image](https://hackmd.io/_uploads/r1KY6ra3Jx.png =500x) **讓 fA, fB, fC 與時間t有關** : 可隨時間變化, 依輸入x變化 ex: 設計成 遺忘\清除資訊 等多元操作 ![image](https://hackmd.io/_uploads/Sk136Bp3kx.png =500x) **LSTM 的功能對應:** fA,t --> forget gate fB,t --> input gate fC,t --> output gate ▪ **RNN-Style vs. AI Agent’s Memory** ![image](https://hackmd.io/_uploads/B100pSThyl.png =400x) ![image](https://hackmd.io/_uploads/S1Qim_gpyl.png =400x) **對應上一堂課的 AI Agent’s Memory 機制:** memory --> H read --> fC,t write --> fB,t reflection --> fA,t ![image](https://hackmd.io/_uploads/SkSe0SahJe.png =500x) RNN 的運行 ## 3-2. Self-Attention Style ![image](https://hackmd.io/_uploads/BJub0ST3Jl.png =500x) **Self-Attention 流程:** 算yt 輸入seq x1,...xt xi 分別乘上 3個transform 得到 vi,ki,qi 第t時間點的 qt, 去和每個位置的 ki 做內積 得到 alpha_t,i (attetion 的 weight) softmax 使 alpha 總和為零 各 alpha' 與 各 vi 相乘再相加(weighted sum) 就得到 yt ![image](https://hackmd.io/_uploads/HJOzCrThJe.png =300x) (課程上的 atteation 簡化圖) ▪ **Attention 概念很早就有了** ![image](https://hackmd.io/_uploads/H1DmCHpn1e.png =500x) Neural Turing Machine https://arxiv.org/abs/1410.5401 Memory Networks https://arxiv.org/pdf/1410.3916 ![image](https://hackmd.io/_uploads/rJs4Cr6hke.png =500x) Attention-based Memory Selection Recurrent Network for Language Modeling https://arxiv.org/abs/1611.08656 ▪ **Attention 的 inference** ![image](https://hackmd.io/_uploads/r1a80Hp3yg.png =500x) 每次都要跟前面的位置做att ![image](https://hackmd.io/_uploads/rJyMoirTkg.png =500x) **- RNN(上)** 每一步的運算量固定 memory小 只需記前一個H **- attention(下)** 耗memory 越往後 運算量會越大(要考慮前面的步) Q:RNN無法記得大量資訊 att可(?) ⇒ ==錯錯錯 誤解!== ![image](https://hackmd.io/_uploads/HJ3hRjSp1e.png =500x) "attention is all you need" 非發明att,是拿掉att以外的東西 發現還是可以運作很好 使訓練可以更佳平行化! ### 語言模型的訓練 (找出參數) ![image](https://hackmd.io/_uploads/Syz9AHp2kx.png =500x) 複習訓練的原理 [Backpropagation](https://youtu.be/ibJpTrp5mcE) [Computational Graph](https://youtu.be/-yhm3WdGFok?si=2cZOANbtm0Mjd9lT) transformer 設計是為了讓**訓練可以平行化** ![image](https://hackmd.io/_uploads/rJThCST3kg.png =500x) ▪ 訓練的步驟 算出目前的答案,與正確答案計算差異,更新參數 transformer 可以快速的算出現有的答案 ![image](https://hackmd.io/_uploads/Bk8pRHT21e.png =300x) ![image](https://hackmd.io/_uploads/S1eRRH62yg.png =300x) 以前的模型要一個一個 token 的吐出結果 transformer的好處: 可以一次輸入完整的seq, 平行算出每個時間的 token 結果 ![image](https://hackmd.io/_uploads/SyZ1JLa3Je.png =500x) **"給定完整輸入"** sequence token 轉成 向量 x1,...,x6 做 self-attention 平行算出 y1,...,y6 (他們之間無關連) **"平行輸出"** 每一時間點的 token ![image](https://hackmd.io/_uploads/H1tkyIahJl.png =500x) **▪ GPU friendly 的設計 (矩陣運算)** x 乘上 transformation 得到 qkv (左) kq相乘 得到每一時間點兩兩間的 att matrix (下) att matrix 做 softmax, 再跟 value v 相乘, 得到 y 整個過程都是**矩陣運算** --> GPU 最擅長做的了:D ![image](https://hackmd.io/_uploads/HkLHP3B6kg.png =500x) 反之 RNN 是無法平行運算的,H 6需等H1~H5算出來才能計算, GPU討厭等待! **▪ Self-attention vs. RNN-style** ![image](https://hackmd.io/_uploads/BJsjJhHpJe.png =500x) (其實RNN是可以平行化的 請繼續看下去) ![image](https://hackmd.io/_uploads/r1YdRHT3yl.png =500x) 人類需要越來越長的序列 故開始想念RNN的好 ### RNN 有沒有訓練時平行的可能性 ![image](https://hackmd.io/_uploads/r1vGQar61x.png =500x) 這樣展開還是要連續算 難以平行 但發現這塊都是fA ![image](https://hackmd.io/_uploads/rkgMETrayl.png =500x) --> **拿掉fA** Ht 為 X1,...Xt 分別做 fB 的相加 --> Ht 是個 dxd 矩陣, fBT 用 Dt 當代號 ![image](https://hackmd.io/_uploads/SJkrKarp1l.png =500x) --> D=vk ![image](https://hackmd.io/_uploads/By5nFpHT1g.png =500x) --> kq = scalar alpha scalar前移 即為對 向量v做 weighted sum ![image](https://hackmd.io/_uploads/ryE7yIa31x.png =500x) !!!變成attention了!!! 少了 softmax, 稱為 **linear attention** ### linear attention ![image](https://hackmd.io/_uploads/S16X1Iphkg.png =500x) **linear attention**: 沒有做 "Reflection"(fA,t) 的 RNN, 像廣義的 RNN **RNN**: 就是 linear attention 加上 "Reflection"(fA,t) ![image](https://hackmd.io/_uploads/HkUJ398aye.png =500x) **▪ Linear Attention** Training 的時候像 Self-attention, 就也可平行化**加速**訊練 Inference 的時候像 RNN ![image](https://hackmd.io/_uploads/SyvtZ8pnkx.png =500x) dxd 矩陣 **"vk"** 的直觀含義 v:要寫入記憶(Hidden state)的資訊 k:scalar 要寫到哪裡 (ex:第幾個column) ![image](https://hackmd.io/_uploads/Bk85bLa2Jg.png =500x) H:各colume存各個資訊 q:決定要從哪個colume取出多少資訊 ![image](https://hackmd.io/_uploads/ryGi-L62Je.png =500x) Linear Attention 的變形可以近似 Softmax [[yt]](https://youtu.be/yHoAq1IT_og?si=pS) Linear Attention 還是無法贏 Self-attention ## RNN (Linear Attention) 贏不過 Transformer (Self-attention with Softmax)? ![image](https://hackmd.io/_uploads/r1opZ8pnye.png =500x) Q: 是因為 RNN 記憶有限嗎? A: 不, 兩個都有限 ![image](https://hackmd.io/_uploads/HJ24Q8Thkx.png =500x) **▪RNN** 儲存的記憶有限, 最多存d個時間點的v, 超過就會重疊使用區間 互相干擾 ![image](https://hackmd.io/_uploads/H1NSQLahkx.png =350x) ![image](https://hackmd.io/_uploads/SyhDmI62ye.png =350x) **▪Transformer** (Self-attention with softmax) 儲存的記憶 也是有限的! 當時間t>維度d時 就無法找到一個key把單純的v取出 模型記憶會開始錯亂 --> 所以比較弱應該是差在softmax的機制 ![image](https://hackmd.io/_uploads/SktdmU63Je.png =500x) Linear Attention 最大的問題: **記憶永不改變** 而 softmax 可做到記憶的改變(如圖), 只要後面有出現更重要的事, 前面的記憶就變得沒那麼重要了(值會變小) ![image](https://hackmd.io/_uploads/Bk2cQU6n1x.png =500x) 試著讓他可以改變呢? **▪ 加上 Reflection: 逐漸遺忘** Retention Network(RetNet), 即加上常數項(0-1) Gamma r 來讓記憶逐漸淡忘 ![image](https://hackmd.io/_uploads/H18jmUp3yg.png =500x) 訓練時 alpha 多乘以 r_t-1 推論時 H_t-1 多乘以 r ![image](https://hackmd.io/_uploads/Syf3QUp3Jl.png =500x) Gated Retention: r 改成 r_t, 使記憶淡忘非定值,可以隨時間改變 r_t 是模型學出來的, 哪些事要記得 哪些要遺忘 ![image](https://hackmd.io/_uploads/B1s2XITn1x.png =500x) 訓練時 要多計算各個r **▪ 對 Reflection 做一點限制** ![image](https://hackmd.io/_uploads/rkKamLTnJe.png =500x) ☉:elementwise的相乘 Gt:決定Ht中 每個col的記憶 要做什麼行動(抹去/保留/減弱) ![image](https://hackmd.io/_uploads/Sk7AQL6nJg.png =300x) ![image](https://hackmd.io/_uploads/HyyyEI6h1x.png =300x) Mamba ![image](https://hackmd.io/_uploads/ByF1VUahke.png =300x) ![image](https://hackmd.io/_uploads/rk4xV8phJe.png =300x) =左圖= Mamba(linear att的架構) 第一次贏過 transformer 橫軸FLOPs 不同大小的模型 縱軸perplecity 越小模型越好 =右圖= 縱軸 每秒可以處理多少tokens Mamba 在推論時也可以比 transformer 有更好的加速 ![image](https://hackmd.io/_uploads/B1KmlnUpkx.png =500x) DeltaNet 第二行:把 memory 清空(減去原先想放的資訊v_t,old) 再放入新的資訊v_t 推推推... 變成 Gradient Descent了!!! **▪ 其他有用到 linear attention的系列模型:** ![image](https://hackmd.io/_uploads/r1CMULTnJg.png =500x) 大模型 Jamba, Minimax-01 ![image](https://hackmd.io/_uploads/ByuQIU6hkx.png =500x) sana 影像 ![image](https://hackmd.io/_uploads/HJzEUI6hyx.png =500x) MambaOut: Do We Really Need Mamba for Vision? https://arxiv.org/abs/2405.07992 Mamba不一定要用在影像上 像在分類任務上 拔掉比較好 ![image](https://hackmd.io/_uploads/S1rSUUp2yl.png =500x) Do not train from scratch ex: finetune -- END --

    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