[英]Error when training AllenNLP adversarial bias mitigator using a pretrained masked language model
[英]How to test masked language model after training it?
我已經按照本教程使用 BERT 從 Hugging Face 進行蒙面語言建模,但我不確定如何實際部署 model。
教程: https://github.com/huggingface/notebooks/blob/master/examples/language_modeling.ipynb
我已經使用我自己的數據集訓練了 model,效果很好,但我不知道如何實際使用 model,因為遺憾的是,筆記本沒有包含如何執行此操作的示例。
在 Hugging Face 網站上,這是示例中使用的代碼; 因此,我想用我的 model 做這件事:
>>> from transformers import pipeline
>>> unmasker = pipeline('fill-mask', model='bert-base-uncased')
>>> unmasker("Hello I'm a [MASK] model.")
[{'sequence': "[CLS] hello i'm a fashion model. [SEP]",
'score': 0.1073106899857521,
'token': 4827,
'token_str': 'fashion'},
{'sequence': "[CLS] hello i'm a role model. [SEP]",
'score': 0.08774490654468536,
'token': 2535,
'token_str': 'role'},
{'sequence': "[CLS] hello i'm a new model. [SEP]",
'score': 0.05338378623127937,
'token': 2047,
'token_str': 'new'},
{'sequence': "[CLS] hello i'm a super model. [SEP]",
'score': 0.04667217284440994,
'token': 3565,
'token_str': 'super'},
{'sequence': "[CLS] hello i'm a fine model. [SEP]",
'score': 0.027095865458250046,
'token': 2986,
'token_str': 'fine'}
關於如何做到這一點的任何幫助都會很棒。
這很大程度上取決於您的任務。 您的任務似乎是蒙面語言建模,即預測一個或多個蒙面詞:
今天我吃了___。
(pizza) 或 (pasta) 可能同樣正確,因此您不能使用准確度等指標。 但是(水)應該比其他兩個更“正確”。 因此,您通常要做的是檢查語言 model 在評估數據集上的“驚訝”程度。 這個度量被稱為perplexity 。 因此,在您對特定數據集微調 model 之前和之后,您將計算困惑度,並且您會期望微調后它會更低。 model 應該更適合您的特定詞匯等。這就是您測試model 的方式。
如您所見,他們在您提到的教程中計算了困惑度:
import math
eval_results = trainer.evaluate()
print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}")
要預測樣本,您需要標記這些樣本並為 model 准備輸入。 Fill-mask-Pipeline 可以為您執行此操作:
# if you trained your model on gpu you need to add this line:
trainer.model.to('cpu')
unmasker = pipeline('fill-mask', model=trainer.model, tokenizer=tokenizer)
unmasker("today I ate <mask>")
這導致以下 output:
[{'score': 0.23618391156196594,
'sequence': 'today I ate it.',
'token': 24,
'token_str': ' it'},
{'score': 0.03940323367714882,
'sequence': 'today I ate breakfast.',
'token': 7080,
'token_str': ' breakfast'},
{'score': 0.033759087324142456,
'sequence': 'today I ate lunch.',
'token': 4592,
'token_str': ' lunch'},
{'score': 0.025962186977267265,
'sequence': 'today I ate pizza.',
'token': 9366,
'token_str': ' pizza'},
{'score': 0.01913984678685665,
'sequence': 'today I ate them.',
'token': 106,
'token_str': ' them'}]
與困惑密切相關,並且更具體到掩碼語言 model 評估: https://aclanthology.org/2020.acl-main.240.Z437175BA41913410EE094E1D9
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.