模型简介
模型特点
模型能力
使用案例
🚀 CodeMorph-ModernBERT
CodeMorph-ModernBERT 是一个专门为代码搜索和代码理解任务从头开始训练的预训练模型。该模型利用 code-search-net/code_search_net
数据集进行训练,以增强对代码的语义理解。它支持 最大 2048 个标记的序列长度(相比之下,传统的 Microsoft 模型仅支持 512 个标记),尤其在 Python 代码搜索中表现出色。
🚀 快速开始
你可以使用 Hugging Face Transformers 库轻松加载本模型。(※ Transformers 的版本需为 4.48.0
及以上)
模型加载
from transformers import AutoModelForMaskedLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("Shuu12121/CodeMorph-ModernBERT")
model = AutoModelForMaskedLM.from_pretrained("Shuu12121/CodeMorph-ModernBERT")
掩码填充 (fill-mask)
from transformers import pipeline
fill_mask = pipeline("fill-mask", model=model, tokenizer=tokenizer)
print(fill_mask("def add_numbers(a, b): return a + [MASK]"))
获取代码嵌入
import torch
def get_embedding(text, model, tokenizer, device="cuda"):
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256)
if "token_type_ids" in inputs:
inputs.pop("token_type_ids")
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = model.model(**inputs)
embedding = outputs.last_hidden_state[:, 0, :]
return embedding
embedding = get_embedding("def my_function(): pass", model, tokenizer)
print(embedding.shape)
✨ 主要特性
-
支持长序列
能够处理最长 2048 个标记的序列,适用于长代码和复杂函数。 -
高代码搜索性能
采用基于六种编程语言训练的 Sentencepiece 分词器,实现了比以往模型显著更高的搜索精度。 -
专门训练的模型
使用 CodeSearchNet 数据集从头开始训练,能够深入理解编程语法和注释。
📦 安装指南
文档未提及安装相关内容,可参考快速开始部分使用模型。
💻 使用示例
基础用法
from transformers import AutoModelForMaskedLM, AutoTokenizer
tokenizer = AutoTokenizer.from_pretrained("Shuu12121/CodeMorph-ModernBERT")
model = AutoModelForMaskedLM.from_pretrained("Shuu12121/CodeMorph-ModernBERT")
高级用法
from transformers import pipeline
fill_mask = pipeline("fill-mask", model=model, tokenizer=tokenizer)
print(fill_mask("def add_numbers(a, b): return a + [MASK]"))
import torch
def get_embedding(text, model, tokenizer, device="cuda"):
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=256)
if "token_type_ids" in inputs:
inputs.pop("token_type_ids")
inputs = {k: v.to(device) for k, v in inputs.items()}
outputs = model.model(**inputs)
embedding = outputs.last_hidden_state[:, 0, :]
return embedding
embedding = get_embedding("def my_function(): pass", model, tokenizer)
print(embedding.shape)
📚 详细文档
模型参数
模型设计使用了以下参数:
属性 | 详情 |
---|---|
模型类型 | ModernBERT 架构 |
训练数据 | CodeSearchNet(所有语言) |
最大序列长度 | 2048 个标记 |
词汇表大小 (vocab_size ) |
50000 |
隐藏层大小 (hidden_size ) |
768 |
隐藏层数 (num_hidden_layers ) |
12 |
注意力头数 (num_attention_heads ) |
12 |
中间层大小 (intermediate_size ) |
3072 |
最大位置嵌入 (max_position_embeddings ) |
2048 |
类型词汇表大小 (type_vocab_size ) |
2 |
隐藏层丢弃概率 (hidden_dropout_prob ) |
0.1 |
注意力概率丢弃概率 (attention_probs_dropout_prob ) |
0.1 |
局部注意力窗口 (local_attention_window ) |
128 |
rope_theta |
160000 |
局部注意力 rope_theta (local_attention_rope_theta ) |
10000 |
数据集
本模型使用 code-search-net/code_search_net
数据集进行训练。该数据集包含多种编程语言(如 Python、Java、JavaScript 等)的代码片段,非常适合代码搜索任务。
评估结果
本模型使用 code_x_glue_ct_code_to_text
数据集的 Python 部分进行评估。以下是主要的评估指标:
指标 | 得分 |
---|---|
MRR (平均倒数排名) | 0.8172 |
MAP (平均精度均值) | 0.8172 |
R-Precision | 0.7501 |
Recall@10 | 0.9389 |
Precision@10 | 0.8143 |
NDCG@10 | 0.8445 |
F1@10 | 0.8423 |
与其他模型的比较
以下是 CodeMorph-ModernBERT 与其他主要代码搜索模型的比较结果:
模型 | MRR | MAP | R-Precision |
---|---|---|---|
CodeMorph-ModernBERT | 0.8172 | 0.8172 | 0.7501 |
microsoft/graphcodebert-base | 0.5482 | 0.5482 | 0.4458 |
microsoft/codebert-base-mlm | 0.5243 | 0.5243 | 0.4378 |
Salesforce/codet5p-220m-py | 0.7512 | 0.7512 | 0.6617 |
Salesforce/codet5-large-ntp-py | 0.7846 | 0.7846 | 0.7067 |
Shuu12121/CodeMorph-BERT | 0.6851 | 0.6851 | 0.5934 |
Shuu12121/CodeMorph-BERTv2 | 0.6535 | 0.6535 | 0.5543 |
Code Search 模型评估结果 (google/code_x_glue_tc_nl_code_search_adv 数据集测试)
以下是使用 google/code_x_glue_tc_nl_code_search_adv
数据集(测试集)对各种 Code Search 模型的评估结果总结。所有评估的候选池大小均为 100。
点击此处查看额外实验代码
模型 | MRR | MAP | R-Precision |
---|---|---|---|
Shuu12121/CodeMorph-ModernBERT | 0.6107 | 0.6107 | 0.5038 |
Salesforce/codet5p-220m-py | 0.5037 | 0.5037 | 0.3805 |
Salesforce/codet5-large-ntp-py | 0.4872 | 0.4872 | 0.3658 |
microsoft/graphcodebert-base | 0.3844 | 0.3844 | 0.2764 |
microsoft/codebert-base-mlm | 0.3766 | 0.3766 | 0.2683 |
Shuu12121/CodeMorph-BERTv2 | 0.3142 | 0.3142 | 0.2166 |
Shuu12121/CodeMorph-BERT | 0.2978 | 0.2978 | 0.1992 |
与其他 CodeBERT 和 CodeT5 模型相比,CodeMorph-ModernBERT 实现了更高的搜索精度。
多语言评估结果
CodeMorph-ModernBERT 在多种编程语言中均表现出较高的代码搜索性能。以下是每种语言的主要评估指标(MRR、MAP、R-Precision)概要: 点击此处查看实验笔记本
语言 | MRR | MAP | R-Precision |
---|---|---|---|
Python | 0.8098 | 0.8098 | 0.7520 |
Java | 0.6437 | 0.6437 | 0.5480 |
JavaScript | 0.5928 | 0.5928 | 0.4880 |
PHP | 0.7512 | 0.7512 | 0.6710 |
Ruby | 0.7188 | 0.7188 | 0.6310 |
Go | 0.5358 | 0.5358 | 0.4320 |
此外,Salesforce/codet5p-220m-bimodal 在整体上的搜索精度高于 CodeMorph-ModernBERT:
语言 | MRR | MAP | R-Precision |
---|---|---|---|
Python | 0.8322 | 0.8322 | 0.7660 |
Java | 0.8886 | 0.8886 | 0.8390 |
JavaScript | 0.7611 | 0.7611 | 0.6710 |
PHP | 0.8985 | 0.8985 | 0.8530 |
Ruby | 0.7635 | 0.7635 | 0.6740 |
Go | 0.8127 | 0.8127 | 0.7260 |
然而,在另一个数据集 google/code_x_glue_tc_nl_code_search_adv
(测试集)上,CodeMorph-ModernBERT 的得分更高:
模型 | MRR | MAP | R-Precision |
---|---|---|---|
Shuu12121/CodeMorph-ModernBERT | 0.6107 | 0.6107 | 0.5038 |
Salesforce/codet5p-220m-bimodal | 0.5326 | 0.5326 | 0.4208 |
这表明 CodeMorph-ModernBERT 在 更具挑战性的任务和 Python 泛化方面 可能更具优势。
🔧 技术细节
文档未提供详细技术实现细节。
📄 许可证
本模型在 Apache-2.0
许可证下发布。
💡 使用建议
- 确保使用的 Transformers 库版本为
4.48.0
及以上,以保证模型正常加载和使用。 - 在进行代码嵌入获取时,可根据实际情况调整
max_length
参数,以平衡性能和准确性。
📞 联系方式
如果您对本模型有任何疑问,请发送邮件至 shun0212114@outlook.jp。



