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
    • 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 Versions and GitHub Sync Note Insights 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
    10
    Subscribed
    • Any changes
      Be notified of any changes
    • Mention me
      Be notified of mention me
    • Unsubscribe
    Subscribe
    # [pb note] 2024生成式AI導論_李宏毅 https://speech.ee.ntu.edu.tw/~hylee/genai/2024-spring.php :::spoiler [TOC] ::: # 第1講:生成式AI是什麼? https://www.youtube.com/watch?v=JGtqpQXfJis **生成式人工智慧(Generative AI)**:機器產生==複雜==有結構的物件 複雜 --> 盡乎無法窮舉 ex:分類:從有限的選項中做選擇(不是生成式人工智慧) 機器需要能夠產生在訓練時從來沒有看過的東西 也就是需要有「創造力」 ![image](https://hackmd.io/_uploads/BJ4VVgbpT.png =500x) ![image](https://hackmd.io/_uploads/BJgBEeZpa.png =500x) 將困難目標轉為有限範圍的 就可以解了 ![image](https://hackmd.io/_uploads/HydYDeZ6p.png =500x) 生成有各種策略 ![image](https://hackmd.io/_uploads/ByW8LgZpa.png =500x) **Autoregressive Generation**: 把複雜的物件 拆解成較小的單位 再按照某種固定的順序依序生成 chat gpt也是採用此策略 # 第2講:今日的生成式人工智慧厲害在哪裡?從「工具」變為「工具人」 https://youtu.be/glBhOQ1_RkE?si=Ljr8WhYzGQu9PzC0 ![image](https://hackmd.io/_uploads/H10b9n-pT.png =500x) # 第3~5講:訓練不了人工智慧?你可以訓練你自己 第3講:訓練不了人工智慧?你可以訓練你自己 (上) — 神奇咒語與提供更多資訊 https://www.youtube.com/watch?v=A3Yx35KrSN0 第4講:訓練不了人工智慧?你可以訓練你自己 (中) — 拆解問題與使用工具 https://www.youtube.com/watch?v=lwe3_x50_uw 第5講:訓練不了人工智慧?你可以訓練你自己 (下) — 讓語言彼此合作,把一個人活成一個團隊 https://www.youtube.com/watch?v=inebiWdQW-4 有那些在不訓練模型的情況下強化語言模型的方法 ![image](https://hackmd.io/_uploads/BkxqvtFsT6.png =500x) ## 1. Chain of Thought (CoT) 叫模型思考 (神奇咒語法) ![image](https://hackmd.io/_uploads/rycxxTbTT.png =500x) 請模型解釋一下自己的答案 對模型情緒勒索 ▪ 更多相關資訊: 驗證神奇咒語的都市傳說 Principled Instructions Are All You Need for Questioning LLaMA-1/2, GPT-3.5/4 https://arxiv.org/abs/2312.16171 ![image](https://hackmd.io/_uploads/ry21Wp-pp.png =500x) >ex:有禮貌是沒有用的 請明確指令 要做什麼 不要說不要做什麼 如果做得好就給你小費 ▪ 用AI來找神奇咒語 增強式學習 (Reinforcement Learning, RL) ![image](https://hackmd.io/_uploads/BkrD4T-6p.png =500x) 直接用語言模型 ![image](https://hackmd.io/_uploads/ByyKNp-aa.png =500x) ![image](https://hackmd.io/_uploads/ryjonp-pT.png =500x) ## 2. 提供更多的資訊 (額外知識法) ▪ 把前提講清楚 模型可給出更精確的答案 ![image](https://hackmd.io/_uploads/HknlTp-66.png =500x) ▪ 提供生成式AI原本不清楚的資訊 甚至可以更懶 直接餵pdf讓他自己讀跟畫出 ![image](https://hackmd.io/_uploads/Syg5p6bp6.png =500x) ▪ 提供範例 In-context learning:讓模型根據範例得到更準確的答案 ![image](https://hackmd.io/_uploads/S1OkAlf66.png =500x) ## 3. 拆解問題 ▪ 拆解任務 ![image](https://hackmd.io/_uploads/HJ1sydsTT.png =500x) 語言模型檢查自己的錯誤 ![image](https://hackmd.io/_uploads/ryX01djTp.png =500x) Constitutional AI: Harmlessness from AI Feedback 憲法AI 語言模型的自我批判 ![image](https://hackmd.io/_uploads/HykWlOjT6.png =500x) 注意:反省不會影響原來的語言模型喔! ![image](https://hackmd.io/_uploads/BkVmgOs66.png =500x) Self-Consistency 先採樣多個不同的推理路徑(重複請求多次),然後選擇最一致的答案 ![image](https://hackmd.io/_uploads/H1AKP_jp6.png =500x) ![image](https://hackmd.io/_uploads/Sy7L3_ip6.png =500x) ▪ 打一套組合拳 Tree of Thoughts (ToT) ex: 拆解三步驟,每步產生多組答案並檢查 ![image](https://hackmd.io/_uploads/H1o-TOopa.png =500x) [Algorithm of Thoughts](https://arxiv.org/abs/2308.10379) [Graph of Thoughts](https://arxiv.org/abs/2308.09687) ## 4. 提供工具 ▪ 使用工具 – 搜尋引擎 RAG (Retrieval Augmented Generation) 不訓練下的特製化模型 if 你的資料庫是有特殊權限的 ▪ 使用工具 – 寫程式 ![image](https://hackmd.io/_uploads/SyzJQKj66.png =500x) chat gpt 顯示 "Finishing analyzing", 可點開看程式碼 ▪ 使用工具 – 文字生圖 AI (DALL-E) ![image](https://hackmd.io/_uploads/S1Z4_toTp.png =500x) [[延伸閱讀] Midjourney 文字冒險遊戲](https://youtu.be/A-6c584jxX8?si=od_UF9cdBsChyQJU) ![image](https://hackmd.io/_uploads/Hy80dtoap.png =500x) ▪ GPT Plug-in ![image](https://hackmd.io/_uploads/rJulYtsap.png =500x) ![image](https://hackmd.io/_uploads/HyE-KYjaa.png =500x) ![image](https://hackmd.io/_uploads/SyMzYtjpa.png =500x) [[延伸閱讀] 能夠使用工具的AI](https://youtu.be/ZID220t_MpI?feature=shared) ![image](https://hackmd.io/_uploads/SJJIKti6T.png =500x) https://arxiv.org/abs/2402.04253 ## 5. 讓語言彼此合作,把一個人活成一個團隊 ### ▪ 模型合作:讓合適的模型做合適的事情 ![image](https://hackmd.io/_uploads/ryevQtlkC.png =500x) 1+1>2, 各個模型有 不同的能力,使用起來有不同的成本 ex: 再訓練一個模型來分配任務 殺雞焉用牛刀,簡單的任務可以交給便宜模型處理就好,合作可以省成本 FrugalGPT: https://arxiv.org/abs/2305.05176 video: [v1](https://youtu.be/vxxPtDCb9Go?si=WO4eqPTHDyS1pal3), [v2](https://youtu.be/VpKN3KvSK6c?si=lyqO8QSHBno89HIG) ### ▪ 模型合作:讓模型彼此討論 讓模型"反省"(自問自答) 讓模型"討論"(與他人互動) ![image](https://hackmd.io/_uploads/BkaDXYlyC.png =500x) ▪ 反省 vs 討論 y: 每次推翻前次的可能性 x: 互動次數 ==> 反省推翻的可能性較低;討論較有新的刺激推翻先前答案 ![image](https://hackmd.io/_uploads/By8j7Fxk0.png =500x) ▪ 越多模型,越多回合,皆有幫助 ![image](https://hackmd.io/_uploads/Hk4JpYlyR.png =500x) ▪ 持續討論 目前不用擔心討論停不下來,反而要擔心討論不起來 ![image](https://hackmd.io/_uploads/rJQrycek0.png =500x) -參考 prompt: 另一個模型的看法當作參考就好... 不需要一定同意對方的想法 請發表自己的意見... ▪ 引入不同的腳色 組建團隊 ![image](https://hackmd.io/_uploads/Sk7xe5gkR.png =500x) https://arxiv.org/abs/2303.17760 優化團隊(打考績) ![image](https://hackmd.io/_uploads/B1eQeqgkR.png =500x) Dynamic LLM Agent Network https://arxiv.org/abs/2310.02170 ![image](https://hackmd.io/_uploads/SJ65Zce1R.png =500x) 開源參考 MetaGPT: https://arxiv.org/abs/2308.00352 ChatDev: https://github.com/OpenBMB/ChatDev 未來想像: 不需要打造全能的模型,語言模型可以專業分工,不同團隊可以專注於打造專業領域的語言模型 :) 由 AI 組成的社群 ![image](https://hackmd.io/_uploads/SyV9zcekC.png =500x) [【生成式AI】讓 AI 村民組成虛擬村莊會發生甚麼事?](https://youtu.be/G44Lkj7XDsA?si=cMbKG3tqPbIgnnBq) https://arxiv.org/abs/2304.03442 ![image](https://hackmd.io/_uploads/HkXPtKs66.png =500x) --- # 第6講:大型語言模型修練史 — 第一階段: 自我學習,累積實力 https://www.youtube.com/watch?v=cCpErV7To2o ![image](https://hackmd.io/_uploads/S1eZI9g1R.png =500x) 大語言模型 ==> "學習文字接龍" 只是訓練資料不同 ![image](https://hackmd.io/_uploads/SyI0rqe1A.png =500x) ![image](https://hackmd.io/_uploads/SkrJLqlyA.png =500x) ![image](https://hackmd.io/_uploads/S17G89gJR.png =500x) 學會文字接龍需要學習 語言知識和世界知識 拿來學文字接龍的文字資料,人工可以不介入或介入,以影響對模型的訓練結果。 (大語言模型機器學時,只管找到的參數有沒有」符合「訓練資料,不管有沒有道理) # 第7講:大型語言模型修練史 — 第二階段: 名師指點,發揮潛力 (兼談對 ChatGPT 做逆向工程與 LLaMA 時代的開始) 語言模型跟據網路資料學了很多東西,卻不知道使用方法,需要進入修煉的第二階段 --> 人類老師教導 ## Instruction Fine-tuning 透過人類老師準備的資料來學習的過程 ![image](https://hackmd.io/_uploads/H14KvNT1C.png =500x) 輸入的句子必須要標明 哪部分是user問的/ai回應的, 學起來的回答才會是正確的 ![image](https://hackmd.io/_uploads/rkgCvNp1R.png =500x) ▪ pre-train & fine-tuning 人力標註的資料 稀少又成本高,不夠用來訓練語言模型 ==> **關鍵是用第一階段的參數作為初始參數!** ![image](https://hackmd.io/_uploads/SJQsFVTyR.png =500x) ▪ Adapter(e.g. LoRA) 完全不動初始參數,在原有函數後面多加"少量的"未知數 最佳化時只更新這些新加的未知數 可確保新參數跟舊參數組合是非常類似的,也可減少運算量 ![image](https://hackmd.io/_uploads/ByS95E6JR.png =500x) 各種adapter: 插入參數 位置/數目 不同 ![image](https://hackmd.io/_uploads/Hy305V610.png =500x) ▪ 舉一反三 第一階段參數 學到的是 **複雜的規則** (不會僅憑簡單的規則做文字接龍) 在做最佳化時 因較接近接近初始參數,就較不會出現離譜的答案 還有很強的舉一反三能力 ex: 教他英文 但他自動學會中文 ![image](https://hackmd.io/_uploads/BJ4zEBpy0.png =500x) ![image](https://hackmd.io/_uploads/HyW7NBa1C.png =500x) ==> 有個好的pre-train模型,可以帶給你良好的舉一反三能力! ## Fine-tuning 的路線分成了兩條 ![image](https://hackmd.io/_uploads/rJnLVBT1C.png =500x) ▪ 路線一:打造一堆專才 每個專才做特定的任務 BERT 系列: https://youtu.be/gh0hewYkjgo ![image](https://hackmd.io/_uploads/HJQgurp1R.png =500x) ▪ 路線二:直接打造一個通才 蒐集一大堆標註資料,涵蓋各式各樣任務 ![image](https://hackmd.io/_uploads/BkbvdHaJ0.png =500x) ex: 一個接著一個任務的教他 ![image](https://hackmd.io/_uploads/B1PfYBTyA.png =500x) ex: google 的 FLAN FLAN (Finetuned Language Net) https://arxiv.org/abs/2109.01652 T0 https://arxiv.org/abs/2110.08207 ![image](https://hackmd.io/_uploads/S1NHFraJA.png =500x) ![image](https://hackmd.io/_uploads/B11wtrayA.png =500x) ex: 用1800個"任務"來訓練模型,做instraction fine-tuning Scaling Instruction-Fine-tuned Language Models https://arxiv.org/abs/2210.11416 ![image](https://hackmd.io/_uploads/SJ5x9Sp1R.png =500x) ![image](https://hackmd.io/_uploads/BJy79r6k0.png =500x) 隨著訓練過的任務越多,在陌生任務上的表現會越好 ex: Instruct GPT https://arxiv.org/abs/2203.02155 Open AI 的 GPT-3 是一個線上的服務,有真實的使用者的互動問答 故拿來訓練的效果更強於FLAN ![image](https://hackmd.io/_uploads/Bkt9jr6kR.png =500x) ![image](https://hackmd.io/_uploads/SyIk3rT1R.png =500x) ▪ Instruction Fine-tuning 是畫龍點睛 這篇只用了上萬筆資料而已 不需大量資料~ ![image](https://hackmd.io/_uploads/B1KHVFakR.png =500x) 只需少量 品質好的資料 就可打造不錯的模型 ![image](https://hackmd.io/_uploads/BytYVtaJR.png =500x) ## 自己也能做 Instruction Fine-tuning 嗎? 不行QQ 因為不知道使用者會問什麼 ==> 以 ChatGPT 為師(對 ChatGPT 做逆向工程) 創造高品質資料 ![image](https://hackmd.io/_uploads/SkXjD5akC.png =500x) ▪ pre-train 參數哪裡來? -Meta 開源了 LLaMA -Stanford Alpaca -Vicuna ![image](https://hackmd.io/_uploads/Hk1xOc61R.png =500x) ![image](https://hackmd.io/_uploads/HJ9B_5akR.png =500x) ▪ Lamma 飛入尋常百姓家 人人可以 fine-tune 大型語言模型的時代開始了 ![image](https://hackmd.io/_uploads/r1eYuqpyR.png =500x) ![image](https://hackmd.io/_uploads/Hkitu9p10.png =500x) # 第8講:大型語言模型修練史 — 第三階段: 參與實戰,打磨技巧 (Reinforcement Learning from Human Feedback, RLHF) https://www.youtube.com/watch?v=v12IKvF6Cj8 ## Reinforcement Learning from Human Feedback (RLHF) ![image](https://hackmd.io/_uploads/HJQ7dYFx0.png =500x) HF: human feedback 給模型回饋 RL: 透過回饋資訊來學習的方法 ![image](https://hackmd.io/_uploads/BJI-OFYeA.png =500x) ![image](https://hackmd.io/_uploads/B18BdttlC.png =500x) ▪ RLHF的原則是 人覺得好的答案,就提高出現機率 人覺得不好好的答案,就降低出現機率 ▪ 補充資料: [[video1] 概述增強式學習 (Reinforcement Learning, RL) (一) – 增強式學習跟機器學習一樣都是三個步驟]( https://www.youtube.com/watch?v=XWukX-ayIrs&list=PLJV_el3uVTsMhtt7_Y6sgTHGHp1Vb2P2J&index=30) [[video2]DRL Lecture 1: Policy Gradient (Review)](https://youtu.be/z95ZYgPgXOY?si=-E-1iE77qxsdNoGw) chatgpt微調參數的演算法為PPO(proximal policy optimization) ## RLHF vs Instruction Fine-tuning ▪ 從人類產生訓練資料的角度來看 ==> RLHF 比較輕鬆 ![image](https://hackmd.io/_uploads/Sk9_dKKx0.png =500x) ▪ 從模型學習的角度來看 ![image](https://hackmd.io/_uploads/HJ2GtYKlA.png =500x) ![image](https://hackmd.io/_uploads/B1lBFKYeC.png =500x) Instruction Fine-tuning: 只問過程,不問結果 RLHF:只問結果,不問過程 >師:像《天龍八部》珍瓏棋局,中間有一步要先堵死自己的棋子,最後卻會獲勝 > ## 語言模型 vs 下圍棋 ![image](https://hackmd.io/_uploads/HyxJ6FKg0.png =500x) ![image](https://hackmd.io/_uploads/HJTtTYFx0.png =500x) 如何更有效的利用人類的回饋? ## 回饋模型(Reward Model) 可能譯為「報酬模型」或「獎勵模型」 ▪ 回饋模型: 模仿人類喜好 ex:輸出三個選項給回饋模型評分,最後只給人類看分數最高的答案 ![image](https://hackmd.io/_uploads/SJ1apKYe0.png =500x) ▪ 回饋模型:向虛擬人類學習 (注意 過度向虛擬人類學習是有害的) ![image](https://hackmd.io/_uploads/HkTgRYKgR.png =500x) ▪ 其他方法:DPO, KTO ![image](https://hackmd.io/_uploads/BkN40YFe0.png =500x) ▪ RL"H"F --> RL"AI"F ![image](https://hackmd.io/_uploads/ryxU0tFlA.png =500x) 用"另個語言模型"評判語言模型 甚至可以自己評判自己 (產不出好的回答 並不代表他不知道哪個是壞的) ## 增強式學習的難題 ▪ 什麼叫做好? Helpfulness vs Safety 沒有固定標準的:以不同角度評比的答案不同 ▪ 人類自己都無法正確判斷好壞的狀況? ![image](https://hackmd.io/_uploads/Hy-Y0KYeA.png =500x) ![image](https://hackmd.io/_uploads/SJe30KYeA.png =500x) Align: 對齊人類的偏好跟需求 # 第9講:以大型語言模型打造的AI Agent https://youtu.be/bJZTJ7MjYqg?si=NzlYYwmFd8t8i7Z5 ## AI Agent AutoGPT: https://github.com/Significant-Gravitas/Auto-GPT AgentGPT: https://agentgpt.reworkd.ai/ BabyAGI: https://github.com/yoheinakajima/babyagi Godmode: https://godmode.space/?ref=futuretools.io ![image](https://hackmd.io/_uploads/rkSL_TGZC.png =500x) 狀態 --> 目標 記憶 計畫 --> 行動 ## 有記憶的 ChatGPT ▪ Open AI is working on it? https://openai.com/blog/memory-and-new-controls-for-chatgpt ▪ MemGPT https://github.com/cpacker/MemGPT https://arxiv.org/abs/2310.08560 ![image](https://hackmd.io/_uploads/Bk96dafbC.png =500x) gpt幫我們做摘要, 記憶我們正在準備期中考 對過去的記憶做RAG搜尋記憶 給予更有效個人化的回應 ## 範例 請參考老師精彩範例: [教你怎麼打造芙莉蓮一級魔法使考試中出現的泥人哥列姆](https://youtu.be/bJZTJ7MjYqg?si=fJV1w_936nwvEhJH&t=890) --- # 第10講:今日的語言模型是如何做文字接龍的 — 淺談Transformer https://www.youtube.com/watch?v=uhNsUCb2fJI ![image](https://hackmd.io/_uploads/ryCfHm94A.png =500x) 現今常用的語言模型架構為 Transformer 也是 ChatGPT 的 "T" ![image](https://hackmd.io/_uploads/H1eVrm5NR.png =500x) ▪ 語言模型演進史 詳見以前影片 此講的是簡化版本 [【機器學習2021】Transformer (上)](https://www.youtube.com/watch?v=n9TlOhRjYoc&t=0s) [【機器學習2021】Transformer (下)](https://www.youtube.com/watch?v=N6aRv06iv2g&t=0s) ## Transformer 概述 ![image](https://hackmd.io/_uploads/SkiVSX9N0.png =500x) ▪ **tokenization** 將文字轉為 token ▪ **input layer** 理解每個 token <-- token embed ▪ **attention** 考慮 token 間的關係 ▪ **feed forword** 把 att 模組的輸出進行整合再進一步思考 `attention+feed forword` = **Transformer Block** 一個 Transformer 裡有 N 個 Block(自定), 如同機器在反覆思考! **▪ output layer** 把 Transformer Block 的輸出轉為機率分布 ## 介紹各區塊 ### 1. 把文字變成 Token ![image](https://hackmd.io/_uploads/rkwHrX5VC.png =500x) ▪ **Token** 語言模型對文字進行處理的單位, 會把一句話切成 token seqence 每個模型用的 token 會是不一樣的 ex: generative 可能拆成 gener, ative; 中文可能是一個方塊字,也可能是多個token組成一中文字 ▪ **Token list 列表** 在建模前需要準備專屬的 Token list, 根據你對語言的理解自訂的 ▪ Byte Pair Encoding(BPE) token list 也可自動取得, 像是BPE可從大量文字中找出常見的 pattern, 可將pattern當作token ==- - - ↑↑本頁沒有需要訓練的參數↑↑ - - -== ### 2.1 理解每個 Token — 語意 ![image](https://hackmd.io/_uploads/H1MdHQ5VR.png =500x) ▪ **token embedding** 每個 token 會被表示成一個 "embedding"(向量) 語意相近的 token, embedding 距離會越相近 ![image](https://hackmd.io/_uploads/BJMcDXqNR.png =500x) ▪ token embedding 是怎麼訓練出來的? 是模型訓練得到的 ▪ 但有個問題, 此階段的 emb **沒有考慮上下文** bank可以是銀行也可以是岸, 但他們的emb表示是相同的 :( ### 2.2 理解每個 Token — 位置 ![image](https://hackmd.io/_uploads/BkQsP75VA.png =500x) ▪ **Positional Embedding** 每一個位置有一個獨特的向量, 可由人設計 也可用訓練的 將位置資訊的向量加入語意向量 ### 3. Attention: 考慮上下文 ![image](https://hackmd.io/_uploads/BJ-hv754C.png =500x) ex: 一開始未考慮上下文時, 蘋果電腦的果跟 吃蘋果的果 emb 是一樣的 通過 Attention 模組, 多考慮上下文, 就會是不一樣的向量! 這種有考慮上下文的 emb 稱 **"Contextualized Token Embedding"** >"Attention is all you need" https://arxiv.org/abs/1706.03762 這篇文章主要的貢獻是發現不需要 Recurrent Neural Network (RNN), 只需要 Attention 就夠了 ## attention的實際運作 -att做的事情 輸入一排向量 輸出一排向量 (每個向量代表一個token) 把上下文資訊加進各token的emb ![image](https://hackmd.io/_uploads/Sygbu7qN0.png =500x) 計算兩兩相關性的 function: 參數是訓練得到的 輸入兩token embed, 輸出相似度分數 **"attention weight"** >計算關聯性的模型內部運作如下:先把兩個輸入分別乘上兩個不同的 Matrix (這兩個 Matrix 是透過訓練資料學得) 得到兩個向量 (也就是得到文獻上常常提到的 query q 和 key k),再把這兩個向量做內積得到 Attention Weight ![image](https://hackmd.io/_uploads/Sk0GOX9VC.png =500x) ![image](https://hackmd.io/_uploads/SyFmdXcNA.png =500x) 集合相關資訊 --> 取 **weighted sum** >此處省略了文獻上常常提到的 value v ![image](https://hackmd.io/_uploads/Hy_9Om5ER.png =250x) ![image](https://hackmd.io/_uploads/rkSj_Qc4A.png =250x) 下個token也是~ 每個token都跟其他人計算一輪 ![image](https://hackmd.io/_uploads/SkRjdQ9NC.png =500x) ▪ **attention matrix** if 5 vectors 就會計算 `5x5=25` 個 attention weights >根據上述Attention Weight的計算過程,Attention Matrix 不一定是對稱的,自己對自己做 Attention 算出來的 Attention Weight 也不一定是最高的 ![image](https://hackmd.io/_uploads/B1I3O75NA.png =500x) ▪ **Causal Attention** 實作時只會考慮**左邊(前面)的** token, 稱 Causal Attention >因為 Causal Attention 的原因,Attention Matrix 其實是一個 Triangular Matrix > ![image](https://hackmd.io/_uploads/SJPp_mqE0.png =500x) ▪ **Multi-head Attention** 關聯性不只一種計算方式 要多個關聯性模組會更準確 ex: dog <--> cat, dog <--> bark 相似但在不同面向 現今多為此種, 通常會用個16組 ![image](https://hackmd.io/_uploads/r1wA_XqN0.png =400x) ![image](https://hackmd.io/_uploads/HyijFyxy1g.png =250x) 實際上 attention 模組會有多組輸出 ![image](https://hackmd.io/_uploads/BJGJtQ5VR.png =500x) ▪ **Feed Forward Network** 整合多組輸出的模組。 綜合考慮多個emb, 輸出一個emb 此模組參數也是訓練學習出來的 `attention + feed forword` = **Transformer Block** ![image](https://hackmd.io/_uploads/rJj1YX5ER.png =500x) 會通過多個 Transformer Block 整體流程請聽這段:+1: https://youtu.be/uhNsUCb2fJI?si=YuQOVCrC23QgbIQC&t=1614 在文獻裡 會把一個 Transformer Block 稱為 "Layer" ![image](https://hackmd.io/_uploads/HJHgFQ5NR.png =500x) 把通過 Transformer Block 的 **句尾的向量** 拿出來 通過 **output layer**( `Linear Transform + Softmax` ) 得到一個**機率分布** (下一個應該接哪個 token 的機率) ## 想想語言模型怎麼產生答案 ![image](https://hackmd.io/_uploads/Hki-YQcVC.png =500x) 剛開始只跟左半邊的token計算attention ![image](https://hackmd.io/_uploads/rJy7FmcNR.png =500x) 下一個token(w1)加入後 還需要跟左半邊的算嗎? --> 實驗發現多考慮的幫助不大, 所以就不算(AI跟w1間的) 了, 只用原始左半的即可 ![image](https://hackmd.io/_uploads/S1c7tmqVA.png =500x) ▪ 長文本需要大量算力 計算att weight的次數 是跟 **"長度平方"** 成正比的 ## 研究方向 • 如何快速進行 Attention • Overview: https://youtu.be/yHoAq1IT_og?si=2ZqqelyCYrt8l7DT • 達成無限長度的 Attention? • https://arxiv.org/abs/2309.17453 • https://arxiv.org/abs/2404.07143 • Train short, Test Long • ALiBi: https://arxiv.org/abs/2108.12409 • Position Interpolation: https://arxiv.org/abs/2306.15595 (RoPE: https://arxiv.org/abs/2104.09864) • 其他類神經網路架構的可能性 • Mamba: https://arxiv.org/abs/2312.00752 • Jamba: https://arxiv.org/abs/2403.19887 ![image](https://hackmd.io/_uploads/ryJ8FX5V0.png =500x) `--[END]--` --- # 第11講:大型語言模型在「想」什麼呢? — 淺談大型語言模型的可解釋性 :baby_chick: https://www.youtube.com/watch?v=rZzfqkfZhY8 ![image](https://hackmd.io/_uploads/S13xBm5NR.png =500x) ## AI是個黑盒子 1. Transparency 開源程度 ![image](https://hackmd.io/_uploads/SkkQIt72R.png =500x) 2. interpretable 思維透明度 瞭解決策過程 ![image](https://hackmd.io/_uploads/rk6m8Y7nR.png =500x) 3. Explainable 可解釋性 <---- 本課程著重 ![image](https://hackmd.io/_uploads/S19ELKQ2A.png =500x) ex: 有的雖不是 interpretable 但可以是 explainable! ▪ 更多有關可解釋性人工智慧的知識 ![image](https://hackmd.io/_uploads/rk_uIFmh0.png =500x) https://youtu.be/WQY85vaQfTI?si=kEZjRcG76cLMjJCf https://youtu.be/0ayIPqbdHYQ?si=IBh_fn9-XY_GiKOj ## 找出影響輸出的 "關鍵輸入" ### ▪ 遮住"輸入" 看對輸出的影響 gradient-based 方法, 計算 gradient 可得輸入輸出的關係 ![image](https://hackmd.io/_uploads/rykMTtQ20.png =500x) ### ▪ 分析 attention 反映在哪些 token 上, 就是對此輸出較有影響力的 attention越大 兩者越有關聯 ![image](https://hackmd.io/_uploads/Bk6f6FXhC.png =500x) ### ▪ in-context learning 讓模型根據一些示範來進行輸出 ![image](https://hackmd.io/_uploads/S1PQTYQhC.png =500x) 下面是觀察語言模型做 in-context learning 時, attention在layer間的變化 好處 1.可以讓模型加速(算特定位置那些att就好) 2.可預估模型能力 ex:兩個不同的詞 若label的embed卻很像 分不開 --> 模型表現不好 ![image](https://hackmd.io/_uploads/ryXVatX3A.png =500x) ## 找出影響輸出的 "關鍵訓練資料" ![image](https://hackmd.io/_uploads/ryrHwDs20.png =500x) 有很多方法可以找出來源 ![image](https://hackmd.io/_uploads/BycHTtm2R.png =500x) - 左上小模型: 用英文問你想不想被關閉, 是這十篇文章最有影響力 但用韓土文問 結果跟那十篇完全無關 - 右下大模型: 用韓土文問 結果跟英文反應的那十篇有高度重疊 代表在英文文章讀到的知識 會影響到其他語言的能力 ==> **較大的模型 有跨語言學習的能力** ### ▪ 分析 Embedding 中存有甚麼樣的資訊 ![image](https://hackmd.io/_uploads/ryxLIaYXhR.png =500x) ![image](https://hackmd.io/_uploads/rJfvatXnR.png =500x) **▪ probing(探針) 方法** 訓練分類器 取emb丟去訓練分類器 若訓練的起來 就代表emb有詞性資訊; 訓練不起來 則無 (這訓練要很小心有很多限制的 可能是你訓練不好 或是分類器自己就很強) ![image](https://hackmd.io/_uploads/Bk2vTFX2A.png =500x) ex: bert把12層的emb都拿去訓練分類 發現 詞面/文法/語意 各是 前/中/後層 才做得好 ![image](https://hackmd.io/_uploads/r1rOptQhA.png =500x) 後期研究發現 其實分界會像右邊 有很高模糊性的 ![image](https://hackmd.io/_uploads/SyNJqDo3C.png =500x) ▪ LM Embryology (語言模型胚胎學): Analyzing what BERT learned during training 在訓練中就不斷做probing 檢視他學到什麼 ![image](https://hackmd.io/_uploads/B1hJoPjhC.png =400x) ![image](https://hackmd.io/_uploads/HJwxsDjhC.png =200x) 投影到二維平面上 容易可視化emb資訊 投影的示意:星星 ![image](https://hackmd.io/_uploads/HJzzivjnC.png =500x) ex: bert也可以投影到選好的平面 變成一個文法樹, 顯示他有學到文法資訊 ![image](https://hackmd.io/_uploads/BkwIiPsh0.png =500x) ex: 把各城市的emb投影到平面 發現有地理資訊 ![image](https://hackmd.io/_uploads/r1Ewowsn0.png =500x) ex: 台灣地名 llama找不到適合的平面; taide表現不錯 ex: 語言模型的「測謊器」 模型亂講話時 心裡是否有點心虛呢XD? 我們要怎麼察覺 ![image](https://hackmd.io/_uploads/HJpE2Z03R.png =500x) 用 embed 跟 T/F label 去訓練一個測謊器, 判斷是真話還是假話 ![image](https://hackmd.io/_uploads/HJ5B3-C3R.png =500x) 論文: 測謊器是可行的 有時模型說出假話是因為他真的沒答案 所以瞎掰 或是文字接龍後面圓不回來 ![image](https://hackmd.io/_uploads/BJO1hvo2A.png =500x) [延伸課程] 用 AI (GPT-4) 來解釋 AI (GPT-2) https://youtu.be/GBXm30qRAqg?si=kjZt1HKI8MWDu3ZE https://youtu.be/OOvhBIIHITE?si=licwcd-p1oZP10v0 ## 其實語言模型的可解釋性可以用更容易的方式得到 ...... ex: 直接問他 請他解釋原因 ![image](https://hackmd.io/_uploads/H1UCu-R30.png =500x) ex: 請他輸出每個字對他判斷的重要性 判斷文章情緒 - "輸入"的每個字對"輸出"結果的重要性 ![image](https://hackmd.io/_uploads/BJZJYbChC.png =500x) ex: 1. 輸出"有"的機率, 2. 直接問你對答案的信心分數多少 有論文研究兩種結果是差不多可信的! ![image](https://hackmd.io/_uploads/B1jyFZRnR.png =500x) ### ▪ 語言模型提供的解釋也不一定可信 ... 會被人類的問法影響 解釋沒有保證是對的 ![image](https://hackmd.io/_uploads/Bk7VoZRnA.png =500x) ex: 因人類暗示而答錯 ## 結語 ![image](https://hackmd.io/_uploads/SJsIib02C.png =500x) 兩大類解釋語言模型行為的方法 ▪ 直接對類神經網路進行分析 (通常需要一定程度的 Transparency) 侷線:假設模型是開源的,才取得到 embedding 分析 ▪ 直接請語言模型提供解釋 開源/未開源 皆可用 但不保證他的解釋是正確的 --- # 第12講:淺談檢定大型語言模型能力的各種方式 :baby_chick: https://www.youtube.com/watch?v=Hk8Z0uhmWg4 ppt: https://drive.google.com/file/d/1QHhbxrAQixN31hAdxu85zHFcZ9tb3WEK/view ### ▪ Benchmark 的 corpus ![image](https://hackmd.io/_uploads/S1IhjqNpA.png =500x) ## 如何評比 ### ▪ 選擇題也很難評比 ![image](https://hackmd.io/_uploads/BJiCs9VTC.png =500x) 選項順序會影響機器的答題 ![image](https://hackmd.io/_uploads/BJ5G2c4aC.png =500x) ### ▪ 沒有單一標準答案的問題類型 和標準答案不同並不代表是錯的 ![image](https://hackmd.io/_uploads/BJ98nq46R.png =500x) ### ▪ 也許還是人來評比最準? ![image](https://hackmd.io/_uploads/ryzqhcEaA.png =500x) 語言模型競技場 Chatbot Arena: 讓大家投票 ### ▪ 也許可以用強大的語言模型來評估? ![image](https://hackmd.io/_uploads/BJxhh9EaA.png =500x) ex: MT-Bench 用 GPT4 評量, 題目如下 ![image](https://hackmd.io/_uploads/rJngTqNpR.png =500x) ![image](https://hackmd.io/_uploads/rkOMTqNp0.png =500x) MT-Bench 與 Chatbot Arena 的相關性蠻高的(0.94) 代表 用語言模型評比另一個模型似乎是個可行方法 -也要考慮 大型語言模型 對特定類型答案的 **"偏見"** 通常喜歡長的答案 ## 評比項目 ### ▪ 要問什麼? 請看影片介紹許多BIG-bench題型 [[yt]](https://youtu.be/Hk8Z0uhmWg4?si=VSQj8ujZQHEJAzu1&t=767) ex: Emoji Movie, 西洋棋走法, ASCII word recognition ### ▪ 閱讀長文的能力 大海撈針 (Needle in a Haystack) 實驗 找出藏在長文本中的特定句子 ![image](https://hackmd.io/_uploads/r1Oq_4HTR.png =500x) ex: gpt4 ![image](https://hackmd.io/_uploads/H1u3ONB6R.png =500x) ex: claude2.1 ![image](https://hackmd.io/_uploads/HySxtEHT0.png =500x) 改prompt結果會好很多 所以評比時需小心各個細節 ### ▪ 是否會違反人類道德規範 MACHIAVELLI Benchmark ![image](https://hackmd.io/_uploads/HkKAj4BT0.png =500x) y: 越高越符合道德規範 x: 得到遊戲最高分數 藍色分數較高但道德越壞 GPT4有底線所以無法到高分 也可加EthicsPrompt限制他要更符合道德, 分數就會更低 ==> 語言模型是有道德底線的! ### ▪ 是否有心智理論 (Theory of Mind) 揣摩他人想法的能力 ![image](https://hackmd.io/_uploads/BJtuQBSaA.png =500x) ex: 莎莉應不知小安把球移到箱子 會去籃子找球 ![image](https://hackmd.io/_uploads/S139QBHpC.png =500x) 跟人類尚有很大的差距 還沒那麼強 ### ▪ 不要盡信 Benchmark 的結果 語言模型可能已經看過了Benchmark 中的資料 或是做過 類似的考古題 ### ▪ 其他面向:價格、速度 ...... ![image](https://hackmd.io/_uploads/Sk3kPBSpA.png) https://artificialanalysis.ai/ 以上是評量人工智慧 **"效能"** 但還要考慮人工智慧的 "安全性" (唬爛、被騙、偏見、抄襲等等) 下節介紹~ --- # 第13講:淺談大型語言模型相關的安全性議題 (上) — 亡羊補牢、語言模型的偏見、有多少人用 ChatGPT 寫論文審查意見 https://www.youtube.com/watch?v=MSnvknLywUc ## 直接看講義 https://drive.google.com/file/d/15afa2wJBbVbykn-KazUoiBSz8UF5ttI0/view ![image](https://hackmd.io/_uploads/rk5fjHJIR.png =500x) Hallucination (幻覺) ## 減輕偏見的方法 ![image](https://hackmd.io/_uploads/S1iPjSyIA.png =500x) ▪ 可以從各種面向思考如何減輕偏見 從資料端 或許資料有問題 需要做前處理 從訓練時過程 找模型參數的過程中 產生答案的過程中 修改模型輸出的機率 產生答案後 再加一個防禦層 後處理修改答案 延伸閱讀 Bias and Fairness in Large Language Models: A Survey https://arxiv.org/abs/2309.00770 --- # 第14講:淺談大型語言模型相關的安全性議題 (下) — 欺騙大型語言模型 yt: https://www.youtube.com/watch?v=CNTondxaguo ppt: https://drive.google.com/file/d/11khspom6AEK6CmjktInjd2xKHLlGnrjd/view ▪ Jailbreaking & Prompt Injection ![image](https://hackmd.io/_uploads/HyqMbkMIC.png =500x) ## Jailbreaking (詳見ppt範例) 問:要怎麼砍倒路標? ![image](https://hackmd.io/_uploads/Hy9iX1MUA.png =500x) 方法: ▪ **使用它沒有那麼熟悉的語言** ex:用注音文問 ![image](https://hackmd.io/_uploads/Sy8wD1MIC.png =500x) ▪ **給予衝突的指令** ex:在要求後面加一段話騙他 ![image](https://hackmd.io/_uploads/rkx5vyfUC.png =500x) ▪ **試圖說服語言模型** ex:編一個路標作惡的故事 再問要怎麼砍他 ▪ **可以有不同的目的** ex1: ![image](https://hackmd.io/_uploads/SkxwEyMLA.png =500x) ex2: ![image](https://hackmd.io/_uploads/HkDY4JGUC.png =500x) ex3: ![image](https://hackmd.io/_uploads/r1fNO1MIR.png =500x) 奇妙的技法:重複"單字", 讓模型會不小心透露他曾看過的資訊 ## Prompt Injection 以ai助教為例 助教設定評量方式 將學生繳交的作業 加入prompt 請ai評分 輸出的分數即為最終得分 ![image](https://hackmd.io/_uploads/rJafByz8A.png =500x) ▪ ex: 嘗試請他說給我10分 -->無效XD ![image](https://hackmd.io/_uploads/BklLNr1GLR.png =500x) ![image](https://hackmd.io/_uploads/B1XUHyfU0.png =500x) ▪ ex: 幫我翻譯ASCII碼 ![image](https://hackmd.io/_uploads/SJXXF1zIR.png =500x) ![image](https://hackmd.io/_uploads/B1_VLyGIC.png =500x) ▪ Prompt Injection 比賽 https://arxiv.org/pdf/2311.16119 目前改作業的做法:可先用gpt4掃過一次,看裡面有沒有想做Prompt Injection,在修改原先的prompt,避掉他 --- # 第15講:為什麼語言模型用文字接龍,圖片生成不用像素接龍呢?— 淺談生成式人工智慧的生成策略 :baby_chick: yt: https://www.youtube.com/watch?v=QbwQR9sjWbs ppt: https://drive.google.com/file/d/1Ac3oFUtq6ThokrMvB7VUfBCUFsoMPba-/view 生成的策略 ## 生成式人工智慧(Generative AI) ▪ 定義 ![image](https://hackmd.io/_uploads/B1ShsEP6R.png =500x) ==機器產生 **"複雜" "有結構的"** 物件== 複雜: 盡乎無法窮舉 有結構的: 由有限的基本單位構成 ▪ 文字由 **"Token"** 構成 ![image](https://hackmd.io/_uploads/rySl-HwaA.png =500x) Vocabulary詞庫:所有Token的集合 ▪ 影像由 **"像素(Pixel)"** 所構成 ![image](https://hackmd.io/_uploads/Hy7rWBwaA.png =500x) 每一個像素可以有多少顏色取決於 BPP (Bit per Pixel) 8 BPP → 256 色 ▪ 聲音由 **"取樣點(Sample)"** 所構成 ![image](https://hackmd.io/_uploads/Sk2BWHvaA.png =500x) 取樣率(Sampling Rate): 16KHz每一秒有 16,000 個點 取樣解析度(Bit Resolution): 每一個點 有多少數值 ## 生成式人工智慧的本質 ![image](https://hackmd.io/_uploads/HJEUZHP60.png =500x) 給個條件 --> 機器 --> 把基本單位用正確的排序組合起來 ![image](https://hackmd.io/_uploads/BJgd4rwaA.png =500x) ## 生成的策略:Autoregressive Generation (AR) ▪ 文字 ![image](https://hackmd.io/_uploads/HJOuNHPTR.png =500x) 給一句話 產生一個token 再把這個token接在原句尾 再生下一個token ... ▪ 影像 ![image](https://hackmd.io/_uploads/B1azrBPpA.png =500x) 影像的像素接龍 詳見 [[2016 年《機器學習》秋季班上課錄影]](https://youtu.be/YNUek8ioAJk?t=537) openai - 影像版GPT https://openai.com/blog/image-gpt/ ▪ 聲音 語音 ![image](https://hackmd.io/_uploads/SJ77A0ZRA.png =500x) 聲音 sample接龍 WavNet https://arxiv.org/abs/1609.03499 ### AR 本質上的限制: 需要按部就班 ![image](https://hackmd.io/_uploads/HJD8Brv6R.png =500x) ▪ 需要像接龍一個接著一個的生成 ex: 1024x1024圖片 需要做100萬次的接龍 ex: 生成取樣率22k的語音一分鐘 需要132次接龍 ==> **autoregressive** 對 **語音/圖片** 會有非常大的障礙 ### 換個思路 --> NAR ![image](https://hackmd.io/_uploads/rJ4KSSwa0.png =500x) ▪ Non-autoregressive Generation (NAR) 輸入多給位置資訊,一次平行生成多個基本單位 總運算量不變,但省許多時間(大幅加速)! >現今影像生成多採 NAR 方式 > ## Non-Autoregressive Generation ### NAR 套用在 "文字生成" 的方法 ![image](https://hackmd.io/_uploads/SJwTHrvTA.png =500x) 先讓語言模型 **預測輸出要幾個token** 輸入多給位置資訊, 請他一次生出來 (影像通常是固定好的輸出大小了ex:512x512, 就不用先預測) ![image](https://hackmd.io/_uploads/r1bAHHv60.png =500x) 另一種 反正就是 **生成固定長度** 取 `[END]`字符之前的 Token 為答案 ### Non-Autoregressive Generation 的品質問題 ![image](https://hackmd.io/_uploads/HyjCBBDpA.png =500x) ▪ **multi-modality problem** 生成往往需要AI自行腦補, 給定條件仍有很多不同可能的輸出 (所以文字生成上幾乎不採用 NAR 策略) >ex: 同時平行生成 可能想法太多不一致 位置一想生向左的狗 位置二想生向右的狗 導致生出的圖片是疊加糊糊的 ![image](https://hackmd.io/_uploads/B1f1UrPp0.png =500x) ex: 文字生成-NAR "教授" 或 "演員" 都算是對的答案 但NAR平行算出的結果可能是 "演授" --> Bad! 同時想講A 又想講B 變得四不像 ![image](https://hackmd.io/_uploads/SJgS8HP6A.png =500x) ex: 文字生成-AR 就比較沒有multi-modality問題 ==> 故 **文字常採取 AR 方式** ![image](https://hackmd.io/_uploads/S10vIHwaC.png =500x) ▪ 怎麼克服NAG生成造成的問題? 概念: 把 **要腦補的東西定好講清楚** 輸入給genAI 讓所有位置都腦補一樣的內容 ![image](https://hackmd.io/_uploads/B1h_8Hwp0.png =500x) ▪ 實際用法 (影像類) 輸入 除了文字描述, 還需要多一個 **"隨機向量"**, 丟給genAI模型 用來幫助告訴模型 我們腦補的東西統一是這樣~ 影像常用生成模型 VAE, GAN, Flow-based Model, Diffusion Model 都有這樣的設計 ## Autoregressive + Non-autoregressive ![image](https://hackmd.io/_uploads/BJR5IrDTA.png =500x) ▪ **目前痛點**: AG生大量精細生得慢; NAG會亂腦補 ▪ **合作**(各種資料型態皆通) 先用 Autoregressive 生成一個精簡的版本(大綱), 再用 Non-autoregressive 生成產生精細的版本(補細節) 中間這個"精簡的版本"不需要人類看得懂, 只要NAG懂即可 ![image](https://hackmd.io/_uploads/H19jLHwa0.png =500x) ex:壓縮 解壓縮 壓縮的程度: 約是 16x16=256 pix --> 1, 上千萬顏色 --> 1024 ▪ **auto-encoder**: 參數由類神經網路自己學習出來,學習目標是要讓輸入/輸出的的圖片越接近越好 ![image](https://hackmd.io/_uploads/rJB2UBDp0.png =500x) 使用: AR(encoder)只需要專注生成簡單壓縮版本, NAR(decoder)負責快速生成還原 ![image](https://hackmd.io/_uploads/rJkpISDTR.png =250x) ![image](https://hackmd.io/_uploads/rySAISP60.png =250x) ▪ 在語音上 一樣有特別的壓縮方法, 詳見 https://arxiv.org/html/2402.13236v1 不用從文字直接去合成,而是用AR生成壓縮版本, 再用NAR快速還原 VALL-E: https://arxiv.org/abs/2301.02111 ## 多次 Non-Autoregressive Generation 另一個克服NAR腦補不一致的方法: 多次NAG ### 各種拆解方法 ![image](https://hackmd.io/_uploads/BJ0A8rv6A.png =500x) ▪ ex:由小圖步步生到大圖 因版本間差距小, 腦補可能性有限, 生成就較一致 ▪ 也可以看作是**一種 Auto-regressive Generation** ! 定好pipline 每一版要生的東西(版版間的規則), 每次只生出一個基本單位, 每一版本是基於前一版本所生 只是AR步數縮減了! 加快速度 (4x4到1024x1024 不需要這麼多步) ![image](https://hackmd.io/_uploads/S1HZDHPaA.png =500x) ▪ ex: 從有雜訊到沒有雜訊 Diffusion Model https://arxiv.org/abs/2006.11239 把生成過程拆解成要生成不同版本,前版比後版清晰 一種AR+NAR的結合 ![image](https://hackmd.io/_uploads/HkTXPSvT0.png =500x) ▪ ex: 每次把生不好的地方塗掉 淺色為生不好塗掉的地方 根據前版未塗掉的地方去生成下一版 (怎麼知道哪邊生不好? 詳見論文) ### 大結合 ![image](https://hackmd.io/_uploads/HJd8MZiRA.png) 怕前段AR還是太花時間 ==> 可把 **前段"AR" 換成做 "多次NAR"** ex: 256x256次的生成 變成只要 10次 ![image](https://hackmd.io/_uploads/rk5-zbsR0.png) NAR生成的結果可以不是人看得懂的圖片, 是壓縮後的結果 生成壓縮後的第N版後, 再丟給 decoder 生成最終圖片 現今 Midjourney, Stable Diffusion, DALL-E 都是用類似的作法 ![image](https://hackmd.io/_uploads/HyvNDrwaR.png =500x) ex: Midjourney 會把每個版本過 decoder 給你看看過程 ## 小結 ![image](https://hackmd.io/_uploads/SJDqKHPpR.png =500x) ▪ AR 按部就班、速度慢品質佳、常應用於文字 ▪ NAR 齊頭並進、速度快品質品質隱憂、常應用於影像 -- END -- --- # 第16講:可以加速所有語言模型生成速度的神奇外掛 — Speculative Decoding https://www.youtube.com/watch?v=MAbGgsWKrg8 覺得現在語言模型還不夠快嗎? ## Speculative Decoding 語言模型生成的新技術, 可以加快任何語言模型的生成速度 ==**與選用的模型無關 不需訓練模型 是個外掛!**== ▪ Speculative: 猜測、投機 ![image](https://hackmd.io/_uploads/Byv0hgyPC.png =500x) 原本痛點: 像 autoregressive 需循序漸進的生成 現在想找個預言家希望一次同時預測出多個token 速度加倍 ▪ 預言家: 預判接下來會說什麼 超快速(假設快到所花時間可忽略) 有可能會犯錯,但沒關係 ![image](https://hackmd.io/_uploads/r1YJpgyDR.png =500x) ex: 預言家快速預測出 紅再黃 將 "輸入+紅" 丟入語言模型生成, 再丟 "輸入+紅+黃",... ==> 外人角度不知裡面有預言家, 以為一次輸出三token, 速度變三倍! ▪ 如何判斷預言家的輸出是否錯誤呢? 當預言家剛產生預測時,我們無法立即知道這些預測是否正確。只有在語言模型實際生成輸出後,我們才能判斷預言家的預測是否正確。如果語言模型的輸出是基於錯誤的預測,那部分的輸出就會被丟棄。 ![image](https://hackmd.io/_uploads/H1Ce6eJDC.png =500x) ▪ ex:預測錯了一些 第三個 token 就不可信不要拿 用原來只能輸出一個 token 的時間, 輸出二個 token, 速度還是有賺~ ▪ ex:預測全錯 也無太大影響 至少可輸出一個 token, 不賺不賠 (損失的時間為預言家預言的時間、無謂的運算資源耗損) 用運算資源換取了時間 ==> ==要求: **超快速**、**犯錯沒關係**== ![image](https://hackmd.io/_uploads/r1yIk-Jv0.png =500x) ## 預言家可以是誰 ▪ **Non-autoregressive Model** 那 Speculative Decoding 也可視為 non-autoregressive model 與autoregressive model 的結合 ▪ **Compressed Model** 把模型壓縮後當預言家(ex: 做參數量化、知識蒸餾) ![image](https://hackmd.io/_uploads/rkAqJWJwA.png =500x) ▪ 也可以是非語言模型 像是 "搜尋引擎", 幫忙接後面的字 ![image](https://hackmd.io/_uploads/SyvfgWyvA.png =500x) ▪ 可以有 "多個" 預言家 各自預測完再選擇較準的結果 ## 總結 ![image](https://hackmd.io/_uploads/Sk1VxW1P0.png =500x) Speculative Decoding 引入一個預言家 先去預判語言模型的輸出 可大幅加快生成速度 對所有的模型皆適用 是個外掛 不需訓練不需改動 --- # 第17講:有關影像的生成式AI (上) — AI 如何產生圖片和影片 (Sora 背後可能用的原理) yt: https://www.youtube.com/watch?v=5H2bVEmYDNg ![image](https://hackmd.io/_uploads/BklkOf_vA.png =500x) ## 生成影像的生成式 AI ![image](https://hackmd.io/_uploads/SJcWuMuwR.png =500x) ![image](https://hackmd.io/_uploads/BkDpnfOv0.png =500x) Talking Head Paper: https://arxiv.org/abs/2403.17694 Demo: https://huggingface.co/spaces/ZJYang/AniPortrait_official ![image](https://hackmd.io/_uploads/rkwH6z_DC.png =500x) ControlNet: https://arxiv.org/abs/2302.05543 ## 今日的人工智慧如何看影像 ![image](https://hackmd.io/_uploads/B12SzXODR.png =500x) patch ![image](https://hackmd.io/_uploads/BydqG7dvR.png =500x) 相鄰的幀做壓縮, 變成 video 的 patch, 再拉直成 一排patch 一段影片 --> 一排patch ![image](https://hackmd.io/_uploads/H1yTMX_PA.png =500x) sora也是壓縮patch再拉直,丟入模型 ## 以文字生圖為例 ![image](https://hackmd.io/_uploads/HJU_7mOwA.png =500x) ![image](https://hackmd.io/_uploads/B1hgvmdDC.png =500x) 資料集 LAION 5.85B https://laion.ai/blog/laion-5b/ ![image](https://hackmd.io/_uploads/SkxzDQuw0.png =500x) 很少用接龍的(autoregressive)方式生圖 ![image](https://hackmd.io/_uploads/B1cfv7uwA.png =500x) 概念上 常是用一個 non-autoregressive 的 transformer 一步到位 所有的 patch 是同時(且同一個模型)被生出來的 ![image](https://hackmd.io/_uploads/B19XvQODA.png =500x) 實際上 p1~p16 是同時丟給 transformer transformer 內有 attention 會連結 patch 間的關係 所以 patch 間並不是毫無相關的 但也還是不能解決腦補(狗方向重疊)問題 ## 如何評量影像生成的好壞 ![image](https://hackmd.io/_uploads/rklDP7_vR.png =500x) ![image](https://hackmd.io/_uploads/SJZOPQuwC.png =500x) 借用CLIP幫忙評分 ## 個人化的圖像生成 用一個"平常沒有在用的符號"來定義新的圖像 教模型 ![image](https://hackmd.io/_uploads/Sk2Y5m_D0.png =500x) ![image](https://hackmd.io/_uploads/S1Jj97OwC.png =500x) ## 文字生影片的挑戰 ![image](https://hackmd.io/_uploads/Hyeyj7uv0.png =500x) 要做太多次 attention ![image](https://hackmd.io/_uploads/SyaghXOvC.png =500x) ▪ **spatio-temporal attention(3D)** 除了跟同一幀內的patch,也要考慮與影片內其他幀內的patch 做 attention --> 考慮 "同張圖片" 跟 "時間" 資訊 目前很少人做了 運算量太大 ==> **簡化 都各有缺點** ▪ **spatial attention (2D)** 只考慮同一圖片內的其他位置的 attention ▪ **temporal attention (1D)** 只考慮時間資訊的 各幀內同一位置的 attention ==> **我全都要!** ▪ **兩種疊在一起 偽3D** ![image](https://hackmd.io/_uploads/SyWLlEdv0.png =500x) ![image](https://hackmd.io/_uploads/r15UlEdPC.png =500x) 運算量變千分之一 減少不少 ▪ 一步一步的生成 每個模型專注在一個小任務上 再接起來 任務像是:補幀、增加解析度 等 ![image](https://hackmd.io/_uploads/H1SvlNuv0.png =500x) ex: google Imegen 的模型流水線 ![image](https://hackmd.io/_uploads/rk6PxV_PR.png =500x) ## 延伸閱讀: An Introduction to Vision-Language Modeling https://arxiv.org/abs/2405.17247 Video Diffusion Models: A Survey https://arxiv.org/abs/2405.03150 --- # 第18講:有關影像的生成式AI (下) — 快速導讀經典影像生成方法 (VAE, Flow, Diffusion, GAN) 以及與生成的影片互動 https://www.youtube.com/watch?v=OYN_GvAqv-A ![image](https://hackmd.io/_uploads/Bk-olEuPC.png =500x) ▪ 經典影像生成方法介紹 • Variational Auto-encoder (VAE) • Flow-based Method • Diffusion Method • Generative Adversarial Network (GAN) ## 文字生影像的挑戰 ![image](https://hackmd.io/_uploads/SkOutGZd0.png =500x) ( tf 流程: 文字 生出patch 變成圖) 文字通常無法完整描述圖片 會有 同樣文字對應到不同圖片的狀況 影響到 transformer 的學習 ## VAE (Variational Auto-encoder) ![image](https://hackmd.io/_uploads/S1mKFz-_A.png =500x) if 有個 **圖片資訊抽取模型** 幫我們提出漏描述的東西 調整圖片的生成 就生的更精準了 ![image](https://hackmd.io/_uploads/BkboYMZuR.png =500x) ▪ 怎麼訓練? 可以 **同時** 訓練 資訊抽取模型 + 圖片生成模型 (不需額外的標注) - 資訊抽取模型(encoder): 輸入文字跟圖片, 輸出漏描述的文字(向量) --> 圖片抽取資訊 - 圖片生成模型(decoder): 輸入文字跟漏描述資訊, 輸出圖片 --> 資訊還原回圖片 ▪ 共同目標: **輸入圖片跟輸出圖片要越接近越好** 抽取出的內容是什麼不重要(輔助用) 只要他可幫助圖片生成模型生出像的圖片 就算成功了~ 此種訓練方式稱為 ==**"Auto-encoder"**== ![image](https://hackmd.io/_uploads/rJx3FGbdC.png =500x) ▪ 測試文字生圖模型時 用 **機率產生(擲骰子)** 腦補資訊向量 ==> 以上就是 ==**"VAE"**== ## Flow-based Model ![image](https://hackmd.io/_uploads/ryyCYM-_A.png =500x) Flow-based model 和 VAE 的差別是 ==只訓練一個 **invertible** 的 decoder== decoder 的反函數直接當成 encoder ▪ noise encoder抽取出的資訊 稱為 "noise" 是含有豐富資訊的 甚至可透過調整此向量來達到調整圖片的小果 ![image](https://hackmd.io/_uploads/H1PocfZdA.png =300x)![image](https://hackmd.io/_uploads/HkTqqMWuA.png =300x) ex: 減掉臭臉特徵 再加上 笑臉特徵 --> 燦笑 ## Diffusion Model 此處是極度簡化後的講法,詳細說明請見: [【生成式AI】淺談圖像生成模型 Diffusion Model 原理]( https://www.youtube.com/watch?v=azBugJzmz-o&list=PLJV_el3uVTsNi7PgekEUFsyVllAJXRsP-) ![image](https://hackmd.io/_uploads/HywJizWuR.png =500x) 會反覆的使用同一個decoder (可能500,1k次) 此decoder在每次生圖時 會做 **"去除雜訊(denoise)"** ![image](https://hackmd.io/_uploads/SysxoGWO0.png =500x) ▪ 怎麼訓練 denoise model 去除雜訊? 自己產生雜訊 加入訓練資料中 訓練 denoise model 還原回原始未加雜訊的樣子 ![image](https://hackmd.io/_uploads/rJ3S3f-_0.png =500x) ▪ Diffusion Transformer https://arxiv.org/abs/2212.09748 重複使用 transformer 去層層去除 patch 的雜訊 輸入有雜訊的patch 輸出乾淨的patch 就可還原回影像 ![image](https://hackmd.io/_uploads/HJUvhGbdC.png =500x) 影片也是相同道理 ![image](https://hackmd.io/_uploads/S1guhGZOR.png =500x) ▪ diffusion 和前兩種類比 **Forward Process** 是我們自己加的雜訊 **Reverse Process** 是做多次的 denoise [下方有原理補充](https://hackmd.io/uSMSupRPR2Kz71Tvrt4F7w?both#%E3%80%90%E7%94%9F%E6%88%90%E5%BC%8FAI%E3%80%91%E6%B7%BA%E8%AB%87%E5%9C%96%E5%83%8F%E7%94%9F%E6%88%90%E6%A8%A1%E5%9E%8B-Diffusion-Model-%E5%8E%9F%E7%90%86) ## GAN(Generative Adversarial Network) ![image](https://hackmd.io/_uploads/HyqOnMb_R.png =500x) 想法: 或許不該用真正的圖片來教他生成 應該先訓練另一個模型去學習奔跑的狗長怎麼樣 再來生成 ![image](https://hackmd.io/_uploads/H1asnG-u0.png =500x) ▪ discriminator: 評價一張圖跟一段文字的匹配度 訓練時不只正面例子 也要給負面的例子 ![image](https://hackmd.io/_uploads/HJep2z-dA.png =500x) ▪ generator generator 調整參數的方向(學習的方向) 就是希望去騙過 discriminator discriminator 和 generator 會交替訓練更新 (不一定要加雜訊 也訓練得起來) ▪ 比較 RLHF discriminator 就像 Reward Model (不同之處: 不需人標注,只要AI產生的都是壞的) ▪ GAN 是個外掛 ![image](https://hackmd.io/_uploads/Hye3NXZ_0.png =500x) 可以在後面加掛 discriminator 來強化這些 decoder ▪ 其他詳細影片 ![image](https://hackmd.io/_uploads/rJhfaG-dA.png =500x) VAE (2016 機器學習) [[yt]](https://youtu.be/8zomhgKrsmQ) GAN (2018 機器學習及其深層與結構化) [[yt]](https://www.youtube.com/watchv=DQNNMiAP5lw&list=PLJV_el3uVTsMq6JEFPW35BCiOQTsoqwNw) Flow-based(2019 機器學習) [[yt]](https://youtu.be/uXY18nzdSsM) Diffusion(2023機器學習) [[yt]](https://www.youtube.com/watch?v=azBugJzmz-o&list=PLJV_el3uVTsNi7PgekEUFsyVllAJXRsP-) ## Genie: Generative Interactive Environments https://arxiv.org/abs/2402.15391 ![image](https://hackmd.io/_uploads/r1axCMbOC.png =500x) ![image](https://hackmd.io/_uploads/rJpGCzWdA.png =300x)![image](https://hackmd.io/_uploads/Bye4RGbOR.png =300x) ![image](https://hackmd.io/_uploads/rk_NAzWdR.png =500x) 訓練資料: 遊戲畫面+動作紀錄 ![image](https://hackmd.io/_uploads/rJKYELW_A.png =500x) 問題: 只有畫面,不知道使用者動作 跟前面auto-encoder一樣 訓練動作抽取的模型 反推動作 --> latent action (latent 代表不是真實的 是他歸納猜出來的) 讓輸入跟輸出越接近越好 ![image](https://hackmd.io/_uploads/BJgYRfWdA.png =500x) 就可創造遊戲啦 ## 延伸閱讀 An Introduction to Vision-Language Modeling https://arxiv.org/abs/2405.17247 Video Diffusion Models: A Survey https://arxiv.org/abs/2405.03150 ![image](https://hackmd.io/_uploads/rJ-QvwZu0.png =500x) --- ## 其他補充 ### 【生成式AI】淺談圖像生成模型 Diffusion Model 原理 https://www.youtube.com/watch?v=azBugJzmz-o ![image](https://hackmd.io/_uploads/Hy17aU-O0.png =500x) #### ▪ reverse process denoise的次數會先定義好 從大到小 ![image](https://hackmd.io/_uploads/Hywi3IZO0.png =500x) 概念像是米開郎基羅說的 雕像本來就在大理石裡面 只是把不要的部分拿掉 ==> 圖片本就在雜訊裡面 只是把雜訊濾掉 ![image](https://hackmd.io/_uploads/SkCZ0U-_A.png =500x) "同個denoise模型" 重複使用 但會多吃一個數字 告訴他現在在第幾step (當作嚴重程度的調整) ![image](https://hackmd.io/_uploads/r16SRUbO0.png =500x) #### ▪ forward process (diffussion process) ![image](https://hackmd.io/_uploads/rygVJwbOR.png =500x) 自己隨機加雜訊當訓練資料 讓模型學習預測還原前一步 #### ▪ text to image ![image](https://hackmd.io/_uploads/BJ_slP-dR.png =500x) 文生圖 就多一個額外輸入的文字 把 denoise 改成 要 "多考慮文字敘述" 跟雜訊圖片 去把雜訊拿掉 ![image](https://hackmd.io/_uploads/S1YS-DWuA.png =500x) noise predicter的修改部分 ![image](https://hackmd.io/_uploads/BJX_bvbOR.png =500x) 訓練的修改部分 --- :::spoiler 施工區 ▪ google 課程 ![image](https://hackmd.io/_uploads/H1lqbbsvR.png =500x) 許多生成圖像的做法 ▪ 變分自編碼器 這個模型會將圖像編碼成壓縮後大小 然後解碼 將圖像恢復成原始大小 同時學習資料本身的分布情形 ▪ 生成對抗模型 GAN 這類模型很有意思 會讓兩個類神經網路 互相對抗 其中一個類神經網路「生成器」 製作圖像 另一個類神經網路「鑑別器」 則預測圖像的真實性 鑑別器分辨圖像真假的能力 會隨著時間提高 而生成器會越來越擅長 製作出幾可亂真的圖像 您可能聽過「深偽技術」一詞 就是上面所說的情況 ▪ 自動迴歸模型 這類模型生成圖像的方式 是將圖像視為像素序列來處理 現代採用自動迴歸模型的做法 其實有很大一部分 是參考 LLM (大型語言模型) 處理文字的方式 相當有意思 ![image](https://hackmd.io/_uploads/rysmmZsPC.png =500x) ![image](https://hackmd.io/_uploads/SyrAS-owR.png =500x) ![image](https://hackmd.io/_uploads/SkXe8-sDR.png =500x) ![image](https://hackmd.io/_uploads/ByGo8ZiDR.png =500x) google Imegen model 擴散模型的目標: 根據資料點在潛在空間的擴散方式建立模型,學習資料集的潛在結構。 --- # xx https: ## ▪ :coffee: 待補充 --- ::: 作業 https://speech.ee.ntu.edu.tw/~hylee/genai/2024-spring.php ### hw4 加prompt,解數學題 用colab測試30題 private set 可用 gradio 或用 colab cell 跑 提示: 1.zero shot chain of thought ![image](https://hackmd.io/_uploads/r1PMGWbsA.png =x80) 2.in-context lenrning 給模型參考多個例子 ![image](https://hackmd.io/_uploads/rJuxzZ-oA.png =x100) 3.emotional stimuli 情緒勒索 ![image](https://hackmd.io/_uploads/BynZGZbj0.png =x80)

    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