🚀 ⭐ GLiClass:用於序列分類的通用輕量級模型
GLiClass 是一個高效的零樣本分類器,其靈感源自 GLiNER 的研究工作。它在性能上與交叉編碼器相當,但計算效率更高,因為分類僅需一次前向傳播即可完成。該模型可用於 主題分類
、情感分析
,還能在 RAG
管道中作為重排器使用。模型基於合成數據進行訓練,可用於商業應用,且除了初始數據集(MoritzLaurer/synthetic_zeroshot_mixtral_v0.1)外,未在其他任何數據集上進行額外的微調。
🚀 快速開始
安裝
首先,你需要安裝 GLiClass 庫:
pip install gliclass
初始化模型和管道
from gliclass import GLiClassModel, ZeroShotClassificationPipeline
from transformers import AutoTokenizer
model = GLiClassModel.from_pretrained("knowledgator/gliclass-large-v1.0-init")
tokenizer = AutoTokenizer.from_pretrained("knowledgator/gliclass-large-v1.0-init")
pipeline = ZeroShotClassificationPipeline(model, tokenizer, classification_type='multi-label', device='cuda:0')
text = "One day I will see the world!"
labels = ["travel", "dreams", "sport", "science", "politics"]
results = pipeline(text, labels, threshold=0.5)[0]
for result in results:
print(result["label"], "=>", result["score"])
✨ 主要特性
- 高效零樣本分類:受 GLiNER 啟發,在性能與交叉編碼器相當的情況下,計算效率更高。
- 多任務應用:可用於主題分類、情感分析以及 RAG 管道中的重排。
- 合成數據訓練:基於合成數據訓練,可用於商業應用。
📦 安裝指南
若要使用 GLiClass,可通過以下命令安裝:
pip install gliclass
💻 使用示例
基礎用法
from gliclass import GLiClassModel, ZeroShotClassificationPipeline
from transformers import AutoTokenizer
model = GLiClassModel.from_pretrained("knowledgator/gliclass-large-v1.0-init")
tokenizer = AutoTokenizer.from_pretrained("knowledgator/gliclass-large-v1.0-init")
pipeline = ZeroShotClassificationPipeline(model, tokenizer, classification_type='multi-label', device='cuda:0')
text = "One day I will see the world!"
labels = ["travel", "dreams", "sport", "science", "politics"]
results = pipeline(text, labels, threshold=0.5)[0]
for result in results:
print(result["label"], "=>", result["score"])
📚 詳細文檔
基準測試
以下是該模型在幾個文本分類數據集上的 F1 分數。所有測試模型均未在這些數據集上進行微調,且在零樣本設置下進行測試。
📄 許可證
本項目採用 Apache-2.0 許可證。