偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

大模型的溫度?解讀Temperature

原創(chuàng) 精選
人工智能
LLM 中的溫度參數(shù)控制生成文本的隨機(jī)性。較低的值導(dǎo)致更具確定性和一致性的輸出,而較高的值增加多樣性,但可能降低一致性。

LLM 溫度是一個(gè)參數(shù),它控制著 LLM 預(yù)測(cè)的下一個(gè)單詞的概率分布。它通過改變下一個(gè)單詞被選中的可能性,為 LLM 的輸出增加了一些隨機(jī)性或多樣性。溫度可以影響 LLM 的輸出,使其更確定 (可預(yù)測(cè)) 或更隨機(jī) (隨機(jī)),這樣的參數(shù)被用來模擬或模仿人類語言產(chǎn)生的內(nèi)在變化。

1. LLM Temperature 的簡(jiǎn)要回顧

在生產(chǎn)環(huán)境中,較低的溫度值 (<1) 可以導(dǎo)致更確定或可預(yù)測(cè)的 LLM 輸出,稱為使 LLM 更 “可預(yù)測(cè)”。溫度為 1 默認(rèn)為在訓(xùn)練中學(xué)到的 LLM 固有的單詞分布,反映了Softmax未改變的輸出。較高的溫度值 (> 1) 可以導(dǎo)致更隨機(jī)或隨機(jī)和多變的 LLM 輸出,被稱為使 LLM 更 “創(chuàng)造性”。然而,“創(chuàng)造性” 這個(gè)詞可能用詞不當(dāng),因?yàn)楫a(chǎn)生更多不同的輸出并不一定等同于創(chuàng)造性。

當(dāng)溫度值設(shè)置為 0 時(shí),大多數(shù)系統(tǒng)會(huì)觸發(fā)對(duì)下一個(gè)單詞預(yù)測(cè)的貪婪采樣,這只是以詞匯表中概率最高的單詞進(jìn)行采樣。在某些情況下,如果溫度值太高 (> 2) 或太低 (= 0) ,這可能會(huì)引發(fā)退化行為,如 LLM 幻覺。LLM 中的幻覺指的是看似合理但事實(shí)上不正確的輸出,或者是不連貫和無意義的文本。這個(gè)術(shù)語在隱喻上類似于一個(gè)人如何感知那些并不真實(shí)存在的東西。

值得注意的是,溫度值在 0 到 2 之間并不能消除幻覺;相反,它們?cè)谳敵鲋幸肓穗S機(jī)性和多樣性,這可能會(huì)根據(jù)上下文增加或減少幻覺。為了減輕幻覺,可以采用檢索增強(qiáng)生成 (RAG)、思維鏈 (CoT) 等策略來提高 LLM 生成文本的準(zhǔn)確性和連貫性。

2. Softmax激活函數(shù) 與 LLM的溫度

Softmax激活函數(shù)是一種數(shù)學(xué)變換,將原始分?jǐn)?shù)的向量轉(zhuǎn)換為概率分布。為此,它對(duì)每個(gè)值進(jìn)行指數(shù)運(yùn)算,并對(duì)所有指數(shù)運(yùn)算值的和進(jìn)行規(guī)范化,以便它們的和等于 1。它最初應(yīng)用于 1868 年左右的物理學(xué)和統(tǒng)計(jì)學(xué),被稱為玻爾茲曼分布或吉布斯分布。術(shù)語 “softmax” 是由 John s. Bridle 在 1989 年創(chuàng)造的。

在自然語言處理 (NLP) 中,Softmax激活函數(shù)通常應(yīng)用于 LLM 生成的 logits,從而對(duì)可能的下一個(gè)令牌產(chǎn)生概率分布。該分布表示每個(gè)標(biāo)記成為序列中下一個(gè)字或子字的可能性。

圖片

溫度 (t) 參數(shù)是對(duì)調(diào)節(jié)輸入的Softmax激活函數(shù)的一個(gè)簡(jiǎn)單修改:

圖片

“溫度” 這個(gè)術(shù)語是從物理學(xué)領(lǐng)域借來的。它來源于它與波茲曼分布的關(guān)系,后者描述了能量狀態(tài)如何隨溫度變化。早在 1985 年,Ackley 及其同事就在機(jī)器學(xué)習(xí)中使用了術(shù)語 “溫度”。

2.1 無溫度的softmax 變換

例如,給定一個(gè)數(shù)字列表,計(jì)算它們的 softmax 概率。list=[2.0,4.0,3.0]

#Calculating Softmax
import torch
import torch.nn.functional as F

#1) Using Our Function
#Define a softmax function
def my_softmax(input_vector):
    e = np.exp(input_vector)
    return e / e.sum()

list_in = [2.0, 4.0, 3.0]
output = my_softmax(list_in)

print(f"\nThe softmax probabilities are: \n {output}")

#2) Using PyTorch Function
#Convert list to torch tensor
list_in_torch = torch.tensor(list_in)
output = F.softmax(list_in_torch, dim=0)

print(f"\nThe softmax probabilities (using Pytorch) are: \n {output}")

輸出結(jié)果如下:

The softmax probabilities are: 
 [0.09003057 0.66524096 0.24472847]

The softmax probabilities (using Pytorch) are: 
 tensor([0.0900, 0.6652, 0.2447])

2.2 隨溫度變化的 Softmax 變換

給定一個(gè)來自 LLM 的 logit 輸出列表,找到最可能的單詞及其概率。假設(shè) LLM 只知道 5 個(gè)單詞 (LLM 詞匯通常包含數(shù)千個(gè)單詞),計(jì)算溫度為 1.0 和 100.0 的概率。

index=[0,1,2,3,4]

words=[ceiling,floor,mat,car,grass]

logits=[?49.82,?46.40,?45.25,?47.30,?48.32]

Temperature: 1.0

python 代碼如下:

# Assume for simplicity:
# * The model only knows the 5 words listed below (it has a vocabulary of 5).

import pandas as pd
import seaborn as sns

#Example model output
model_output_vals = {"word_index":[i for i in range(5)],
                "words":["ceiling", "floor", "mat", "car", "grass"], 
                "logits":[-49.82, -46.40, -45.25, -47.30, -48.32]}
temp = 1.0
#Convert the data to a DataFrame
model_output = pd.DataFrame(model_output_vals)

#Define a softmax function with temperature
def my_softmax(input_vector, Temp=1.0):
    e = np.exp(np.divide(input_vector,Temp))
    return e / e.sum()

#Calculate the probabilities
probs =  my_softmax(model_output["logits"], Temp=temp)
model_output["softmax_prob"] = probs 

#Select the most probable word
most_prob = np.argmax(probs)

print(f"\nThe index of the most probable word is: {most_prob}")

#Pull out the most probable word
print(f"\nThe most probable word is: { model_output['words'][most_prob] }" \
      f" (Prob: {model_output['softmax_prob'][most_prob]:.5f})")


#Style our table
cm = sns.light_palette("orange", as_cmap=True)
s1 = model_output
s1 = s1.style.background_gradient(subset=["logits"],cmap=cm)

cm = sns.light_palette("green", as_cmap=True)
s1.background_gradient(subset=["softmax_prob"],cmap=cm)

輸出結(jié)果如下:

The index of the most probable word is: 2

The most probable word is: mat (Prob: 0.66571)

從 softmax 概率中我們看到最可能的單詞是: mat,概率約為0.666

如果 Temperature: 100.0,那么

# Example Softmax Calculation

# Assume for simplicity:
# * The model only knows the 5 words listed below (it has a vocabulary of 5).

import pandas as pd
import seaborn as sns

#Example model output
model_output_vals = {"word_index":[i for i in range(5)],
                "words":["ceiling", "floor", "mat", "car", "grass"], 
                "logits":[-49.82, -46.40, -45.25, -47.30, -48.32]}
temp = 100.0
#Convert the data to a DataFrame
model_output = pd.DataFrame(model_output_vals)

#Define a softmax function with temperature
def my_softmax(input_vector, Temp=1.0):
    e = np.exp(np.divide(input_vector,Temp))
    return e / e.sum()

#Calculate the probabilities
probs =  my_softmax(model_output["logits"], Temp=temp)
model_output["softmax_prob"] = probs 

#Select the most probable word
most_prob = np.argmax(probs)

print(f"\nThe index of the most probable word is: {most_prob}")

#Pull out the most probable word
print(f"\nThe most probable word is: { model_output['words'][most_prob] }" \
      f" (Prob: {model_output['softmax_prob'][most_prob]:.5f})")


#Style our table
cm = sns.light_palette("orange", as_cmap=True)
s1 = model_output
s1 = s1.style.background_gradient(subset=["logits"],cmap=cm)

cm = sns.light_palette("green", as_cmap=True)
s1.background_gradient(subset=["softmax_prob"],cmap=cm)

輸出結(jié)果為:

The index of the most probable word is: 2

The most probable word is: mat (Prob: 0.20436)

從 softmax 概率中,我們看到最可能的單詞是: mat,概率為: 0.204

隨著溫度從 1.0 升高到 100.0,概率分布從更加集中 (或 “尖峰”) 轉(zhuǎn)變?yōu)楦臃稚?(或 “平坦”) ,這意味著在較低溫度下概率較低的單詞被選中的幾率更高。使用貪婪抽樣,總是選擇概率最高的單詞,模型一致地選擇排名最高的單詞。

3. LLM 的Temperature 應(yīng)用

考察溫度參數(shù)如何影響大模型的輸出,我們將使用GPT-2, 這個(gè)由 OpenAI 開發(fā)的開源文本生成模型,可以通過Hugging Face 獲得。GPT-2 具有以下特點(diǎn):

  • 1.24 億個(gè)參數(shù): 這些是模型中可學(xué)習(xí)的權(quán)重,幫助模型根據(jù)輸入數(shù)據(jù)進(jìn)行預(yù)測(cè)。
  • 50,257 詞匯量: 模型的詞匯量由一組符號(hào) (使用字節(jié)對(duì)編碼的單詞或子詞) 組成,gpt-2 被訓(xùn)練來識(shí)別和生成這些符號(hào)。
  • 768 維向量嵌入大?。?這是指用于編碼每個(gè)令牌的稠密向量表示的大小。
  • 12 個(gè)注意力頭: 這些是用于每個(gè)Transformer層的并行注意力機(jī)制,用于捕捉輸入序列關(guān)系的不同方面。
  • 12 層: 該模型有 12 個(gè)轉(zhuǎn)換層,允許它處理和理解數(shù)據(jù)中更復(fù)雜的模式。

我們將探索如何將 LLM 用于兩種類型的任務(wù):

單個(gè)的下一個(gè)單詞生成: 根據(jù)給定輸入的上下文預(yù)測(cè)下一個(gè)單詞。連續(xù)的下一個(gè)單詞生成: 生成一個(gè)單詞序列,根據(jù)先前生成的單詞預(yù)測(cè)每個(gè)新單詞。

3.1 模型構(gòu)建

from transformers import AutoModelForCausaLLM, AutoTokenizer

model_to_load = "openai-community/gpt2"
model_to_load_task = "text-generation"

# Load the model's pretrained tokenizer
tokenizer = AutoTokenizer.from_pretrained(model_to_load)

# Load the pretrained model
model = AutoModelForCausaLLM.from_pretrained(
    model_to_load,
    device_map = device, #CPU or GPU
    torch_dtype = "auto",
    trust_remote_code = True
)

To pass inputs to the model we can run the following:

# Input sentence
prompt = "The cat sat on the"

temperature = 0.5

# Tokenize/encode input prompt
input_ids = tokenizer.encode(prompt, return_tensors="pt")

# Generate the output with adjusted temperature
outputs = model.generate(input_ids,
                        max_new_tokens=1, #Just want one word generated
                        temperature=temperature, #Set temp
                        output_scores=True, #Output model word scores
                        output_logits=True, #Outout logits
                        return_dict_in_generate=True,
                        do_sample=True, #Perform sampling for next word
                        pad_token_id=tokenizer.eos_token_id)


# Get the generated token ID/next word
generated_token_id = outputs.sequences[0][-1].item()    


# Decode the generated token ID to a word
generated_word = tokenizer.decode([generated_token_id])

3.2 單個(gè)的下一個(gè)單詞的生成

在單個(gè)的下一個(gè)詞生成中,gpt-2 被給定一個(gè)初始輸入序列 (例如一個(gè)部分句子) 并預(yù)測(cè)最有可能的下一個(gè)詞。該模型根據(jù)序列中前面的單詞提供的上下文進(jìn)行預(yù)測(cè)。一旦下一個(gè)單詞被預(yù)測(cè),它就會(huì)被輸出,這個(gè)過程就停止了,意思是一次只生成一個(gè)單詞. 根據(jù)模型的學(xué)習(xí)關(guān)聯(lián),基于最高概率選擇單詞,并且除非使用新的輸入重復(fù)該過程,否則不會(huì)發(fā)生進(jìn)一步的預(yù)測(cè)。

輸入: The cat slept on the ______.

prompt = "The cat slept on the"
temps = [0.1,  0.5, 1., 5., 10., 100.]
for ii in temps:
  word_out = next_word_prediction(prompt, temp=ii)

  print(f"LLM Temperature: {ii} \n {prompt} {word_out}")

這里我們將相同的輸入句子以不同的溫度值傳遞給 LLM,然后查看模型詞匯表中選擇單詞的概率分布。

LLM Temperature: 0.1Input : The cat slept on theOutput: The cat slept on the floor

LLM Temperature: 0.5Input : The cat slept on theOutput: The cat slept on the bed

LLM Temperature: 1.0Input : The cat slept on theOutput: The cat slept on the back

LLM Temperature: 5.0Input : The cat slept on theOutput: The cat slept on the bathroom

LLM Temperature: 10.0Input : The cat slept on theOutput: The cat slept on the corner

LLM Temperature: 100.0Input : The cat slept on theOutput: The cat slept on the inside

隨著溫度從 0.1 到 100.0 升高,概率分布從更加集中 (或 “尖峰”) 變得更加分散 (或 “平坦”) ,這意味著在較低溫度下出現(xiàn)概率較低的單詞被選中的幾率更高。

3.3 連續(xù)的下一個(gè)單詞的生成

在連續(xù)的下一個(gè)單詞生成中,gpt-2 給出一個(gè)初始輸入句子,并以自回歸的方式預(yù)測(cè)下一個(gè)最可能的單詞。該模型使用它建立的上下文,根據(jù)它已經(jīng)預(yù)測(cè)的前一個(gè)單詞生成每個(gè)單詞。在預(yù)測(cè)下一個(gè)單詞之后,它被添加到句子中,更新后的序列被傳遞回模型以進(jìn)行下一次迭代。這個(gè)過程一直持續(xù)到滿足以下兩個(gè)條件之一: 模型生成序列結(jié)束標(biāo)記 (例如 < eos> 或 \n) ,或者達(dá)到最大迭代次數(shù) (或標(biāo)記)。

我們將向 LLM 傳遞上述相同的句子,以查看它在如下所示的若干次迭代中將輸出什么。

Input sentence: The cat slept on the ______

1: The cat slept on the floor ______

2: The cat slept on the floor next ______

3: The cat slept on the floor next to ______

4: The cat slept on the floor next to the ______

5: The cat slept on the floor next to the window ______

6: The cat slept on the floor next to the window . ______

7: The cat slept on the floor next to the window . < EOS >

我們將把提示詞傳遞給 LLM,并將其預(yù)測(cè)的輸出 (word _ out) 附加到提示詞后面,然后繼續(xù)迭代,直到達(dá)到最大迭代次數(shù) (max _ gen _ iteration) 或者預(yù)測(cè)句子結(jié)束標(biāo)記 ( 或 \n)。

prompt = "The cat slept on the"
temp = 0.5
max_gen_iteration = 20
for ii in range(max_gen_iteration):
  word_out, probs_out = next_word_prediction(prompt, temp=temp)
  print(prompt + word_out)
  prompt += word_out

這里我們將相同的輸入句子以不同的溫度值傳遞給 LLM,然后查看模型詞匯表中選擇單詞的概率分布。

Temp: 10.0Parameters:

Input text: “The cat slept on the”Temperature: 10.0Max iterations: 20

prompt = "The cat slept on the"
temp = 10.0
max_iter = 20

gen_next_word_loop(prompt, temp = temp, max_iter = max_iter)

當(dāng)比較 0.5 和 10.0 兩種溫度下的輸出時(shí),我們觀察到在 0.5 的溫度下生成的文本更連貫,而在 10.0 的溫度下,輸出變得越來越不連貫,人類讀者越來越難以理解。

這突出了溫度參數(shù)是如何通過改變模型詞匯表中可能的下一個(gè)單詞的概率分布來影響連續(xù)生成單詞的。

4. 小結(jié)

LLM 中的溫度參數(shù)控制生成文本的隨機(jī)性。較低的值導(dǎo)致更具確定性和一致性的輸出,而較高的值增加多樣性,但可能降低一致性。除了基本應(yīng)用之外,業(yè)界也在探索基于輸入上下文的動(dòng)態(tài)溫度調(diào)節(jié),針對(duì)多任務(wù)學(xué)習(xí)等特定任務(wù)進(jìn)行優(yōu)化,控制連貫性和文本長(zhǎng)度,以及影響情緒的基調(diào)。

隨著技術(shù)的發(fā)展,可以期待看到增強(qiáng)的模型靈活性,允許跨不同應(yīng)用程序的更上下文敏感、自適應(yīng)和創(chuàng)造性的輸出。

責(zé)任編輯:武曉燕 來源: 51CTO技術(shù)棧
相關(guān)推薦

2023-06-07 08:22:59

LLM微調(diào)技術(shù)

2023-10-06 20:30:33

大模型LLMtoken

2024-02-05 14:12:37

大模型RAG架構(gòu)

2024-05-06 07:58:23

MoE模型系統(tǒng)

2024-07-08 09:49:54

2025-04-27 02:22:00

MCP大模型Agent

2024-02-27 14:55:00

AI數(shù)據(jù)

2022-12-29 16:58:31

ChatGPT模型

2024-11-26 07:53:07

2024-12-02 01:18:54

2024-06-03 10:43:34

2021-05-18 07:33:20

模型分層

2023-05-08 23:15:26

2022-08-10 06:56:46

通信電源通信設(shè)備蓄電池

2023-05-10 14:40:40

AI模型算力

2023-11-03 07:47:12

機(jī)器資源大模型:

2024-11-27 13:08:34

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)