BARTpho:越南语预训练序列到序列模型
项目介绍
BARTpho是一个为越南语设计的预训练序列到序列模型,包括BARTpho-syllable和 BARTpho-word两个版本。这两个版本是首个公开的大型单语种序列到序列模型,基于BART的“大型”架构和预训练方案,特别适合生成式自然语言处理(NLP)任务。BARTpho在越南语文本摘要、标点符号恢复和大小写转换等下游任务中表现出色,超越了强大的基线模型mBART。
项目技术分析
BARTpho采用了BART的架构,这是一种序列到序列的变分自编码器,用于预训练和微调自然语言处理任务。BARTpho的两个版本分别针对音节级别和单词级别,可以处理不同粒度的文本输入。该模型在预训练阶段使用了大规模的越南语数据集,通过去噪自编码器的方式学习文本的表示。
BARTpho的技术优势体现在以下几个方面:
- 预训练任务:BARTpho通过一系列预训练任务学习到了丰富的语言表示,包括随机删除、替换和乱序等。
- 模型架构:BARTpho采用大型架构,拥有更多的参数,可以捕获更复杂的语言特征。
- 多任务适应性:BARTpho经过预训练后,可以适应多种下游任务,如文本摘要、标点符号恢复等。
项目技术应用场景
BARTpho可以应用于多种场景,包括但不限于以下几方面:
- 文本摘要:在处理大量越南语文本时,BARTpho可以自动生成文本摘要,提高信息筛选的效率。
- 自然语言生成:在聊天机器人、内容生成等领域,BARTpho可以根据输入生成流畅、自然的越南语文本。
- 文本校正:BARTpho可以帮助校正文本中的标点符号和大小写错误,提高文本质量。
- 机器翻译:作为预训练模型,BARTpho也可以为越南语机器翻译任务提供强大的语言模型支撑。
项目特点
- 创新性:BARTpho是首个公开的大型越南语序列到序列预训练模型,填补了越南语NLP领域的空白。
- 性能卓越:在多个越南语NLP任务中,BARTpho表现出优于现有基线的性能。
- 易于使用:BARTpho支持与主流深度学习框架如transformers和fairseq的集成,便于研究人员和开发者快速部署和使用。
- 开放源代码:BARTpho遵循MIT开源协议,用户可以自由使用、修改和分发。
以下是BARTpho的详细使用说明和代码示例:
使用transformers集成BARTpho
安装
pip install transformers
pip install sentencepiece tokenizers
预训练模型
| 模型 | 参数量 | 架构 | 最大长度 | 输入文本 | | --- | --- | --- | --- | --- | | vinai/bartpho-syllable-base | 132M | base | 1024 | 音节级别 | | vinai/bartpho-syllable | 396M | large | 1024 | 音节级别 | | vinai/bartpho-word-base | 150M | base | 1024 | 单词级别 | | vinai/bartpho-word | 420M | large | 1024 | 单词级别 |
示例用法
import torch
from transformers import AutoModel, AutoTokenizer
# BARTpho-syllable
syllable_tokenizer = AutoTokenizer.from_pretrained("vinai/bartpho-syllable")
bartpho_syllable = AutoModel.from_pretrained("vinai/bartpho-syllable")
TXT = 'Chúng tôi là những nghiên cứu viên.'
input_ids = syllable_tokenizer(TXT, return_tensors='pt')['input_ids']
features = bartpho_syllable(input_ids)
# BARTpho-word
word_tokenizer = AutoTokenizer.from_pretrained("vinai/bartpho-word")
bartpho_word = AutoModel.from_pretrained("vinai/bartpho-word")
TXT = 'Chúng_tôi là những nghiên_cứu_viên.'
input_ids = word_tokenizer(TXT, return_tensors='pt')['input_ids']
features = bartpho_word(input_ids)
使用fairseq集成BARTpho
安装
git clone https://github.com/datquocnguyen/fairseq.git
cd fairseq
pip install --editable ./
预训练模型
| 模型 | 参数量 | 下载 | 输入文本 | | --- | --- | --- | --- | | BARTpho-syllable | 396M | fairseq-bartpho-syllable.zip | 音节级别 | | BARTpho-word | 420M | fairseq-bartpho-word.zip | 单词级别 |
示例用法
from fairseq.models.bart import BARTModel
# Load BARTpho-syllable model
model_folder_path = '/PATH-TO-FOLDER/fairseq-bartpho-syllable/'
spm_model_path = '/PATH-TO-FOLDER/fairseq-bartpho-syllable/sentence.bpe.model'
bartpho_syllable = BARTModel.from_pretrained(model_folder_path, checkpoint_file='model.pt', bpe='sentencepiece', sentencepiece_model=spm_model_path).eval()
sentence = 'Chúng tôi là những nghiên cứu viên.'
tokenIDs = bartpho_syllable.encode(sentence, add_if_not_exist=False)
last_layer_features = bartpho_syllable.extract_features(tokenIDs)
# Load BARTpho-word model
model_folder_path = '/PATH-TO-FOLDER/fairseq-bartpho-word/'
bpe_codes_path = '/PATH-TO-FOLDER/fairseq-bartpho-word/bpe.codes'
bartpho_word = BARTModel.from_pretrained(model_folder_path, checkpoint_file='model.pt', bpe='fastbpe', bpe_codes=bpe_codes_path).eval()
sentence = 'Chúng_tôi là những nghiên_cứu_viên.'
tokenIDs = bartpho_word.encode(sentence, add_if_not_exist=False)
last_layer_features = bartpho_word.extract_features(tokenIDs)
通过上述说明和代码示例,用户可以快速集成和使用BARTpho模型,提升越南语NLP任务的性能。我们鼓励用户在各自的NLP项目中使用BARTpho,并引用相关论文以支持进一步的研究。
创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考



