🚀 HPD-MiniLM-F128
本仓库包含了我们论文 Compressing Sentence Representation for Semantic Retrieval via Homomorphic Projective Distillation 的预训练模型。该句子嵌入模型仅包含 2300 万个参数,模型大小仅为 87MB。
🚀 快速开始
本项目提供了一个轻量级的句子嵌入模型,适用于语义检索等任务。你可以按照以下步骤快速使用该模型:
pip install -U sentence-transformers
安装完成后,你可以轻松加载我们的模型:
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('Xuandong/HPD-MiniLM-F128')
然后,你可以使用我们的模型将句子编码为嵌入向量:
sentences = ['He plays guitar.', 'A street vendor is outside.']
sentence_embeddings = model.encode(sentences)
for sentence, embedding in zip(sentences, sentence_embeddings):
print("Sentence:", sentence)
print("Embedding:", embedding)
print("")
✨ 主要特性
- 轻量级模型:句子嵌入模型仅包含 2300 万个参数,模型大小仅为 87MB,便于部署和使用。
- 高质量表示:提出了同态投影蒸馏(HPD)方法,在学习压缩句子嵌入的同时,保留了句子表示的质量。
- 广泛适用性:该模型可以将句子和段落映射到 128 维的密集向量空间,可用于聚类或语义搜索等任务。
📦 安装指南
如果你已经安装了 sentence-transformers,使用此模型将变得非常简单:
pip install -U sentence-transformers
💻 使用示例
基础用法
from sentence_transformers import SentenceTransformer
model = SentenceTransformer('Xuandong/HPD-MiniLM-F128')
sentences = ['He plays guitar.', 'A street vendor is outside.']
sentence_embeddings = model.encode(sentences)
for sentence, embedding in zip(sentences, sentence_embeddings):
print("Sentence:", sentence)
print("Embedding:", embedding)
print("")
高级用法
📚 详细文档
概述
我们提出了同态投影蒸馏(HPD) 方法来学习压缩句子嵌入。我们的方法通过可学习的投影层增强小型 Transformer 编码器模型,以生成紧凑的表示,同时模仿大型预训练语言模型,以保留句子表示的质量。
细节
这是一个 sentence-transformers 模型:它将句子和段落映射到 128 维的密集向量空间,可用于聚类或语义搜索等任务。
教师模型是 princeton-nlp/sup-simcse-roberta-large
,学生模型是 nreimers/MiniLM-L6-H384-uncased
。
评估结果
我们在语义文本相似度(STS)任务上评估了我们的模型。结果如下:
STS12 |
STS13 |
STS14 |
STS15 |
STS16 |
STS-B |
SICK-R |
平均 |
74.94 |
84.52 |
80.25 |
84.87 |
81.90 |
84.98 |
81.15 |
81.80 |
训练
有关训练的详细信息,请参考 GitHub 仓库 (https://github.com/XuandongZhao/HPD)。
完整模型架构
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 384, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
(2): Dense({'in_features': 384, 'out_features': 128, 'bias': True, 'activation_function': 'torch.nn.modules.activation.Tanh'})
)
引用
如果你在工作中使用了 HPD,请引用我们的论文:
@article{zhao2022compressing,
title={Compressing Sentence Representation for Semantic Retrieval via Homomorphic Projective Distillation},
author={Zhao, Xuandong and Yu, Zhiguo and Wu, Ming and Li, Lei},
journal={arXiv preprint arXiv:2203.07687},
year={2022}
}
📄 许可证
本项目采用 Apache-2.0 许可证。