🚀 基于Shuu12121/CodeModernBERT-Snake的句子转换器🐍
本模型是一个基于 Shuu12121/CodeModernBERT-Snake 微调的 句子转换器(sentence-transformers) 模型。其中,Shuu12121/CodeModernBERT-Snake 是一个 专门为代码设计的、由本人从头预训练的 ModernBERT 模型。它专为代码搜索而设计,能够高效计算代码片段和文档之间的语义相似度。该模型的一个关键特性是其 最大序列长度可达 8192 个标记,这使其能够处理极长的代码片段和文档,非常适合全面的代码搜索任务。尽管它是一个参数约为 7500 万的小型模型,但在代码搜索任务中表现出色。
✨ 主要特性
- 专为代码搜索设计:能够高效计算代码片段和文档之间的语义相似度。
- 长序列处理能力:最大序列长度可达 8192 个标记,可处理极长的代码片段和文档。
- 小模型高性能:尽管参数仅约 7500 万,但在代码搜索任务中表现出色。
📦 安装指南
要安装句子转换器(Sentence Transformers),请运行以下命令:
pip install -U sentence-transformers
💻 使用示例
基础用法
from sentence_transformers import SentenceTransformer
model = SentenceTransformer("Shuu12121/CodeSearch-ModernBERT-Snake")
sentences = [
'Encrypts the zip file',
'def freeze_encrypt(dest_dir, zip_filename, config, opt):\n \n pgp_keys = grok_keys(config)\n icefile_prefix = "aomi-%s" % \\\n os.path.basename(os.path.dirname(opt.secretfile))\n if opt.icefile_prefix:\n icefile_prefix = opt.icefile_prefix\n\n timestamp = time.strftime("%H%M%S-%m-%d-%Y",\n datetime.datetime.now().timetuple())\n ice_file = "%s/%s-%s.ice" % (dest_dir, icefile_prefix, timestamp)\n if not encrypt(zip_filename, ice_file, pgp_keys):\n raise aomi.exceptions.GPG("Unable to encrypt zipfile")\n\n return ice_file',
'def transform(self, sents):\n \n\n def convert(tokens):\n return torch.tensor([self.vocab.stoi[t] for t in tokens], dtype=torch.long)\n\n if self.vocab is None:\n raise Exception(\n "Must run .fit() for .fit_transform() before " "calling .transform()."\n )\n\n seqs = sorted([convert(s) for s in sents], key=lambda x: -len(x))\n X = torch.LongTensor(pad_sequence(seqs, batch_first=True))\n return X',
]
embeddings = model.encode(sentences)
print(embeddings.shape)
similarities = model.similarity(embeddings, embeddings)
print(similarities.shape)
📚 详细文档
模型评估
CoIR 评估结果
本模型虽然是一个参数约为 7500 万的 极小模型,但在代码搜索任务的评估指标 CodeSearchNet 上达到了 72.12 的成绩。这一成绩与拥有 4 亿参数的 Salesforce/SFR-Embedding-Code-400M_R 模型相比也毫不逊色。由于该模型专注于代码搜索,不支持其他任务,因此未提供其他任务的评估分数。以下表格展示了与知名模型的对比,表明本模型尽管体积小巧,但仍能取得高分。
模型名 |
CodeSearchNet 评估值 |
Shuu12121/CodeModernBERT-Snake |
72.12 |
Salesforce/SFR-Embedding-Code-2B_R |
73.5 |
CodeSage-large-v2 |
94.26 |
Salesforce/SFR-Embedding-Code-400M_R |
72.53 |
CodeSage-large |
90.58 |
Voyage-Code-002 |
81.79 |
E5-Mistral |
54.25 |
E5-Base-v2 |
67.99 |
OpenAI-Ada-002 |
74.21 |
BGE-Base-en-v1.5 |
69.6 |
BGE-M3 |
43.23 |
UniXcoder |
60.2 |
GTE-Base-en-v1.5 |
43.35 |
Contriever |
34.72 |
模型详细信息
库版本信息
- Python: 3.11.11
- Sentence Transformers: 3.4.1
- Transformers: 4.50.0
- PyTorch: 2.6.0+cu124
- Accelerate: 1.5.2
- Datasets: 3.4.1
- Tokenizers: 0.21.1
引用信息
句子转换器(Sentence Transformers)
@inproceedings{reimers-2019-sentence-bert,
title = "Sentence-BERT: Sentence Embeddings using Siamese BERT-Networks",
author = "Reimers, Nils and Gurevych, Iryna",
booktitle = "Proceedings of the 2019 Conference on Empirical Methods in Natural Language Processing",
month = "11",
year = "2019",
publisher = "Association for Computational Linguistics",
url = "https://arxiv.org/abs/1908.10084",
}
MultipleNegativesRankingLoss
@misc{henderson2017efficient,
title={Efficient Natural Language Response Suggestion for Smart Reply},
author={Matthew Henderson and Rami Al-Rfou and Brian Strope and Yun-hsuan Sung and Laszlo Lukacs and Ruiqi Guo and Sanjiv Kumar and Balint Miklos and Ray Kurzweil},
year={2017},
eprint={1705.00652},
archivePrefix={arXiv},
primaryClass={cs.CL}
}
📄 许可证
本模型使用的许可证为 Apache-2.0。