🚀 DISTIL-ITA-LEGAL-BERT
本项目运用知识蒸馏技术,打造了一个仅含 4 层 Transformer 的快速轻量级学生模型。该模型能够生成与更复杂的 ITALIAN-LEGAL-BERT 教师模型相似的句子嵌入。它在 ITALIAN-LEGAL-BERT 训练集(3.7GB)上进行优化,借助 Sentence-BERT 库,通过最小化其嵌入与教师模型嵌入之间的均方误差(MSE)来提升性能。这是一个 sentence-transformers 模型,可将句子和段落映射到 768 维的密集向量空间,适用于聚类或语义搜索等任务。
🚀 快速开始
本模型可用于将句子和段落映射到 768 维的密集向量空间,适用于聚类或语义搜索等任务。
✨ 主要特性
- 运用知识蒸馏技术,构建快速轻量级的学生模型。
- 能够生成与 ITALIAN-LEGAL-BERT 教师模型相似的句子嵌入。
- 基于 Sentence-BERT 库,在 ITALIAN-LEGAL-BERT 训练集上优化。
📦 安装指南
若要使用此模型,需安装 sentence-transformers:
pip install -U sentence-transformers
💻 使用示例
基础用法
from sentence_transformers import SentenceTransformer
sentences = ["This is an example sentence", "Each sentence is converted"]
model = SentenceTransformer('dlicari/distil-ita-legal-bert')
embeddings = model.encode(sentences)
print(embeddings)
高级用法
若未安装 sentence-transformers,可按以下方式使用模型:首先将输入传递给 Transformer 模型,然后对上下文词嵌入应用正确的池化操作。
from transformers import AutoTokenizer, AutoModel
import torch
def mean_pooling(model_output, attention_mask):
token_embeddings = model_output[0]
input_mask_expanded = attention_mask.unsqueeze(-1).expand(token_embeddings.size()).float()
return torch.sum(token_embeddings * input_mask_expanded, 1) / torch.clamp(input_mask_expanded.sum(1), min=1e-9)
sentences = ['This is an example sentence', 'Each sentence is converted']
tokenizer = AutoTokenizer.from_pretrained('dlicari/distil-ita-legal-bert')
model = AutoModel.from_pretrained('dlicari/distil-ita-legal-bert')
encoded_input = tokenizer(sentences, padding=True, truncation=True, return_tensors='pt')
with torch.no_grad():
model_output = model(**encoded_input)
sentence_embeddings = mean_pooling(model_output, encoded_input['attention_mask'])
print("Sentence embeddings:")
print(sentence_embeddings)
📚 详细文档
评估结果
若要对该模型进行自动评估,请参考 Sentence Embeddings Benchmark:https://seb.sbert.net
训练
模型训练参数如下:
数据加载器:
torch.utils.data.dataloader.DataLoader
,长度为 409633,参数如下:
{'batch_size': 24, 'sampler': 'torch.utils.data.sampler.RandomSampler', 'batch_sampler': 'torch.utils.data.sampler.BatchSampler'}
损失函数:
sentence_transformers.losses.MSELoss.MSELoss
fit()
方法的参数:
{
"epochs": 4,
"evaluation_steps": 5000,
"evaluator": "sentence_transformers.evaluation.SequentialEvaluator.SequentialEvaluator",
"max_grad_norm": 1,
"optimizer_class": "<class 'transformers.optimization.AdamW'>",
"optimizer_params": {
"correct_bias": false,
"eps": 1e-06,
"lr": 0.0001
},
"scheduler": "WarmupLinear",
"steps_per_epoch": null,
"warmup_steps": 1000,
"weight_decay": 0.01
}
完整模型架构
SentenceTransformer(
(0): Transformer({'max_seq_length': 512, 'do_lower_case': False}) with Transformer model: BertModel
(1): Pooling({'word_embedding_dimension': 768, 'pooling_mode_cls_token': False, 'pooling_mode_mean_tokens': True, 'pooling_mode_max_tokens': False, 'pooling_mode_mean_sqrt_len_tokens': False})
)
🔧 技术细节
本模型通过知识蒸馏技术,从更复杂的 ITALIAN-LEGAL-BERT 教师模型中提取知识,构建了一个轻量级的学生模型。在训练过程中,使用 Sentence-BERT 库,通过最小化学生模型嵌入与教师模型嵌入之间的均方误差(MSE)来优化模型。
📄 许可证
本模型采用 AFL-3.0 许可证。
📋 信息表格
属性 |
详情 |
模型类型 |
sentence-transformers 模型,可将句子和段落映射到 768 维的密集向量空间 |
训练数据 |
ITALIAN-LEGAL-BERT 训练集(3.7GB) |