調整 Llama 3 的參數

使用 Llama 3

Imtihan Ahmed

Machine Learning Engineer

參數有什麼用?

from llama_cpp import Llama
llm = Llama(model_path="path/to/model.gguf")
output = llm("What are some ways to improve customer retention?")

控制品質、隨機性與長度

使用 Llama 3

參數有什麼用?

  • 範例:產生產品描述

professional_site.png

→ 需具體且精簡

 

生活風格導向網站

 

→ 需有吸引力且具創意

使用 Llama 3

Llama 3 的解碼參數

$$

  • 調整 Llama 的行為

$$

  • 解碼參數 對應不同語氣

$$

  • 將原始輸出轉為可讀文本

用參數調整 Llama 行為

使用 Llama 3

Llama 3 的解碼參數

$$

  • Temperature:控制隨機性
  • Top-K:僅從最高機率的候選中取樣
  • Top-P:依累積機率調整取樣範圍
  • Max tokens:限制回應長度

$$

Screenshot 2025-02-26 at 12.51.27.png

使用 Llama 3

Temperature(溫度)

  • 值通常介於 0 到 1

  • 低溫度(如接近 0):

    • 回應更可預測
      A smartwatch with a heart rate monitor, GPS, and a long-lasting battery 
      for all-day tracking.
      
  • 高溫度(如接近 1):
    • 回應更有創意
      Your personal fitness coach on your wrist - track every heartbeat, every step, 
      and every adventure without limits.
      
使用 Llama 3

Top-k

  • 限制 Llama 可選的最高機率詞數量

  • 低 k 值(如 1):

    • 回應更可預測
      Track fitness, stream music, and receive notifications with our sleek smartwatch.
      
  • 高 k 值(如 50):

    • 回應更多樣
      Experience the future with our cutting-edge smartwatch, featuring fitness tracking, music streaming, customizable notifications, personalized insights, and seamless smartphone integration always.
      
使用 Llama 3

Top-p

  • 依信心(機率)控制輸出詞的選擇

  • 高 top-p 值(如接近 1):

    • 回應更豐富多變
      Stay connected with our sleek smartwatch, featuring fitness tracking,
      music, and customizable notifications, perfect for fitness 
      enthusiasts and busy professionals.
      
  • 低 top-p 值(如接近 0):

    • 變化較少
      Smartwatch with fitness tracking and music control, perfect for workouts.
      
使用 Llama 3

Max tokens(最大 tokens)

  • 用來限制回應長度
  • 回應中的 tokens 計數(詞的基本單位)

  • 較低的 max_tokens 值:

    Stay connected with our sleek smartwatch, featuring fitness tracking 
    and music control.
    
  • 較高的 max_tokens 值:
    Stay connected with our sleek smartwatch, featuring fitness tracking, 
    music control, customizable notifications, and seamless smartphone 
    integration. Monitor your health, track your progress, and receive 
    alerts on your wrist. Perfect for fitness enthusiasts.
    
使用 Llama 3

組合不同參數

llm = Llama(model_path="path/to/model.gguf")

output_concise = llm(
    "Describe an electric car.",

temperature=0.2,
top_k=1,
top_p=0.4,
max_tokens=20
)
A fast, eco-friendly electric car with a long range and cutting-edge technology.
使用 Llama 3

組合不同參數

output_creative = llm(
    "Describe an electric car.",

temperature=0.8,
top_k=10,
top_p=0.9,
max_tokens=100
)
Glide into the future with an electric car that blends speed, luxury,
and sustainability. Silent yet powerful, it redefines the road ...
使用 Llama 3

一起來練習吧!

使用 Llama 3

Preparing Video For Download...