最近在研究embedding模型,发现向量模型大多是通过[CLS]来进行语义表征,比较好奇为什么[CLS]能表征单个句子的语义,比如bge是使用CLS作为sentence embedding
bge:BAAI/bge-large-zh · HF Mirror
With the transformers package, you can use the model like this: First, you pass your input through the transformer model, then you select the last hidden state of the first token (i.e., [CLS]) as the sentence embedding.
向量模型通常是基于BERT架构 ,Input会在句首加上一个[CLS]标签, CLS是classification的缩写,而CLS的输出能表征整个句子的含义,用于文本分类等下游任务。
选择CLS标签是因为其本来不具备语义,但是经过self-Attention层(Self-Attention 的核心是用文本中的其它词来增强目标词的语义表示)后,CLS很好地融合了整个句子的语义信息,可以理解为,将整个句子的语义信息都“平均”到了CLS上,因此CLS能够比较好地表征整个句子的语义。
除了CLS外,也有pooling方法是通过直接average整个最后一层的embedding实现的
另外收集信息时发现知乎上一个比较有趣的问题: