python - 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?
解决方案
这只是底层模型的一个特性(请参阅此处以检查是否为distilroberta-base
)。
具体来说,蒸馏模型使用与其“教师模型”(在本例中为 RoBERTa)相同的分词器。反过来,RoBERTa 有一个严格工作的标记器,没有任何形式的空格,另请参阅OpenAI 的 GPT-2 模型上的这个线程,它使用相同的标记化策略(参见此处)。
具体来说,你可以注意到它总是相同的 unicode 字符\u0120
来表示一个新单词的开始。相比之下,由多个子词组成的词对于后面的子词将没有这样的起始字符。
即,complication
将被分成两个虚构的子词Ġcompli
cation
。
Ġ
因此,如果它出现在单词中,您可以简单地删除它。
推荐阅读
- python-3.x - Dual Task 中的同时响应收集
- python-3.x - 在快速启动之后,Snips-nlu 意图返回空意图 json
- javascript - 有没有办法访问 JavaScript 中尚未定义的变量?
- javascript - Semmle CodeQL CLI:“QL 数据集与 QL 不兼容”
- java - Swagger2:无法自省类 [springfox.documentation.swagger2.configuration.Swagger2DocumentationConfiguration]
- c# - 将全名拆分为名字、姓氏和头衔
- javascript - 按计数显示结果列表
- c# - 如何在数组中选择多个值?
- c# - C# 中的“as”关键字是否仅将 Object 类型变量转换为我想要的类型?
- vb.net - 如何在不悬停鼠标的情况下在用户窗体上显示按钮控件,如弹出模式中的按钮