温度如何影响 LLMs 中的下一个标记预测?

原文:towardsdatascience.com/how-does-temperature-impact-next-token-prediction-in-llms-779bd908f2cf?source=collection_archive---------1-----------------------#2024-05-06

https://ankur-m.medium.com/?source=post_page---byline--779bd908f2cf--------------------------------https://towardsdatascience.com/?source=post_page---byline--779bd908f2cf-------------------------------- Ankur Manikandan

·发布于Towards Data Science ·阅读时间 4 分钟·2024 年 5 月 6 日

简而言之

1. 在温度为 1 时,概率值与标准 softmax 函数得出的概率值相同。

2. 提高温度会增加较不可能的标记的概率,从而扩展模型预测下一个标记的潜在候选范围(或多样性)。

3. 降低温度则会使最可能标记的概率接近 1.0,从而增强模型的信心。减少温度有效地消除了模型中的不确定性。

Google Colab 笔记本.

介绍

大型语言模型(LLMs)是多功能的生成模型,适用于广泛的任务。它们可以生成一致、可重复的输出,也可以通过将不太可能的单词组合在一起生成创造性内容。温度设置允许用户微调模型的输出,控制预测的可预见性程度。

让我们通过一个假设的例子来理解温度对下一个标记预测的影响。

我们让一个大型语言模型(LLM)完成句子**“这是一个美妙的 _____。”** 假设潜在的候选标记是:

|   token    | logit |
|------------|-------|
| day        |    40 |
| space      |     4 |
| furniture  |     2 |
| experience |    35 |
| problem    |    25 |
| challenge  |    15 |

对数值通过 softmax 函数处理,使得值的总和等于 1。实际上,softmax 函数为每个标记生成概率估计。

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/d581b23a99b01e18aef8ec99d1dabb2b.png

标准 softmax 函数

让我们在 Python 中计算概率估计值。

import numpy as np
import seaborn as sns
import pandas as pd
import matplotlib.pyplot as plt
from ipywidgets import interactive, FloatSlider

def softmax(logits):
    exps = np.exp(logits)
    return exps / np.sum(exps)

data = {
    "tokens": ["day", "space", "furniture", "experience", "problem", "challenge"],
    "logits": [5, 2.2, 2.0, 4.5, 3.0, 2.7]
}
df = pd.DataFrame(data)
df['probabilities'] = softmax(df['logits'].values)
df
| No. |   tokens   | logits | probabilities |
|-----|------------|--------|---------------|
|   0 | day        |    5.0 |      0.512106 |
|   1 | space      |    2.2 |      0.031141 |
|   2 | furniture  |    2.0 |      0.025496 |
|   3 | experience |    4.5 |      0.310608 |
|   4 | problem    |    3.0 |      0.069306 |
|   5 | challenge  |    2.7 |      0.051343 |
ax = sns.barplot(x="tokens", y="probabilities", data=df)
ax.set_title('Softmax Probability Estimates')
ax.set_ylabel('Probability')
ax.set_xlabel('Tokens')
plt.xticks(rotation=45)
for bar in ax.patches:
    ax.text(bar.get_x() + bar.get_width() / 2, bar.get_height(), f'{bar.get_height():.2f}',
            ha='center', va='bottom', fontsize=10, rotation=0)
plt.show()

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/ed3c4b8677cc0f1065ceb18f6e73ccc1.png

带温度的 softmax 函数定义如下:

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/0805fb3b6ec66ae8072b0ecacbd3fdd0.png

其中 (T) 是温度,(x_i) 是输入向量 (logits) 的第 (i) 个分量,(n) 是向量中分量的数量。

def softmax_with_temperature(logits, temperature):
    if temperature <= 0:
        temperature = 1e-10  # Prevent division by zero or negative temperatures
    scaled_logits = logits / temperature
    exps = np.exp(scaled_logits - np.max(scaled_logits))  # Numerical stability improvement
    return exps / np.sum(exps)

def plot_interactive_softmax(temperature):
    probabilities = softmax_with_temperature(df['logits'], temperature)
    plt.figure(figsize=(10, 5))
    bars = plt.bar(df['tokens'], probabilities, color='blue')
    plt.ylim(0, 1)
    plt.title(f'Softmax Probabilities at Temperature = {temperature:.2f}')
    plt.ylabel('Probability')
    plt.xlabel('Tokens')
    # Add text annotations
    for bar, probability in zip(bars, probabilities):
        yval = bar.get_height()
        plt.text(bar.get_x() + bar.get_width()/2, yval, f"{probability:.2f}", ha='center', va='bottom', fontsize=10)
    plt.show()

interactive_plot = interactive(plot_interactive_softmax, temperature=FloatSlider(value=1, min=0, max=2, step=0.01, description='Temperature'))
interactive_plot

当 T = 1 时,

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/4250d9a5ebcd02c95d879ccd132cc5a3.png

在温度为 1 时,概率值与标准 softmax 函数推导出的概率值相同。

当 T > 1 时,

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/28a0ff2d89cddc0ce05b88819422a3b3.png

提高温度会膨胀不太可能出现的标记的概率,从而扩大模型下一个标记预测的潜在候选范围(或多样性)。

当 T < 1 时,

https://github.com/OpenDocCN/towardsdatascience-blog-zh-2024/raw/master/docs/img/0803db21504f8d41d771b1ed63ea7350.png

降低温度则会使最可能的标记的概率接近 1.0,从而提高模型的信心。降低温度有效地消除了模型中的不确定性。

结论

大型语言模型(LLMs)利用温度参数为其预测提供灵活性。模型在温度为 1 时表现得可预测,紧跟原始的 softmax 分布。提高温度会引入更多的多样性,放大不太可能的标记。相反,降低温度则使预测更加集中,通过减少不确定性来增强模型对最可能标记的信心。这种适应性使得用户可以根据不同任务调整大型语言模型的输出,在创意探索和确定性输出之间找到平衡。

除非另有说明,所有图片均为作者提供。

利用大语言模型(LLMs)开发一个功能齐全的命令行工具辅助程序,可以按照以下步骤进行: ### 1. 确定需求和功能 首先,明确你希望命令行工具具备哪些功能。例如: - 代码生成:根据用户输入的需求生成代码片段。 - 代码补全:在用户输入代码时提供补全建议。 - 文档生成:根据代码或注释生成文档。 - 自然语言查询:允许用户用自然语言查询代码或文档。 ### 2. 选择合适的大语言模型 选择一个适合你需求的大语言模型。例如: - GPT-3/GPT-4:功能强大,适合生成复杂文本。 - BERT:适合理解和生成自然语言查询。 - 其他开源模型:如GPT-Neo、Bloom等。 ### 3. 设置开发环境 配置好开发环境,包括安装必要的库和工具。例如,使用Python可以安装`transformers`库来调用大语言模型: ```bash pip install transformers ``` ### 4. 设计与实现 设计命令行工具的架构,并实现各个功能模块。 #### 示例代码: ```python from transformers import pipeline # 初始化大语言模型 generator = pipeline('text-generation', model='gpt2') def generate_code(prompt): response = generator(prompt, max_length=50, num_return_sequences=1) return response[0]['generated_text'] def main(): print("欢迎使用命令行工具辅助程序!") while True: user_input = input("请输入你的查询或输入 'exit' 退出: ") if user_input.lower() == 'exit': break code = generate_code(user_input) print("生成代码:") print(code) if __name__ == "__main__": main() ``` ### 5. 测试与优化 在开发过程中不断测试各个功能模块,确保其稳定性和准确性。根据用户反馈进行优化和调整。 ### 6. 部署与发布 将开发好的命令行工具部署到目标环境中,并发布给用户使用。 ### 7. 持续维护 定期更新和维护工具,修复潜在的问题,并根据需求添加新功能。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值