为什么需要位置编码,而输入的id已经表示了Bert中单词的顺序?

huangapple go评论52阅读模式
英文:

Why is positional encoding needed while input ids already represent the order of words in Bert?

问题

例如,在Huggingface的示例中:

encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.")
print(encoded_input)
{'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102],
'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

input_ids向量已经编码了原始句子中每个标记的顺序。为什么还需要再次进行位置编码,并使用额外的向量来表示它呢?

英文:

For example, in Huggingface's example:

encoded_input = tokenizer("Do not meddle in the affairs of wizards, for they are subtle and quick to anger.")
print(encoded_input)
{'input_ids': [101, 2079, 2025, 19960, 10362, 1999, 1996, 3821, 1997, 16657, 1010, 2005, 2027, 2024, 11259, 1998, 4248, 2000, 4963, 1012, 102], 
 'token_type_ids': [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0], 
 'attention_mask': [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]}

The input_ids vector already encode the order of each token in the original sentence. Why does it need positional encoding again with an extra vector to represent it?

答案1

得分: 0

原因在于神经架构的设计。BERT由自注意力和前馈子层组成,它们都不是顺序的。

前馈层独立处理每个标记,与其他标记无关。

自注意力将输入状态视为无序的状态集合。注意力可以解释为根据某些键对一组值进行软概率检索。位置嵌入存在是为了使键包含有关它们相对顺序的信息。

英文:

The reason is the design of the neural architecture. BERT consists of self-attention and feedforward sub-layers, and neither of them is sequential.

The feedforward layers process each token independently of others.

The self-attention views the input states as an unordered set of states. Attention can be interpreted as soft probabilistic retrieval from a set of values according to some keys. The position embeddings are there so the keys can contain information about their relative order.

huangapple
  • 本文由 发表于 2023年1月9日 03:42:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/75050748.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定