gpt2.rst 6.7 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
OpenAI GPT2
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
OpenAI GPT-2 model was proposed in `Language Models are Unsupervised Multitask Learners
Sylvain Gugger's avatar
Sylvain Gugger committed
20
21
22
<https://cdn.openai.com/better-language-models/language_models_are_unsupervised_multitask_learners.pdf>`_ by Alec
Radford, Jeffrey Wu, Rewon Child, David Luan, Dario Amodei and Ilya Sutskever. It's a causal (unidirectional)
transformer pretrained using language modeling on a very large corpus of ~40 GB of text data.
23
24
25

The abstract from the paper is the following:

Sylvain Gugger's avatar
Sylvain Gugger committed
26
27
28
29
30
*GPT-2 is a large transformer-based language model with 1.5 billion parameters, trained on a dataset[1] of 8 million
web pages. GPT-2 is trained with a simple objective: predict the next word, given all of the previous words within some
text. The diversity of the dataset causes this simple goal to contain naturally occurring demonstrations of many tasks
across diverse domains. GPT-2 is a direct scale-up of GPT, with more than 10X the parameters and trained on more than
10X the amount of data.*
31
32
33

Tips:

Sylvain Gugger's avatar
Sylvain Gugger committed
34
35
- GPT-2 is a model with absolute position embeddings so it's usually advised to pad the inputs on the right rather than
  the left.
36
- GPT-2 was trained with a causal language modeling (CLM) objective and is therefore powerful at predicting the next
Sylvain Gugger's avatar
Sylvain Gugger committed
37
38
  token in a sequence. Leveraging this feature allows GPT-2 to generate syntactically coherent text as it can be
  observed in the `run_generation.py` example script.
39
40
41
42
43
- The model can take the `past_key_values` (for PyTorch) or `past` (for TF) as input, which is the previously computed
  key/value attention pairs. Using this (`past_key_values` or `past`) value prevents the model from re-computing
  pre-computed values in the context of text generation. For PyTorch, see `past_key_values` argument of the
  :meth:`~transformers.GPT2Model.forward` method, or for TF the `past` argument of the
  :meth:`~transformers.TFGPT2Model.call` method for more information on its usage.
44

Lysandre's avatar
TF GPT2  
Lysandre committed
45
46
`Write With Transformer <https://transformer.huggingface.co/doc/gpt2-large>`__ is a webapp created and hosted by
Hugging Face showcasing the generative capabilities of several models. GPT-2 is one of them and is available in five
Sylvain Gugger's avatar
Sylvain Gugger committed
47
different sizes: small, medium, large, xl and a distilled version of the small checkpoint: `distilgpt-2`.
Lysandre's avatar
TF GPT2  
Lysandre committed
48

49
50
This model was contributed by `thomwolf <https://huggingface.co/thomwolf>`__. The original code can be found `here
<https://openai.com/blog/better-language-models/>`__.
51

52

Lysandre's avatar
Lysandre committed
53
GPT2Config
Sylvain Gugger's avatar
Sylvain Gugger committed
54
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
55

56
.. autoclass:: transformers.GPT2Config
57
    :members:
58
59


Lysandre's avatar
Lysandre committed
60
GPT2Tokenizer
Sylvain Gugger's avatar
Sylvain Gugger committed
61
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
62

63
.. autoclass:: transformers.GPT2Tokenizer
Lysandre Debut's avatar
Lysandre Debut committed
64
    :members: save_vocabulary
65
66


67
GPT2TokenizerFast
Sylvain Gugger's avatar
Sylvain Gugger committed
68
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69
70
71
72
73

.. autoclass:: transformers.GPT2TokenizerFast
    :members:


74
GPT2 specific outputs
Sylvain Gugger's avatar
Sylvain Gugger committed
75
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76

Sylvain Gugger's avatar
Sylvain Gugger committed
77
.. autoclass:: transformers.models.gpt2.modeling_gpt2.GPT2DoubleHeadsModelOutput
78
79
    :members:

Sylvain Gugger's avatar
Sylvain Gugger committed
80
.. autoclass:: transformers.models.gpt2.modeling_tf_gpt2.TFGPT2DoubleHeadsModelOutput
Sylvain Gugger's avatar
Sylvain Gugger committed
81
82
    :members:

83

Lysandre's avatar
Lysandre committed
84
GPT2Model
Sylvain Gugger's avatar
Sylvain Gugger committed
85
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
86

87
.. autoclass:: transformers.GPT2Model
88
    :members: forward, parallelize, deparallelize
89
90


Lysandre's avatar
Lysandre committed
91
GPT2LMHeadModel
Sylvain Gugger's avatar
Sylvain Gugger committed
92
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
93

94
.. autoclass:: transformers.GPT2LMHeadModel
95
    :members: forward, parallelize, deparallelize
96
97


Lysandre's avatar
Lysandre committed
98
GPT2DoubleHeadsModel
Sylvain Gugger's avatar
Sylvain Gugger committed
99
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
100

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


105
106
107
108
109
110
111
GPT2ForSequenceClassification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.GPT2ForSequenceClassification
    :members: forward


112
113
114
115
116
117
118
GPT2ForTokenClassification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.GPT2ForTokenClassification
    :members: forward


Lysandre's avatar
Lysandre committed
119
TFGPT2Model
Sylvain Gugger's avatar
Sylvain Gugger committed
120
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LysandreJik's avatar
LysandreJik committed
121

122
.. autoclass:: transformers.TFGPT2Model
Sylvain Gugger's avatar
Sylvain Gugger committed
123
    :members: call
LysandreJik's avatar
LysandreJik committed
124
125


Lysandre's avatar
Lysandre committed
126
TFGPT2LMHeadModel
Sylvain Gugger's avatar
Sylvain Gugger committed
127
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LysandreJik's avatar
LysandreJik committed
128

129
.. autoclass:: transformers.TFGPT2LMHeadModel
Sylvain Gugger's avatar
Sylvain Gugger committed
130
    :members: call
LysandreJik's avatar
LysandreJik committed
131
132


Lysandre's avatar
Lysandre committed
133
TFGPT2DoubleHeadsModel
Sylvain Gugger's avatar
Sylvain Gugger committed
134
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LysandreJik's avatar
LysandreJik committed
135

136
.. autoclass:: transformers.TFGPT2DoubleHeadsModel
Sylvain Gugger's avatar
Sylvain Gugger committed
137
    :members: call
138
139
140
141
142
143
144
145
146
147
148
149

TFGPT2ForSequenceClassification
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.TFGPT2ForSequenceClassification
    :members: call

TFSequenceClassifierOutputWithPast
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.modeling_tf_outputs.TFSequenceClassifierOutputWithPast
    :members:
Suraj Patil's avatar
Suraj Patil committed
150
151
152
153
154
155
156
157
158
159
160
161
162
163


FlaxGPT2Model
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.FlaxGPT2Model
    :members: __call__


FlaxGPT2LMHeadModel
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

.. autoclass:: transformers.FlaxGPT2LMHeadModel
    :members: __call__