🚀 西班牙BERT2BERT(BETO)在MLSUM ES數據集上微調用於文本摘要
本項目基於MLSUM ES數據集對西班牙BERT2BERT(BETO)模型進行微調,用於文本摘要任務。該模型能夠有效處理西班牙語文本,為新聞等文本提供高質量的摘要。
🚀 快速開始
環境準備
確保你已經安裝了torch
和transformers
庫。如果沒有安裝,可以使用以下命令進行安裝:
pip install torch transformers
代碼示例
import torch
from transformers import BertTokenizerFast, EncoderDecoderModel
device = 'cuda' if torch.cuda.is_available() else 'cpu'
ckpt = 'mrm8488/bert2bert_shared-spanish-finetuned-summarization'
tokenizer = BertTokenizerFast.from_pretrained(ckpt)
model = EncoderDecoderModel.from_pretrained(ckpt).to(device)
def generate_summary(text):
inputs = tokenizer([text], padding="max_length", truncation=True, max_length=512, return_tensors="pt")
input_ids = inputs.input_ids.to(device)
attention_mask = inputs.attention_mask.to(device)
output = model.generate(input_ids, attention_mask=attention_mask)
return tokenizer.decode(output[0], skip_special_tokens=True)
text = "Your text here..."
generate_summary(text)
✨ 主要特性
- 多語言支持:基於MLSUM數據集,該數據集包含法語、德語、西班牙語、俄語和土耳其語等五種語言的文章和摘要對,為跨語言研究提供了可能。
- 微調模型:在MLSUM ES數據集上對BERT2BERT模型進行微調,提高了模型在西班牙語文本摘要任務上的性能。
- 良好的評估指標:在測試集上,模型取得了較好的Rouge指標,如Rouge1為26.24,Rouge2為8.9等。
📦 安裝指南
使用以下命令安裝所需的庫:
pip install torch transformers
💻 使用示例
基礎用法
import torch
from transformers import BertTokenizerFast, EncoderDecoderModel
device = 'cuda' if torch.cuda.is_available() else 'cpu'
ckpt = 'mrm8488/bert2bert_shared-spanish-finetuned-summarization'
tokenizer = BertTokenizerFast.from_pretrained(ckpt)
model = EncoderDecoderModel.from_pretrained(ckpt).to(device)
def generate_summary(text):
inputs = tokenizer([text], padding="max_length", truncation=True, max_length=512, return_tensors="pt")
input_ids = inputs.input_ids.to(device)
attention_mask = inputs.attention_mask.to(device)
output = model.generate(input_ids, attention_mask=attention_mask)
return tokenizer.decode(output[0], skip_special_tokens=True)
text = "Your text here..."
generate_summary(text)
高級用法
你可以根據具體需求調整max_length
等參數,以獲得更好的摘要效果。
📚 詳細文檔
模型
使用的是 dccuchile/bert-base-spanish-wwm-cased(BERT檢查點)。
數據集
MLSUM 是第一個大規模的多語言摘要數據集。它從在線報紙獲取,包含150多萬篇文章/摘要對,涵蓋五種不同語言,即法語、德語、西班牙語、俄語和土耳其語。與流行的CNN/Daily mail數據集中的英文報紙文章一起,收集的數據構成了一個大規模的多語言數據集,為文本摘要社區帶來了新的研究方向。我們基於最先進的系統進行了跨語言比較分析,這些分析凸顯了現有的偏差,這也促使我們使用多語言數據集。
MLSUM es
結果
數據集 |
指標 |
值 |
測試集 |
Rouge2 - mid - 精確率 |
9.6 |
測試集 |
Rouge2 - mid - 召回率 |
8.4 |
測試集 |
Rouge2 - mid - F值 |
8.7 |
測試集 |
Rouge1 |
26.24 |
測試集 |
Rouge2 |
8.9 |
測試集 |
RougeL |
21.01 |
測試集 |
RougeLsum |
21.02 |
📄 許可證
文檔中未提及相關許可證信息。
由 Manuel Romero/@mrm8488 在 Narrativa 的支持下創建。
於西班牙用心打造 ♥