使用HuggingFace的RobertaTokenizer之前,我需要先对文本进行预标记吗? (不同的打底)

时间:2020-06-17 06:19:07

标签: huggingface-transformers huggingface-tokenizers

在Huggingface中使用Roberta令牌生成器时,我感到困惑。

>>> tokenizer = RobertaTokenizer.from_pretrained('roberta-base')
>>> x = tokenizer.tokenize("The tiger is ___ (big) than the dog.")
['The', 'Ġtiger', 'Ġis', 'Ġ___', 'Ġ(', 'big', ')', 'Ġthan', 'Ġthe', 'Ġdog', '.']
>>> x = tokenizer.tokenize("The tiger is ___ ( big ) than the dog.")
['The', 'Ġtiger', 'Ġis', 'Ġ___', 'Ġ(', 'Ġbig', 'Ġ)', 'Ġthan', 'Ġthe', 'Ġdog', '.']
>>> x = tokenizer.encode("The tiger is ___ (big) than the dog.")
[0, 20, 23921, 16, 2165, 36, 8527, 43, 87, 5, 2335, 4, 2]
>>> x = tokenizer.encode("The tiger is ___ ( big ) than the dog.")
[0, 20, 23921, 16, 2165, 36, 380, 4839, 87, 5, 2335, 4, 2]
>>>

问题(big)( big )的令牌化结果不同,因此令牌ID也不同。我应该使用哪一个?这是否意味着我应该先对输入进行预标记以使其成为( big )并进行RobertaTokenization?还是没关系?

第二,似乎BertTokenizer没有这种困惑:

>>> tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
>>> x = tokenizer.tokenize("The tiger is ___ (big) than the dog.")
['the', 'tiger', 'is', '_', '_', '_', '(', 'big', ')', 'than', 'the', 'dog', '.']
>>> x = tokenizer.tokenize("The tiger is ___ ( big ) than the dog.")
['the', 'tiger', 'is', '_', '_', '_', '(', 'big', ')', 'than', 'the', 'dog', '.']
>>>

BertTokenizer使用字词也给了我相同的结果。

有什么想法可以帮助我更好地了解RobertaTokenizer,我知道它正在使用字节对编码?

1 个答案:

答案 0 :(得分:0)

Hugingface的变形金刚经过精心设计,使您不应该进行任何预先标记。

RoBERTa使用具有无损预加令牌的SentecePiece。也就是说,当您拥有标记化的文本时,您应该始终能够说出标记化之前的文本外观。 Ġ(它是,在原始的SentecePiece中是一个奇怪的Unicode下划线),它表示在进行令牌解密时应该有一个空格。结果,big▁big最终成为不同的令牌。当然,在这种特定情况下,它没有多大意义,因为它显然仍然是同一个词,但这是您为无损标记化所付出的代价以及RoBERTa的培训方式。

BERT使用WordPiece,它不会遇到此问题。另一方面,原始字符串和标记化文本之间的映射不是那么简单(例如,当您要突出显示用户生成的文本中的某些内容时,可能会很不方便)。