训练后如何测试掩码语言模型?
How to test masked language model after training it?
我已按照本教程使用 BERT 从 Hugging Face 进行掩码语言建模,但我不确定如何实际部署该模型。
教程:https://github.com/huggingface/notebooks/blob/master/examples/language_modeling.ipynb
我已经使用自己的数据集训练了模型,效果很好,但我不知道如何实际使用该模型,因为 notebook 没有包含如何执行此操作的示例,遗憾的是。
Example of what I want to do with my trained model
在Hugging Face网站上,这是示例中使用的代码;因此,我想用我的模型做这件事:
>>> 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) 可能同样正确,因此您不能使用诸如 accuray 之类的指标。但是(水)应该比其他两个更不“正确”。
所以你通常做的是检查语言模型在评估数据集上的“惊讶”程度。此指标称为 perplexity。
因此,在针对特定数据集对模型进行微调之前和之后,您将计算困惑度,并且您希望它在微调后更低。该模型应该更适合您的特定词汇等。这就是您测试您的模型的方式。
如您所见,他们在您提到的教程中计算了困惑度:
import math
eval_results = trainer.evaluate()
print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}")
要预测 个样本,您需要标记这些样本并为模型准备输入。 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>")
这导致以下输出:
[{'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'}]
我已按照本教程使用 BERT 从 Hugging Face 进行掩码语言建模,但我不确定如何实际部署该模型。
教程:https://github.com/huggingface/notebooks/blob/master/examples/language_modeling.ipynb
我已经使用自己的数据集训练了模型,效果很好,但我不知道如何实际使用该模型,因为 notebook 没有包含如何执行此操作的示例,遗憾的是。
Example of what I want to do with my trained model
在Hugging Face网站上,这是示例中使用的代码;因此,我想用我的模型做这件事:
>>> 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) 可能同样正确,因此您不能使用诸如 accuray 之类的指标。但是(水)应该比其他两个更不“正确”。 所以你通常做的是检查语言模型在评估数据集上的“惊讶”程度。此指标称为 perplexity。 因此,在针对特定数据集对模型进行微调之前和之后,您将计算困惑度,并且您希望它在微调后更低。该模型应该更适合您的特定词汇等。这就是您测试您的模型的方式。
如您所见,他们在您提到的教程中计算了困惑度:
import math
eval_results = trainer.evaluate()
print(f"Perplexity: {math.exp(eval_results['eval_loss']):.2f}")
要预测 个样本,您需要标记这些样本并为模型准备输入。 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>")
这导致以下输出:
[{'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'}]