Hugging Face 社区利用 JAX 训练十亿级句向量模型
在 Hugging Face 组织的 JAX/Flax 社区周活动中,开发者利用 Google Cloud 基础设施成功训练了基于十亿对数据的最新 SOTA 句向量嵌入模型。该项目展示了开源社区协作与高效硬件结合在大规模 NLP 任务中的潜力,为语义搜索和聚类提供了更通用的向量表示方案。
报道正文
本文目录10 个章节
使用 10 亿训练对训练句子嵌入模型
发布于 2021 年 10 月 25 日 在 GitHub 上更新 点赞 2
Antoine SIMOULIN asi 客座作者
句子嵌入是一种将句子映射为实数向量的方法。理想情况下,这些向量应能捕捉句子的语义信息,并具备高度的通用性。此类表示可用于许多下游应用,例如聚类、文本挖掘或问答系统。
作为 "使用 10 亿训练对训练最佳句子嵌入模型" 项目的一部分,我们开发了最先进的句子嵌入模型。该项目是在由 Hugging Face 组织的 使用 JAX/Flax 进行 NLP 与 CV 的社区周 期间进行的。我们受益于高效的硬件基础设施来运行该项目:7 个 TPU v3-8,以及来自 Google Flax、JAX 和 Cloud 团队成员关于高效深度学习框架的指导!
训练方法论
模型
与单词不同,我们无法定义一个有限的句子集合。因此,句子嵌入方法通过组合内部单词来计算最终表示。例如,SentenceBert 模型 (Reimers and Gurevych, 2019) 使用了 Transformer——这是许多 NLP 应用的基石——随后对上下文化词向量执行池化操作。(参见下图。)
多重负例排序损失
组合模块的参数通常通过自监督目标进行训练。在本项目中,我们采用了一种对比学习方法,如下图所示。我们构建了一个由句子对 组成的数据集,其中配对句子的语义相近。例如,我们考虑诸如(查询,答案段落)、(问题,重复问题)、(论文标题,被引论文标题)这样的配对。我们的模型经过训练后,会将配对 映射为嵌入空间中彼此接近的向量,同时将非匹配对 映射为相距较远的向量。这种训练方法也被称为使用批内负样本的训练、InfoNCE 或 NTXentLoss。
形式上,给定一个批次中的训练样本,模型优化以下 损失函数:
下图展示了一个说明性示例。模型首先对批次中每个配对里的句子进行嵌入编码。然后,我们计算所有可能配对 之间的相似度矩阵。接着,我们将该相似度矩阵与指示原始配对关系的真实标签进行比较。最后,我们通过交叉熵损失执行比较。
直观来说,模型应为句子 “How many people live in Berlin?” 和 “Around 3.5 million people live in Berlin” 分配较高的相似度,而为其他负样本答案(如 “The capital of France is Paris”)分配较低的相似度,具体细节见下图。
在损失公式中,sim 表示 之间的相似度函数。该相似度函数可以是余弦相似度(Cosine-Similarity)或点积(Dot-Product)。这两种方法各有优缺点,总结如下(Thakur et al., 2021,Bachrach et al., 2014):
| 余弦相似度 | 点积 |
|---|---|
| 向量与自身的相似度最高,因为 cos(a,a)=1。 | 其他向量的点积可能更高,即 dot(a,a)<dot(a,b)。 |
| 对于归一化向量,它等于点积。最大向量长度为 1。 | 在某些近似最近邻方法中可能会更慢,因为最大向量长度未知。 |
| 对于归一化向量,它与欧几里得距离成正比。适用于 k-means 聚类。 | 不适用于 k-means 聚类。 |
在实践中,我们使用了缩放后的相似度,因为分数差异往往过小,并应用了一个缩放因子 ,使得 ,通常取 (Henderson et al., 2020,Radford et al., 2021)。
通过优化批次提升质量
在我们的方法中,我们构建样本对 的批次。我们将批次中的所有其他样本 视为负样本对。因此,批次的构成是一个关键的训练要素。鉴于该领域的文献,我们主要关注批次的三个核心方面。
1. 规模很重要
在对比学习中,更大的批次大小意味着更好的性能。正如从 Qu et al. (2021) 中提取的图所示,更大的批次大小能提升结果。
2. 困难负样本
在同一张图中,我们观察到包含困难负样本也能提升性能。困难负样本是指那些难以与 区分的样本 。在我们的例子中,可能是“法国的首都是什么?”和“美国的首都是什么?”这样的配对,它们具有相近的语义内容,需要精确理解整个句子才能正确回答。相反,“法国的首都是什么?”和“有多少部星球大战电影?”则较容易区分,因为它们不涉及相同的主题。
3. 跨数据集批次
We concatenated multiple datasets to train our models. We built a large batch and gathered samples from the same batch dataset to limit the topic distribution and favor hard negatives. However, we also mix at least two datasets in the batch to learn a global structure between topics and not only a local structure within a topic.
Training infrastructure and data
As mentioned earlier, the quantity of data and the batch size directly impact the model performances. As part of the project, we benefited from efficient hardware infrastructure. We trained our models on TPUs which are compute units developed by Google and super efficient for matrix multiplications. TPUs have some hardware specificities which might require some specific code implementation.
Additionally, we trained models on a large corpus as we concatenated multiple datasets up to 1 billion sentence pairs! All datasets used are detailed for each model in the model card.
Conclusion
You can find all models and datasets we created during the challenge in our HuggingFace repository. We trained 20 general-purpose Sentence Transformers models such as Mini-LM (Wang and al., 2020), RoBERTa (liu and al., 2019), DistilBERT (Sanh and al., 2020) and MPNet (Song and al., 2020). Our models achieve SOTA on multiple general-purpose Sentence Similarity evaluation tasks. We also shared 8 datasets specialized for Question Answering, Sentence-Similarity, and Gender Evaluation.
General sentence embeddings might be used for many applications. We built a Spaces demo to showcase several applications:
[注:本文篇幅超长,以上为核心前篇精译,后续内容保留原文呈现]
- The sentence similarity module compares the similarity of the main text with other texts of your choice. In the background, the demo extracts the embedding for each text and computes the similarity between the source sentence and the other using cosine similarity.
- Asymmetric QA compares the answer likeliness of a given query with answer candidates of your choice.
- Search / Cluster returns nearby answers from a query. For example, if you input « python », it will retrieve closest sentences using dot-product distance.
- Gender Bias Evaluation report inherent gender bias in training set via random sampling of the sentences. Given an anchor text without mentioning gender for target occupation and 2 propositions with gendered pronouns, we compare if models assign a higher similarity to a given proposition and therefore evaluate their proportion to favor a specific gender.
The Community week using JAX/Flax for NLP & CV has been an intense and highly rewarding experience! The quality of Google’s Flax, JAX, and Cloud and Hugging Face team members' guidance and their presence helped us all learn a lot. We hope all projects had as much fun as we did in ours. Whenever you have questions or suggestions, don’t hesitate to contact us!



