首页 > 解决方案 > Identifying the word picked by hugging face pipeline fill-mask

问题描述

I want to use hugging face's fill-mask pipeline to guess a masked token and then extract just the guessed token as a word. This code should do that:

!pip install -q transformers
model = pipeline('fill-mask')
outcome = model("Kubernetes is a container orchestration <mask>")[0]

#Prints: "Kubernetes is a container orchestration platform" 
print(outcome['sequence']) 

token = outcome['token'] 

#Prints: 1761
print(token)

#Prints: Ġplatform 
print(model.tokenizer.convert_ids_to_tokens(token))

But I am finding that it gives me back "Ġplatform" instead of "platform" - does anyone know why this is or what can be going on here?

标签: pythonneural-networknlphuggingface-transformers

解决方案


这只是底层模型的一个特性(请参阅此处以检查是否为distilroberta-base)。
具体来说,蒸馏模型使用与其“教师模型”(在本例中为 RoBERTa)相同的分词器。反过来,RoBERTa 有一个严格工作的标记器,没有任何形式的空格,另请参阅OpenAI 的 GPT-2 模型上的这个线程,它使用相同的标记化策略(参见此处)。

具体来说,你可以注意到它总是相同的 unicode 字符\u0120来表示一个新单词的开始。相比之下,由多个子词组成的词对于后面的子词将没有这样的起始字符。

即,complication将被分成两个虚构的子词Ġcompli cation

Ġ因此,如果它出现在单词中,您可以简单地删除它。


推荐阅读