我正在解码来自bert tokenizer的标记化标记,它为 € 符号提供[UNK]。但我尝试在 vocab.txt 文件中添加 ##€ 标记。但它没有反映在预测结果中,与之前的结果相同,它再次给出[UNK]。请让我知道要解决这个问题,我是否需要再次微调模型以反映预测的变化。到目前为止,我一直在避免微调,因为它需要 10 多个小时。提前致谢
1 回答
3
使用标记器的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-05-12T22:05:09.683 回答