gpt2.rst 6.34 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
- The PyTorch models can take the `past` as input, which is the previously computed key/value attention pairs. Using
Sylvain Gugger's avatar
Sylvain Gugger committed
40
41
42
  this `past` value prevents the model from re-computing pre-computed values in the context of text generation. See
  `reusing the past in generative models <../quickstart.html#using-the-past>`__ for more information on the usage of
  this argument.
43

Lysandre's avatar
TF GPT2  
Lysandre committed
44
45
`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
46
different sizes: small, medium, large, xl and a distilled version of the small checkpoint: `distilgpt-2`.
Lysandre's avatar
TF GPT2  
Lysandre committed
47

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

51

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

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


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

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


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

.. autoclass:: transformers.GPT2TokenizerFast
    :members:


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

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

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

82

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

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


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

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


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

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


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

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


Lysandre's avatar
Lysandre committed
111
TFGPT2Model
Sylvain Gugger's avatar
Sylvain Gugger committed
112
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LysandreJik's avatar
LysandreJik committed
113

114
.. autoclass:: transformers.TFGPT2Model
Sylvain Gugger's avatar
Sylvain Gugger committed
115
    :members: call
LysandreJik's avatar
LysandreJik committed
116
117


Lysandre's avatar
Lysandre committed
118
TFGPT2LMHeadModel
Sylvain Gugger's avatar
Sylvain Gugger committed
119
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LysandreJik's avatar
LysandreJik committed
120

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


Lysandre's avatar
Lysandre committed
125
TFGPT2DoubleHeadsModel
Sylvain Gugger's avatar
Sylvain Gugger committed
126
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
LysandreJik's avatar
LysandreJik committed
127

128
.. autoclass:: transformers.TFGPT2DoubleHeadsModel
Sylvain Gugger's avatar
Sylvain Gugger committed
129
    :members: call
130
131
132
133
134
135
136
137
138
139
140
141

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

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

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

.. autoclass:: transformers.modeling_tf_outputs.TFSequenceClassifierOutputWithPast
    :members:
Suraj Patil's avatar
Suraj Patil committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155


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

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


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

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