🚀 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) |