lmppl
1.0.0
困惑度衡量文字透過語言模型 (LM) 的可預測程度,通常用於評估文本的流暢性或原型性(困惑度越低,文字越流暢或原型化)。 LM-PPL 是一個 Python 函式庫,用於使用任何類型的預訓練 LM 計算文字的困惑度。我們計算循環LM 的普通困惑度,例如GPT3 (Brown et al., 2020) 和編碼器-解碼器LM 的解碼器困惑度,例如BART (Lewis et al., 2020) 或T5 (Raffel et al., 2020) ),同時我們計算 masked LM 的偽困惑度(Wang 和 Cho,2018)。
透過 pip 安裝。
pip install lmppl
我們以困惑度來解決情緒分析為例!請記住,困惑度較低的文字更好,因此我們比較兩個文字(正面和負面)並選擇困惑度較低的文字作為模型預測。
import lmppl
scorer = lmppl . LM ( 'gpt2' )
text = [
'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.' ,
'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.'
]
ppl = scorer . get_perplexity ( text )
print ( list ( zip ( text , ppl )))
>> > [
( 'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.' , 136.64255272925908 ),
( 'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.' , 139.2400838400971 )
]
print ( f"prediction: { text [ ppl . index ( min ( ppl ))] } " )
>> > "prediction: sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy."
import lmppl
scorer = lmppl . MaskedLM ( 'microsoft/deberta-v3-small' )
text = [
'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.' ,
'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.'
]
ppl = scorer . get_perplexity ( text )
print ( list ( zip ( text , ppl )))
>> > [
( 'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am happy.' , 1190212.1699246117 ),
( 'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad.' , 1152767.482071837 )
]
print ( f"prediction: { text [ ppl . index ( min ( ppl ))] } " )
>> > "prediction: sentiment classification: I dropped my laptop on my knee, and someone stole my coffee. I am sad."
import lmppl
scorer = lmppl . EncoderDecoderLM ( 'google/flan-t5-small' )
inputs = [
'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee.' ,
'sentiment classification: I dropped my laptop on my knee, and someone stole my coffee.'
]
outputs = [
'I am happy.' ,
'I am sad.'
]
ppl = scorer . get_perplexity ( input_texts = inputs , output_texts = outputs )
print ( list ( zip ( outputs , ppl )))
>> > [
( 'I am happy.' , 4138.748977714201 ),
( 'I am sad.' , 2991.629250051472 )
]
print ( f"prediction: { outputs [ ppl . index ( min ( ppl ))] } " )
>> > "prediction: I am sad."
以下是一些流行模型的範例以及在 lmppl 套件中使用的對應模型類型。
模型 | 擁抱臉部辨識碼 | 型號類型 |
---|---|---|
伯特 | google-bert/bert-base-uncased | 蒙面LM |
羅伯塔 | 羅伯塔大 | 蒙面LM |
通用技術2 | gpt2-xl | LM |
水果餡餅-ul2 | Google/flan-ul2 | 編碼器解碼器LM |
GPT-NeoX | EleutherAI/gpt-neox-20b | LM |
選擇 | 臉書/opt-30b | LM |
混合 | 米斯特拉萊/Mixtral-8x22B-v0.1 | LM |
駱駝3 | 元駱駝/元駱駝-3-8B | LM |
最大令牌長度:每個 LM 都有自己的最大令牌長度(用於循環/屏蔽 LM 的max_length
,以及用於編碼器-解碼器 LM 的max_length_encoder
和max_length_decoder
)。限制這些最大令牌將減少處理文字的時間,但可能會影響困惑度的準確性,因此請對您的文字進行實驗並確定最佳令牌長度。
批次大小:可以將批次大小傳遞給函數get_perplexity
(例如get_perplexity(text, batch_size=32)
)。預設情況下,它會將所有文字處理一次,如果文字數量過多,可能會導致記憶體錯誤。