LongformerTokennizer的小坑点

事情是这样的,因为项目的需求,我需要用到Longformer来做一个MLM任务,结果出来的预测结果,很多单词都被分成几块。后来去翻了翻官方介绍,发现LongformerTokennizer的小坑点,官方原文是这样的:

This tokenizer has been trained to treat spaces like parts of the tokens (a bit like sentencepiece) so a word will be encoded differently whether it is at the beginning of the sentence (without space) or not

LongformerTokennizer的小坑点_第1张图片

换言之,由于我没有注意到这一点,导致训练数据和标签都是按照第一种情况:

from transformers import LongformerTokenizer


tokenizer = LongformerTokenizer.from_pretrained("allenai/longformer-base-4096")

print(tokenizer.encode("Leonard", add_special_tokens=False))

print(tokenizer.encode(" Leonard", add_special_tokens=False))
print(tokenizer.decode([34792]))

 

然后根据trash in trash out原则,我在提取mask位置的解码结果时,就会出现类似于leonard被切成两半的情况。

解决办法一般有两种,一个是在每一个字符串添加空格,或者设置参数 add_prefix_space=True。我最终是采用了第一种,得到了还不错的结果。

你可能感兴趣的:(机器学习)