Finetuning Large Language Models
課程概要
- 微調(Fine-tuning)是將現成的大型語言模型(pre-trained large language model (LLM))如ChatGPT客製化到自己的資料和任務上的一種技術,比只利用提示(prompting)更進一步。
- 雖然提示(prompting)可指引語言模型,但微調可以讓模型更貼合資料定義的專門任務,包括調整語調和風格。
- 微調使您能藉由自己的資料專門化既有的語言模型,無需大量資料和運算資源從零訓練語言模型。
- 本課程將涵蓋微調的定義、適用情境、與提示及檢索的差異、ChatGPT所用的指令微調、以及用Python親手微調語言模型的實作。
- 必備知識為Python和基礎深度學習概念。目標是易學的語言模型微調入門。
Course Overview
- Why finetune
- Where finetuning fits in
- Instruction finetuning
- Data preparation
- Training Process
- Evaluation and Iteration
- Considerations on getting started now
- Conclusion
主要講師與課程設計
- Sharon Zhou。課程講師。Lamini。講解課程內容
- Nina Wei。課程設計。Lamini。課程設計
- Lamini
Lamini 的核心產品是一個開源的語言理解平台,可以讓使用者透過微調的方式,將現有的語言模型客製化為解決特定業務任務的 AI 助理。
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
課程概覽
- Fine-tuning 可以將一般用途的模型如 GPT-3 特化為特定用例,如將 GPT-4 轉換為程式碼完成的 GitHub Copilot。這有點像將全科醫生變成專科醫生
- Fine-tuning 允許模型從遠超過提示所能容納的大量資料中學習。這有助於更正不正確的資訊和「幻覺」(hallucinations)
- Prompt engineering 適用於快速原型製作,而 fine-tuning 更適合生產系統。
- 自定義 LLM fine-tuning 的好處包括更好的效能、隱私、成本控制和調節回應的能力
- 課程展示了一個非 fine-tuned LLAMA 模型對提示的糟糕回應,與 fine-tuned LLAMAChat 模型給出的更好回應形成對比
- 課程詳細地講解如何進行 fine-tuning
What is finetuning?
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
What does finetuning do for the model?
- 引導模型輸出更一致的結果Steers the model to more consistent outputs
- 減少幻覺 Reduces hallucinations
- 根據特定用例定製模型 Customizes the model to a specific use case
- 過程類似於模型早期的訓練Process is similar to the model's earlier training
Prompt Engineering vs. Finetuning
Image Not Showing
Possible Reasons
- The image was uploaded to a note which you don't have access to
- The note which the image was originally uploaded to has been deleted
Learn More →
- Prompt Engineering:
- 優點:
- 可以快速上手,不需要資料就可以開始使用
- 前期成本較低
- 不需要技術知識
- 可以透過檢索增強生成(RAG)接入部分資料
- 缺點
- 只能使用很少量的資料,大量資料無法放入提示中
- 容易「遺忘」提示中的資料
- 容易產生「幻覺」(hallucination),亂捏造資訊
- 檢索增強生成(RAG)可能會取回錯誤的資料
- 適用情境:
- Finetuning:
- 優點:
- 可以使用近乎無限量的資料進行訓練
- 模型可以從資料中學習新的知識
- 可以糾正模型原先學到的錯誤資訊
- 使用小型模型後可以降低成本
- 也可以配合RAG使用
- 缺點:
- 需要大量高品質的訓練資料
- 需要額外的計算資源,前期成本較高
- 需要一定的技術能力,尤其是處理資料的技能
- 適用情境:
- 領域專家級的應用
- 生產環境中的應用
- 需要自訂模型的企業應用
Fine-tuning 自己的大型語言模型(LLM)的好處如下
- 效能
- 避免語言模型亂編造資訊(stop hallucinations)
- 增加模型穩定一致的輸出(increase consistency)
- 減少不需要的資訊(reduce unwanted info)
- 隱私
- 在自有機房或雲端虛擬私有雲(on-prem or VPC)部屬,避免資料外洩
- 防止資料洩漏與遭到入侵(prevent leakage and breaches)
- 成本
- 降低每一次請求的成本(lower cost per request)
- 提高成本的透明度與控制力(increased transparency and contro!)
- 可靠度
- 控制服務的上線時間(control uptime)
- 降低延遲(lower latency)
- 進行調節與管控(moderation)
Lab範例
簡單demo 直接使用LLM模型 vs. finetune後的chat模型 vs. chtgpt
- 模型的使用封裝得相當簡單
- 讓 LLAMA2 模型回答「告訴我如何訓練狗狗坐下」等問題,結果並未正確回答
- 再試著讓 LLAMA2 回答其他問題,取得的結果都不太理想
- 接著展示已 Fine-tuned 過專注在對話的 LLAMA2Chat 模型
- 相同的問題透過 Fine-tuned 模型都得到更正確、符合預期的回答
- 顯示 Fine-tuned 模型在指定任務上的表現優於一般模型
補充資料
- 評估生成模型的困難性(Evaluating generative models is notoriously difficult)
- 生成模型的評估是出了名的困難
- 模型的性能隨著時間的推移不斷提高,而評估指標實際上很難跟上這種速度
- 人工專家評估是最可靠的
- 由於缺乏明確的評估指標,人工評估成為了最可靠的方式
- 需要有對領域有深入了解的專家來評估模型的輸出