🚀 印度法律領域微調版Mistral-7B模型
這是Mistral 7B
模型的微調版本,藉助參數高效微調(PEFT)技術,結合QLoRA和LoRA方法,針對印度法律相關內容的理解與回覆生成進行了優化。
🚀 快速開始
安裝依賴
使用pip install transformers peft torch
,建議使用支持CUDA的torch版本。
代碼示例
from transformers import AutoModelForCausalLM, AutoTokenizer
from peft import PeftModel
model_name = "ajay-drew/midtral-7b-indian-law"
tokenizer = AutoTokenizer.from_pretrained(model_name)
base_model = AutoModelForCausalLM.from_pretrained("mistralai/Mixtral-7B-v0.1")
model = PeftModel.from_pretrained(base_model, model_name)
text = "What is the penalty for using forged document? "
inputs = tokenizer(text, return_tensors="pt")
outputs = model.generate(**inputs, max_length=200)
print(tokenizer.decode(outputs[0], skip_special_tokens=True))
模型指標
運行pip install transformers datasets torch
,使用支持CUDA的torch版本以減少指標檢查時間。運行以下代碼檢查模型的困惑度:
from transformers import AutoModelForCausalLM, AutoTokenizer, BitsAndBytesConfig
from datasets import load_dataset
import torch
dataset = load_dataset("kshitij230/Indian-Law", split="train")
quantization_config = BitsAndBytesConfig(
load_in_4bit=True,
bnb_4bit_compute_dtype=torch.float16
)
model_name = "ajay-drew/Mistral-7B-Indian-Law"
tokenizer = AutoTokenizer.from_pretrained(model_name)
model = AutoModelForCausalLM.from_pretrained(
model_name,
quantization_config=quantization_config,
device_map="auto"
)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.eval()
total_loss = 0
total_tokens = 0
test_texts = dataset['Instruction'][:500]
with torch.no_grad():
for text in test_texts:
inputs = tokenizer(text, return_tensors="pt", truncation=True, max_length=512).to(device)
outputs = model(**inputs, labels=inputs["input_ids"])
loss = outputs.loss
if loss is not None:
total_loss += loss.item() * inputs["input_ids"].size(1)
total_tokens += inputs["input_ids"].size(1)
if total_tokens > 0:
perplexity = torch.exp(torch.tensor(total_loss / total_tokens)).item()
print(f"Perplexity: {perplexity}")
print(f"Total tokens: {total_tokens}")
print(f"Total loss: {total_loss}")
else:
print("Error: No tokens processed. Check dataset or tokenization.")
✨ 主要特性
模型詳情
屬性 |
詳情 |
任務類型 |
法律文本理解與生成 |
微調數據集 |
自定義印度法律語料庫(jizzu/llama2_indian_law_v3) |
微調方法 |
結合QLoRA和LoRA的PEFT |
困惑度得分 |
37.32 |
基礎模型倉庫 |
mistralai/Mistral-7B-v0.1 |
適用範圍說明
- 對於高度模糊的法律查詢或非印度法律體系的問題,模型可能表現不佳。
- 困惑度表明,通過擴展訓練或數據,模型有進一步提升的潛力。
🔧 技術細節
硬件使用情況
- 硬件類型:NVIDIA GeForce RTX 4050筆記本GPU
- 使用時長:24:19:47
模型架構
- 基礎模型:
Mistral 7B
(基於Transformer的語言模型,擁有70億參數)
- 架構特點:
- 僅解碼器的Transformer結構,包含多頭自注意力層。
- 32層,隱藏層大小為4096,16個注意力頭(繼承自Mistral 7B)。
- 採用低秩自適應(LoRA)層進行高效微調。
- 微調方法:
- PEFT:參數高效微調,減少內存佔用。
- QLoRA:量化LoRA,使用4位量化高效調整權重。
- 微調參數:LoRA針對特定權重矩陣進行調整,基礎模型大部分參數保持凍結。
軟件環境
- CUDA:PyTorch - 2.6.0+cu126
📄 許可證
本模型遵循MIT許可證。
📚 詳細文檔
模型卡片聯繫方式
- 郵箱:drewjay05@gmail.com
- GitHub:github.com/ajay-drew
- 領英:linkedin.com/in/ajay-a-133b1326a/
框架版本