需要微调 BERT 模型以预测缺失词
Need to Fine Tune a BERT Model to Predict Missing Words
我知道 BERT 具有预测句子中缺失单词的能力,它可以在句法上正确且在语义上连贯。下面是一个示例代码:
import torch
from pytorch_pretrained_bert import BertTokenizer, BertForMaskedLM
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
model.eval(); # turning off the dropout
def fill_the_gaps(text):
text = '[CLS] ' + text + ' [SEP]'
tokenized_text = tokenizer.tokenize(text)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
segments_ids = [0] * len(tokenized_text)
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])
with torch.no_grad():
predictions = model(tokens_tensor, segments_tensors)
results = []
for i, t in enumerate(tokenized_text):
if t == '[MASK]':
predicted_index = torch.argmax(predictions[0, i]).item()
predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])[0]
results.append(predicted_token)
return results
print(fill_the_gaps(text = 'I bought an [MASK] because its rainy .'))
print(fill_the_gaps(text = 'Im sad because you are [MASK] .'))
print(fill_the_gaps(text = 'Im worried because you are [MASK] .'))
print(fill_the_gaps(text = 'Im [MASK] because you are [MASK] .'))
有人可以向我解释一下,我是否需要微调 BERT 模型来预测缺失的单词,或者只使用预训练的 BERT 模型?谢谢
BERT 是一种掩码语言模型,这意味着它正是针对此任务进行训练的。这就是它能做到的原因。所以从这个意义上说,不需要微调。
但是,如果您在运行时看到的文本与训练 BERT 的文本不同,那么如果您微调您希望看到的文本类型,您的性能可能会好得多。
我知道 BERT 具有预测句子中缺失单词的能力,它可以在句法上正确且在语义上连贯。下面是一个示例代码:
import torch
from pytorch_pretrained_bert import BertTokenizer, BertForMaskedLM
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
model.eval(); # turning off the dropout
def fill_the_gaps(text):
text = '[CLS] ' + text + ' [SEP]'
tokenized_text = tokenizer.tokenize(text)
indexed_tokens = tokenizer.convert_tokens_to_ids(tokenized_text)
segments_ids = [0] * len(tokenized_text)
tokens_tensor = torch.tensor([indexed_tokens])
segments_tensors = torch.tensor([segments_ids])
with torch.no_grad():
predictions = model(tokens_tensor, segments_tensors)
results = []
for i, t in enumerate(tokenized_text):
if t == '[MASK]':
predicted_index = torch.argmax(predictions[0, i]).item()
predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])[0]
results.append(predicted_token)
return results
print(fill_the_gaps(text = 'I bought an [MASK] because its rainy .'))
print(fill_the_gaps(text = 'Im sad because you are [MASK] .'))
print(fill_the_gaps(text = 'Im worried because you are [MASK] .'))
print(fill_the_gaps(text = 'Im [MASK] because you are [MASK] .'))
有人可以向我解释一下,我是否需要微调 BERT 模型来预测缺失的单词,或者只使用预训练的 BERT 模型?谢谢
BERT 是一种掩码语言模型,这意味着它正是针对此任务进行训练的。这就是它能做到的原因。所以从这个意义上说,不需要微调。
但是,如果您在运行时看到的文本与训练 BERT 的文本不同,那么如果您微调您希望看到的文本类型,您的性能可能会好得多。