【问题标题】:Need to Fine Tune a BERT Model to Predict Missing Words需要微调 BERT 模型来预测缺失的单词
【发布时间】:2020-03-02 10:14:51
【问题描述】:

我知道 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 模型?谢谢。

【问题讨论】:

    标签: python nlp bert-language-model


    【解决方案1】:

    BERT 是一个掩码语言模型,这意味着它是针对此任务进行训练的。这就是为什么它可以做到。所以从这个意义上说,不需要微调。

    但是,如果您在运行时看到的文本与训练 BERT 的文本不同,那么如果您微调您希望看到的文本类型,您的表现可能会好得多。

    【讨论】:

    • 感谢您的澄清。一个问题,如果您不介意,用于训练模型的数据集是什么?谢谢。
    • 来自官方 BERT 存储库:Wikipedia、BookCorpus 和 Common Crawl。这是一个巨大的训练集
    猜你喜欢
    • 1970-01-01
    • 2021-01-16
    • 2021-02-16
    • 1970-01-01
    • 2021-07-22
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多