【问题标题】:how to add tokens in vocab.txt which decoded as [UNK] bert tokenizer如何在解码为 [UNK] bert tokenizer 的 vocab.txt 中添加标记
【发布时间】:2021-07-25 03:23:24
【问题描述】:

我正在解码来自 bert tokenizer 的标记化标记,它为 € 符号提供 [UNK]。但我尝试在 vocab.txt 文件中添加 ##€ 标记。但它没有反映在预测结果中,与之前的结果相同,它再次给出[UNK]。请让我知道要解决此问题,我是否需要再次微调模型以反映预测的变化。到目前为止,我一直在避免微调,因为它需要 10 多个小时。 提前致谢

【问题讨论】:

标签: python nlp bert-language-model huggingface-transformers huggingface-tokenizers


【解决方案1】:

使用分词器的add_tokens函数来避免未知令牌:

from transformers import BertTokenizer
t = BertTokenizer.from_pretrained('bert-base-uncased')
print(t.tokenize("This is an example with an emoji ?."))
t.add_tokens(['?'])
print(t.tokenize("This is an example with an emoji ?."))

输出:

['this', 'is', 'an', 'example', 'with', 'an', 'em', '##oj', '##i', '[UNK]', '.']
['this', 'is', 'an', 'example', 'with', 'an', 'em', '##oj', '##i', '?', '.']

请记住,您还需要调整模型的大小,以便使用 resize_token_embeddings 将其引入新令牌:

model.resize_token_embeddings(len(t))

【讨论】:

猜你喜欢
  • 2021-12-12
  • 2021-02-16
  • 2020-05-28
  • 2021-11-26
  • 2020-08-10
  • 1970-01-01
  • 2020-10-08
  • 2020-02-22
  • 1970-01-01
相关资源
最近更新 更多