🚀 LSG模型
LSG模型是一個基於CamemBERT-base進行適配的模型,無需額外預訓練,使用相同數量的參數和層以及相同的分詞器。它能夠處理長序列,並且比Longformer或BigBird(來自Transformers)更快、更高效,依賴於局部+稀疏+全局注意力(LSG)機制。
🚀 快速開始
此模型依賴於自定義建模文件,需要添加trust_remote_code=True
才能使用。同時,該模型要求序列長度是塊大小的倍數,模型具有“自適應”功能,必要時會自動填充序列(在配置中adaptive=True
)。不過,建議藉助分詞器截斷輸入(truncation=True
),並可選擇以塊大小的倍數進行填充(pad_to_multiple_of=...
)。
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("ccdv/lsg-camembert-base-4096", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
✨ 主要特性
- 基於CamemBERT-base適配,無需額外預訓練。
- 能夠處理長序列,比Longformer或BigBird更快、更高效。
- 依賴於局部+稀疏+全局注意力(LSG)機制。
- 模型具有“自適應”功能,可自動填充序列。
📦 安裝指南
該模型依賴於自定義建模文件,使用時需要添加trust_remote_code=True
。同時,需要確保Transformers >= 4.36.1
。
💻 使用示例
基礎用法
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("ccdv/lsg-camembert-base-4096", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
高級用法
參數設置
可以更改各種參數,如全局令牌數量、局部塊大小、稀疏塊大小、稀疏因子等。
from transformers import AutoModel
model = AutoModel.from_pretrained("ccdv/lsg-camembert-base-4096",
trust_remote_code=True,
num_global_tokens=16,
block_size=64,
sparse_block_size=64,
attention_probs_dropout_prob=0.0,
sparsity_factor=4,
sparsity_type="none",
mask_first_token=True
)
掩碼填充任務
from transformers import FillMaskPipeline, AutoModelForMaskedLM, AutoTokenizer
model = AutoModelForMaskedLM.from_pretrained("ccdv/lsg-camembert-base-4096", trust_remote_code=True)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
SENTENCES = "Paris est la <mask> de la France."
pipeline = FillMaskPipeline(model, tokenizer)
output = pipeline(SENTENCES)
序列分類任務
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained("ccdv/lsg-camembert-base-4096",
trust_remote_code=True,
pool_with_global=True,
)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
SENTENCE = "This is a test for sequence classification. " * 300
token_ids = tokenizer(
SENTENCE,
return_tensors="pt",
truncation=True
)
output = model(**token_ids)
訓練全局令牌
from transformers import AutoModelForSequenceClassification, AutoTokenizer
model = AutoModelForSequenceClassification.from_pretrained("ccdv/lsg-camembert-base-4096",
trust_remote_code=True,
pool_with_global=True,
num_global_tokens=16
)
tokenizer = AutoTokenizer.from_pretrained("ccdv/lsg-camembert-base-4096")
for name, param in model.named_parameters():
if "global_embeddings" not in name:
param.requires_grad = False
else:
param.required_grad = True
📚 詳細文檔
稀疏選擇類型
有6種不同的稀疏選擇模式,最佳類型取決於任務。
sparsity_type="bos_pooling"
(新):使用BOS令牌進行加權平均池化,通常效果最佳,尤其是在稀疏因子較大(8、16、32)時。
sparsity_type="norm"
:選擇範數最高的令牌,適用於較小的稀疏因子(2到4)。
sparsity_type="pooling"
:使用平均池化合並令牌,適用於較小的稀疏因子(2到4)。
sparsity_type="lsh"
:使用LSH算法對相似令牌進行聚類,適用於較大的稀疏因子(4+)。LSH依賴於隨機投影,因此不同種子的推理結果可能略有不同。
sparsity_type="stride"
:每個頭使用不同的令牌,按稀疏因子跨步。如果稀疏因子大於頭數,則不建議使用。
sparsity_type="block_stride"
:每個頭使用按稀疏因子跨步的令牌塊。如果稀疏因子大於頭數,則不建議使用。
注意事項
- 如果
sparse_block_size=0
或sparsity_type="none"
,則僅考慮局部注意力。
- 對於長度小於2*塊大小的序列,稀疏選擇類型沒有影響。
🔧 技術細節
該模型基於CamemBERT-base進行適配,使用局部+稀疏+全局注意力(LSG)機制,能夠處理長序列。模型要求序列長度是塊大小的倍數,具有“自適應”功能,可自動填充序列。
📄 許可證
CamemBERT引用
@inproceedings{Martin_2020,
doi = {10.18653/v1/2020.acl-main.645},
url = {https://doi.org/10.18653%2Fv1%2F2020.acl-main.645},
year = 2020,
publisher = {Association for Computational Linguistics},
author = {Louis Martin and Benjamin Muller and Pedro Javier Ortiz Su{\'{a}}rez and Yoann Dupont and Laurent Romary and {\'{E}}ric de la Clergeri and Djam{\'{e}} Seddah and Beno{\^{\i}}t Sagot},
title = {{CamemBERT}: a Tasty French Language Model},
booktitle = {Proceedings of the 58th Annual Meeting of the Association for Computational Linguistics}
}
相關鏈接
- LSG ArXiv 論文
- Github/轉換腳本可在這個鏈接獲取。