在执行自己的训练任务时,如何更新预训练的bert模型的词汇?

时间:2020-02-17 13:42:55

标签: machine-learning neural-network nlp bert-language-model

我现在正在使用BERT模型来预测掩盖词的任务。与其他答案不同,答案需要从特定选项中进行选择。

例如:

sentence: "In my daily [MASKED], ..."
options: A.word1 B.word2 C.word3 D.word4
the predict word will be chosen from four given words

我使用拥抱面孔的BertForMaskedLM来完成此任务。该模型将为我提供一个概率矩阵,该矩阵代表每个单词出现在[MASK]中的概率,而我只需要比较选项中单词的概率以选择答案即可。

# Predict all tokens
with torch.no_grad():
    predictions = model(tokens_tensor, segments_tensors)
#predicted_index = torch.argmax(predictions[0, masked_index]).item()
#predicted_token = tokenizer.convert_ids_to_tokens([predicted_index])[0]
A = predictions[0, masked_pos][tokenizer.convert_tokens_to_ids([option1])]
B = predictions[0, masked_pos][tokenizer.convert_tokens_to_ids([option2])]
C = predictions[0, masked_pos][tokenizer.convert_tokens_to_ids([option3])]
D = predictions[0, masked_pos][tokenizer.convert_tokens_to_ids([option4])]
#And then select from ABCD

但是问题是: 如果选项不在“ bert-vocabulary.txt”中,则上述方法将不起作用,因为输出矩阵未给出其可能性。如果选项不是单个单词,也会出现相同的问题。

我应该更新词汇表吗?或如何训练模型 在预训练的基础上添加新单词?

0 个答案:

没有答案
相关问题