"megatron/git@developer.sourcefind.cn:wuxk1/megatron-lm.git" did not exist on "798b6a64c71e8fe28665a5c8cd7a2f6d65e965d6"
bertgeneration.rst 5.07 KB
Newer Older
Sylvain Gugger's avatar
Sylvain Gugger committed
1
2
3
4
5
6
7
8
9
10
11
12
.. 
    Copyright 2020 The HuggingFace Team. All rights reserved.

    Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
    the License. You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
    an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
    specific language governing permissions and limitations under the License.

13
BertGeneration
Sylvain Gugger's avatar
Sylvain Gugger committed
14
-----------------------------------------------------------------------------------------------------------------------
15
16

Overview
Sylvain Gugger's avatar
Sylvain Gugger committed
17
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18

Sylvain Gugger's avatar
Sylvain Gugger committed
19
20
21
The BertGeneration model is a BERT model that can be leveraged for sequence-to-sequence tasks using
:class:`~transformers.EncoderDecoderModel` as proposed in `Leveraging Pre-trained Checkpoints for Sequence Generation
Tasks <https://arxiv.org/abs/1907.12461>`__ by Sascha Rothe, Shashi Narayan, Aliaksei Severyn.
22
23
24

The abstract from the paper is the following:

25
*Unsupervised pretraining of large neural models has recently revolutionized Natural Language Processing. By
Sylvain Gugger's avatar
Sylvain Gugger committed
26
27
28
29
30
31
32
warm-starting from the publicly released checkpoints, NLP practitioners have pushed the state-of-the-art on multiple
benchmarks while saving significant amounts of compute time. So far the focus has been mainly on the Natural Language
Understanding tasks. In this paper, we demonstrate the efficacy of pre-trained checkpoints for Sequence Generation. We
developed a Transformer-based sequence-to-sequence model that is compatible with publicly available pre-trained BERT,
GPT-2 and RoBERTa checkpoints and conducted an extensive empirical study on the utility of initializing our model, both
encoder and decoder, with these checkpoints. Our models result in new state-of-the-art results on Machine Translation,
Text Summarization, Sentence Splitting, and Sentence Fusion.*
33
34
35

Usage:

Sylvain Gugger's avatar
Sylvain Gugger committed
36
37
- The model can be used in combination with the :class:`~transformers.EncoderDecoderModel` to leverage two pretrained
  BERT checkpoints for subsequent fine-tuning.
38

Sylvain Gugger's avatar
Sylvain Gugger committed
39
40
.. code-block::

41
  # leverage checkpoints for Bert2Bert model...
Sylvain Gugger's avatar
Sylvain Gugger committed
42
43
44
45
  # use BERT's cls token as BOS token and sep token as EOS token
  encoder = BertGenerationEncoder.from_pretrained("bert-large-uncased", bos_token_id=101, eos_token_id=102)
  # add cross attention layers and use BERT's cls token as BOS token and sep token as EOS token
  decoder = BertGenerationDecoder.from_pretrained("bert-large-uncased", add_cross_attention=True, is_decoder=True, bos_token_id=101, eos_token_id=102)
46
  bert2bert = EncoderDecoderModel(encoder=encoder, decoder=decoder)
Sylvain Gugger's avatar
Sylvain Gugger committed
47

48
49
50
51
52
53
54
  # create tokenizer...
  tokenizer = BertTokenizer.from_pretrained("bert-large-uncased")

  input_ids = tokenizer('This is a long article to summarize', add_special_tokens=False, return_tensors="pt").input_ids
  labels = tokenizer('This is a short summary', return_tensors="pt").input_ids

  # train...
55
  loss = bert2bert(input_ids=input_ids, decoder_input_ids=labels, labels=labels).loss
56
57
58
  loss.backward()


Sylvain Gugger's avatar
Sylvain Gugger committed
59
- Pretrained :class:`~transformers.EncoderDecoderModel` are also directly available in the model hub, e.g.,
60
61


62
.. code-block::
63
64
65
66
67
68
69
70
71
72
73
74
75
76

  # instantiate sentence fusion model
  sentence_fuser = EncoderDecoderModel.from_pretrained("google/roberta2roberta_L-24_discofuse")
  tokenizer = AutoTokenizer.from_pretrained("google/roberta2roberta_L-24_discofuse")

  input_ids = tokenizer('This is the first sentence. This is the second sentence.', add_special_tokens=False, return_tensors="pt").input_ids

  outputs = sentence_fuser.generate(input_ids)

  print(tokenizer.decode(outputs[0]))


Tips:

Sylvain Gugger's avatar
Sylvain Gugger committed
77
78
79
80
- :class:`~transformers.BertGenerationEncoder` and :class:`~transformers.BertGenerationDecoder` should be used in
  combination with :class:`~transformers.EncoderDecoder`.
- For summarization, sentence splitting, sentence fusion and translation, no special tokens are required for the input.
  Therefore, no EOS token should be added to the end of the input.
81
82
83
84

The original code can be found `here <https://tfhub.dev/s?module-type=text-generation&subtype=module,placeholder>`__.

BertGenerationConfig
Sylvain Gugger's avatar
Sylvain Gugger committed
85
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86
87
88
89
90
91

.. autoclass:: transformers.BertGenerationConfig
    :members:


BertGenerationTokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
92
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93
94

.. autoclass:: transformers.BertGenerationTokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
95
    :members: save_vocabulary
96
97

BertGenerationEncoder
Sylvain Gugger's avatar
Sylvain Gugger committed
98
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
99
100

.. autoclass:: transformers.BertGenerationEncoder
Sylvain Gugger's avatar
Sylvain Gugger committed
101
    :members: forward
102
103
104


BertGenerationDecoder
Sylvain Gugger's avatar
Sylvain Gugger committed
105
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
106
107

.. autoclass:: transformers.BertGenerationDecoder
Sylvain Gugger's avatar
Sylvain Gugger committed
108
    :members: forward