|
| 1 | +# BART: Denoising Sequence-to-Sequence Pre-training for Natural Language Generation, Translation, and Comprehension |
| 2 | + |
| 3 | +[https://arxiv.org/pdf/1910.13461.pdf] |
| 4 | + |
| 5 | +## Introduction |
| 6 | + |
| 7 | +BART is sequence-to-sequence model trained with denoising as pretraining objective. We show that this pretraining objective is more generic and show that we can match [RoBERTa](../roberta) Results on SQuAD and GLUE and gain state-of-the-art results on summarization (XSum, CNN dataset), long form generative question answering (ELI5) and dialog response genration (ConvAI2). See the associated paper for more details. |
| 8 | + |
| 9 | +## Pre-trained models |
| 10 | + |
| 11 | +Model | Description | # params | Download |
| 12 | +---|---|---|--- |
| 13 | +`bart.large` | BART model with 12 encoder and decoder layers | 400M | [bart.large.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/bart.large.tar.gz) |
| 14 | +`bart.large.mnli` | `bart.large` finetuned on `MNLI` | 400M | [bart.large.mnli.tar.gz](https://dl.fbaipublicfiles.com/fairseq/models/bart.large.mnli.tar.gz) |
| 15 | + |
| 16 | +## Results |
| 17 | + |
| 18 | +**[GLUE (Wang et al., 2019)](https://gluebenchmark.com/)** |
| 19 | +_(dev set, single model, single-task finetuning)_ |
| 20 | + |
| 21 | +Model | MNLI | QNLI | QQP | RTE | SST-2 | MRPC | CoLA | STS-B |
| 22 | +---|---|---|---|---|---|---|---|--- |
| 23 | +`roberta.large` | 90.2 | 94.7 | 92.2 | 86.6 | 96.4 | 90.9 | 68.0 | 92.4 |
| 24 | +`bart.large` | 89.9 | 94.9 | 92.5 | 87.0 | 96.6 | 90.4 | 62.8 | 91.2 |
| 25 | + |
| 26 | +**[SQuAD (Rajpurkar et al., 2018)](https://rajpurkar.github.io/SQuAD-explorer/)** |
| 27 | +_(dev set, no additional data used)_ |
| 28 | + |
| 29 | +Model | SQuAD 1.1 EM/F1 | SQuAD 2.0 EM/F1 |
| 30 | +---|---|--- |
| 31 | +`roberta.large` | 88.9/94.6 | 86.5/89.4 |
| 32 | +`bart.large` | 88.8/94.6 | 86.1/89.2 |
| 33 | + |
| 34 | +**[CNN/Daily Mail](http://nlpprogress.com/english/summarization.html)** |
| 35 | +_(dev set, no additional data used)_ |
| 36 | + |
| 37 | +Model | R1 | R2 | RL |
| 38 | +---|---|---|--- |
| 39 | +`BERTSUMEXTABS` | 42.13 | 19.60 | 39.18 |
| 40 | +`bart.large` | 44.16 | 21.28 | 40.90 |
| 41 | + |
| 42 | +## Example usage |
| 43 | + |
| 44 | +##### Load BART from torch.hub (PyTorch >= 1.1): |
| 45 | +```python |
| 46 | +import torch |
| 47 | +bart = torch.hub.load('pytorch/fairseq', 'bart.large') |
| 48 | +bart.eval() # disable dropout (or leave in train mode to finetune) |
| 49 | +``` |
| 50 | + |
| 51 | +##### Load BART (for PyTorch 1.0 or custom models): |
| 52 | +```python |
| 53 | +# Download bart.large model |
| 54 | +wget https://dl.fbaipublicfiles.com/fairseq/models/bart.large.tar.gz |
| 55 | +tar -xzvf bart.large.tar.gz |
| 56 | + |
| 57 | +# Load the model in fairseq |
| 58 | +from fairseq.models.bart import BARTModel |
| 59 | +bart = BARTModel.from_pretrained('/path/to/bart.large', checkpoint_file='model.pt') |
| 60 | +bart.eval() # disable dropout (or leave in train mode to finetune) |
| 61 | +``` |
| 62 | + |
| 63 | +##### Apply Byte-Pair Encoding (BPE) to input text: |
| 64 | +```python |
| 65 | +tokens = bart.encode('Hello world!') |
| 66 | +assert tokens.tolist() == [0, 31414, 232, 328, 2] |
| 67 | +bart.decode(tokens) # 'Hello world!' |
| 68 | +``` |
| 69 | + |
| 70 | +##### Extract features from BART: |
| 71 | +```python |
| 72 | +# Extract the last layer's features |
| 73 | +last_layer_features = bart.extract_features(tokens) |
| 74 | +assert last_layer_features.size() == torch.Size([1, 5, 1024]) |
| 75 | + |
| 76 | +# Extract all layer's features from decoder (layer 0 is the embedding layer) |
| 77 | +all_layers = bart.extract_features(tokens, return_all_hiddens=True) |
| 78 | +assert len(all_layers) == 13 |
| 79 | +assert torch.all(all_layers[-1] == last_layer_features) |
| 80 | +``` |
| 81 | + |
| 82 | +##### Use BART for sentence-pair classification tasks: |
| 83 | +```python |
| 84 | +# Download BART already finetuned for MNLI |
| 85 | +bart = torch.hub.load('pytorch/fairseq', 'bart.large.mnli') |
| 86 | +bart.eval() # disable dropout for evaluation |
| 87 | + |
| 88 | +# Encode a pair of sentences and make a prediction |
| 89 | +tokens = bart.encode('BART is a seq2seq model.', 'BART is not sequence to sequence.') |
| 90 | +bart.predict('mnli', tokens).argmax() # 0: contradiction |
| 91 | + |
| 92 | +# Encode another pair of sentences |
| 93 | +tokens = bart.encode('BART is denoising autoencoder.', 'BART is version of autoencoder.') |
| 94 | +bart.predict('mnli', tokens).argmax() # 2: entailment |
| 95 | +``` |
| 96 | + |
| 97 | +##### Register a new (randomly initialized) classification head: |
| 98 | +```python |
| 99 | +bart.register_classification_head('new_task', num_classes=3) |
| 100 | +logprobs = bart.predict('new_task', tokens) |
| 101 | +``` |
| 102 | + |
| 103 | +##### Batched prediction: |
| 104 | +```python |
| 105 | +import torch |
| 106 | +from fairseq.data.data_utils import collate_tokens |
| 107 | + |
| 108 | +bart = torch.hub.load('pytorch/fairseq', 'bart.large.mnli') |
| 109 | +bart.eval() |
| 110 | + |
| 111 | +batch_of_pairs = [ |
| 112 | + ['BART is a seq2seq model.', 'BART is not sequence to sequence.'], |
| 113 | + ['BART is denoising autoencoder.', 'BART is version of autoencoder.'], |
| 114 | +] |
| 115 | + |
| 116 | +batch = collate_tokens( |
| 117 | + [bart.encode(pair[0], pair[1]) for pair in batch_of_pairs], pad_idx=1 |
| 118 | +) |
| 119 | + |
| 120 | +logprobs = bart.predict('mnli', batch) |
| 121 | +print(logprobs.argmax(dim=1)) |
| 122 | +# tensor([0, 2]) |
| 123 | +``` |
| 124 | + |
| 125 | +##### Using the GPU: |
| 126 | +```python |
| 127 | +bart.cuda() |
| 128 | +bart.predict('new_task', tokens) |
| 129 | +``` |
| 130 | + |
| 131 | +#### Evaluating the `bart.large.mnli` model: |
| 132 | + |
| 133 | +Example python code snippet to evaluate accuracy on the MNLI `dev_matched` set. |
| 134 | +```python |
| 135 | +label_map = {0: 'contradiction', 1: 'neutral', 2: 'entailment'} |
| 136 | +ncorrect, nsamples = 0, 0 |
| 137 | +bart.cuda() |
| 138 | +bart.eval() |
| 139 | +with open('glue_data/MNLI/dev_matched.tsv') as fin: |
| 140 | + fin.readline() |
| 141 | + for index, line in enumerate(fin): |
| 142 | + tokens = line.strip().split('\t') |
| 143 | + sent1, sent2, target = tokens[8], tokens[9], tokens[-1] |
| 144 | + tokens = bart.encode(sent1, sent2) |
| 145 | + prediction = bart.predict('mnli', tokens).argmax().item() |
| 146 | + prediction_label = label_map[prediction] |
| 147 | + ncorrect += int(prediction_label == target) |
| 148 | + nsamples += 1 |
| 149 | + print('| Accuracy: ', float(ncorrect)/float(nsamples)) |
| 150 | +# Expected output: 0.9010 |
| 151 | +``` |
| 152 | + |
| 153 | +## Finetuning |
| 154 | + |
| 155 | +- [Finetuning on GLUE](README.glue.md) |
| 156 | + |
| 157 | +## Citation |
| 158 | + |
| 159 | +```bibtex |
| 160 | +@article{lewis2019bart, |
| 161 | + title = {BART: Denoising Sequence-to-Sequence Pre-training for Natural |
| 162 | +Language Generation, Translation, and Comprehension}, |
| 163 | + author = {Mike Lewis and Yinhan Liu and Naman Goyal and Marjan Ghazvininejad and |
| 164 | + Abdelrahman Mohamed and Omer Levy and Veselin Stoyanov |
| 165 | + and Luke Zettlemoyer }, |
| 166 | + journal={arXiv preprint arXiv:1910.13461}, |
| 167 | + year = {2019}, |
| 168 | +} |
| 169 | +``` |
0 commit comments