调整 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(前 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(前 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(最大词元数)

  • 用于限制响应长度
  • 响应中的 token(词元)计数

  • 较低 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

Vamos praticar!

使用 Llama 3

Preparing Video For Download...