自学内容网 自学内容网

BART--详解

BART(Bidirectional and Auto-Regressive Transformers)是一种序列到序列(Seq2Seq)预训练模型。BART结合了BERT的双向编码能力和GPT的自回归解码能力,是一种灵活且强大的生成模型,擅长解决各种文本生成任务,如摘要生成、翻译、对话生成和文本填充。

BART的创新之处在于它能够在输入端通过破坏(例如添加噪声、遮蔽、打乱顺序等)对输入数据进行扰动,模型的任务是将扰动的数据还原为原始输入。它因此也被称为一种去噪自编码器(denoising autoencoder)。这种训练方式使得BART能够处理多种文本生成任务。

BART的基本结构

BART是典型的基于Transformer架构的序列到序列模型。它的核心结构与标准的Transformer相同,包含两个主要部分:

  1. Encoder(编码器)

    • 与BERT类似,BART的编码器是双向的,能捕捉输入文本的上下文信息。编码器将输入句子转化为一个隐状态表示。

  2. Decoder(解码器)

    • 与GPT类似,BART的解码器是自回归的,即每一步根据之前生成的词来预测下一个词。解码器接收编码器生成的隐状态,并生成目标序列(例如翻译后的文本或摘要)。

BART的训练方式为去噪自编码器任务,具体来说,它会对输入进行以下几种扰动:

  • Token Masking:像BERT一样,随机遮蔽一些词。

  • Token Deletion:随机删除输入序列中的一些词。

  • Sentence Permutation:打乱输入序列中句子的顺序。

  • Document Rotation:将输入文本的顺序进行旋转,改变句子的起始位置。

通过这些扰动,BART学会了如何在被破坏的输入上生成高质量的输出,这使它能够很好地应对生成类任务。

经典BART代码实现

使用Hugging Face的transformers库可以轻松加载预训练的BART模型并进行推理或微调。

1. 安装Hugging Face的Transformers库
pip install transformers
2. 加载预训练的BART模型和分词器
from transformers import BartTokenizer, BartForConditionalGeneration
​
# 加载BART分词器
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large')
​
# 加载预训练的BART模型
model = BartForConditionalGeneration.from_pretrained('facebook/bart-large')
​
# 输入文本
text = "BART is a transformer model designed for text generation tasks."
# 对输入文本进行编码
inputs = tokenizer(text, return_tensors="pt")
​
# 使用模型进行生成任务
summary_ids = model.generate(inputs['input_ids'], max_length=50, num_beams=5, early_stopping=True)
​
# 解码生成的文本
output = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print(f"Generated Text: {output}")

BART处理文本生成任务的示例代码

BART非常适合处理文本生成任务,如摘要生成、翻译、对话生成等。下面的示例展示了如何使用BART生成文本摘要。

示例:文本摘要生成
from transformers import BartTokenizer, BartForConditionalGeneration
​
# 加载BART分词器和预训练的文本摘要模型
tokenizer = BartTokenizer.from_pretrained('facebook/bart-large-cnn')
model = BartForConditionalGeneration.from_pretrained('facebook/bart-large-cnn')
​
# 输入待生成摘要的长文本
text = """
BART is a transformer neural network that has been shown to be highly effective for a variety of natural language processing tasks. 
It is capable of generating coherent and contextually appropriate text and has been particularly useful in applications such as summarization, translation, and text completion.
By utilizing both a bidirectional encoder and an autoregressive decoder, BART can learn to generate text based on a noisy or disrupted input sequence.
"""
​
# 对输入文本进行编码
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
​
# 使用BART进行文本摘要生成
summary_ids = model.generate(inputs['input_ids'], max_length=50, num_beams=4, length_penalty=2.0, early_stopping=True)
​
# 解码生成的摘要文本
summary = tokenizer.decode(summary_ids[0], skip_special_tokens=True)
print(f"Summarized Text: {summary}")
示例:文本翻译(使用多任务训练模型)
from transformers import BartTokenizer, BartForConditionalGeneration
​
# 加载BART分词器和预训练的翻译模型(注:BART可以作为翻译模型,如WMT翻译任务中使用)
tokenizer = BartTokenizer.from_pretrained('facebook/mbart-large-en-ro')
model = BartForConditionalGeneration.from_pretrained('facebook/mbart-large-en-ro')
​
# 输入英文文本
text = "BART is a powerful model for text generation."
inputs = tokenizer(text, return_tensors="pt", max_length=1024, truncation=True)
​
# 指定目标语言为罗马尼亚语(ro)
translated_ids = model.generate(inputs['input_ids'], max_length=50, num_beams=4, early_stopping=True)
​
# 解码生成的翻译文本
translated_text = tokenizer.decode(translated_ids[0], skip_special_tokens=True)
print(f"Translated Text: {translated_text}")

总结

  • BART模型概述:BART是结合了BERT和GPT优势的序列到序列生成模型,广泛用于文本生成任务,如摘要、翻译、对话生成等。

  • 基本结构:由双向编码器(类似BERT)和自回归解码器(类似GPT)组成。通过多种扰动输入的方法进行去噪自编码器训练。

  • 经典代码:使用Hugging Face的transformers库,加载预训练模型并进行推理。

  • 文本生成任务示例:展示了如何使用BART进行文本摘要和翻译任务。

BART(大型型号)

 https://huggingface.co/facebook/bart-large

BART 模型预先训练了英语。它已在 Lewis 等人的论文 [BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension](https://arxiv.org/abs/1910.13461) 中引入,并首次在此[存储库](https://github.com/pytorch/fairseq/tree/master/examples/bart)中发布。

型号描述

BART 是一个变压器编码器-解码器 (seq2seq) 模型,具有一个双向(类似 BERT)编码器和一个自回归(类似 GPT)解码器。BART 的预训练方式是(1)使用任意噪声函数破坏文本,以及(2)学习模型来重建原始文本。

BART 在针对文本生成(例如摘要、翻译)进行微调时特别有效,但也适用于理解任务(例如文本分类、问答)。

预期用途和限制

可以使用 Raw 模型进行文本填充。但是,该模型主要用于在监督数据集上进行微调。

如何使用

以下是在 PyTorch 中使用此模型的方法:
from transformers import BartTokenizer, BartModel

tokenizer = BartTokenizer.from_pretrained('facebook/bart-large')
model = BartModel.from_pretrained('facebook/bart-large')

inputs = tokenizer("Hello, my dog is cute", return_tensors="pt")
outputs = model(**inputs)

last_hidden_states = outputs.last_hidden_state


原文地址:https://blog.csdn.net/GDHBFTGGG/article/details/142818400

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!