Compare commits
11 Commits
christophe
...
master
Author | SHA1 | Date | |
---|---|---|---|
|
a74da5d99a | ||
|
0574c5708b | ||
|
03fce0a080 | ||
|
0f97760ebe | ||
|
ebdba20a19 | ||
|
d98291d2ae | ||
|
fbae7db92a | ||
|
ac5d52295f | ||
|
f35fa1d920 | ||
|
cb415376c3 | ||
|
e9378792c4 |
@@ -27,8 +27,10 @@ pip3 install -r requirements.txt
|
|||||||
|
|
||||||
Download the model data
|
Download the model data
|
||||||
```
|
```
|
||||||
python3 download_model.py 117M
|
python3 download_model.py 124M
|
||||||
python3 download_model.py 345M
|
python3 download_model.py 355M
|
||||||
|
python3 download_model.py 774M
|
||||||
|
python3 download_model.py 1558M
|
||||||
```
|
```
|
||||||
|
|
||||||
## Docker Installation
|
## Docker Installation
|
||||||
|
@@ -5,5 +5,7 @@ RUN mkdir /gpt-2
|
|||||||
WORKDIR /gpt-2
|
WORKDIR /gpt-2
|
||||||
ADD . /gpt-2
|
ADD . /gpt-2
|
||||||
RUN pip3 install -r requirements.txt
|
RUN pip3 install -r requirements.txt
|
||||||
RUN python3 download_model.py 117M
|
RUN python3 download_model.py 124M
|
||||||
RUN python3 download_model.py 345M
|
RUN python3 download_model.py 355M
|
||||||
|
RUN python3 download_model.py 774M
|
||||||
|
RUN python3 download_model.py 1558M
|
||||||
|
@@ -14,5 +14,7 @@ RUN mkdir /gpt-2
|
|||||||
WORKDIR /gpt-2
|
WORKDIR /gpt-2
|
||||||
ADD . /gpt-2
|
ADD . /gpt-2
|
||||||
RUN pip3 install -r requirements.txt
|
RUN pip3 install -r requirements.txt
|
||||||
RUN python3 download_model.py 117M
|
RUN python3 download_model.py 124M
|
||||||
RUN python3 download_model.py 345M
|
RUN python3 download_model.py 355M
|
||||||
|
RUN python3 download_model.py 774M
|
||||||
|
RUN python3 download_model.py 1558M
|
||||||
|
37
LICENSE
37
LICENSE
@@ -1,21 +1,24 @@
|
|||||||
MIT License
|
Modified MIT License
|
||||||
|
|
||||||
Copyright (c) 2019 OpenAI
|
Software Copyright (c) 2019 OpenAI
|
||||||
|
|
||||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
We don’t claim ownership of the content you create with GPT-2, so it is yours to do with as you please.
|
||||||
of this software and associated documentation files (the "Software"), to deal
|
We only ask that you use GPT-2 responsibly and clearly indicate your content was created using GPT-2.
|
||||||
in the Software without restriction, including without limitation the rights
|
|
||||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
||||||
copies of the Software, and to permit persons to whom the Software is
|
|
||||||
furnished to do so, subject to the following conditions:
|
|
||||||
|
|
||||||
The above copyright notice and this permission notice shall be included in all
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and
|
||||||
copies or substantial portions of the Software.
|
associated documentation files (the "Software"), to deal in the Software without restriction,
|
||||||
|
including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
|
||||||
|
and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so,
|
||||||
|
subject to the following conditions:
|
||||||
|
|
||||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
The above copyright notice and this permission notice shall be included
|
||||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
in all copies or substantial portions of the Software.
|
||||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
The above copyright notice and this permission notice need not be included
|
||||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
with content created by the Software.
|
||||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
||||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
|
||||||
SOFTWARE.
|
INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||||
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
|
||||||
|
BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
||||||
|
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
||||||
|
OR OTHER DEALINGS IN THE SOFTWARE.
|
||||||
|
12
README.md
12
README.md
@@ -2,16 +2,20 @@
|
|||||||
|
|
||||||
# gpt-2
|
# gpt-2
|
||||||
|
|
||||||
Code from the paper ["Language Models are Unsupervised Multitask Learners"](https://d4mucfpksywv.cloudfront.net/better-language-models/language-models.pdf).
|
Code and models from the paper ["Language Models are Unsupervised Multitask Learners"](https://d4mucfpksywv.cloudfront.net/better-language-models/language-models.pdf).
|
||||||
|
|
||||||
We have currently released small (117M parameter) and medium (345M parameter) versions of GPT-2. While we have not released the larger models, we have [released a dataset](https://github.com/openai/gpt-2-output-dataset) for researchers to study their behaviors.
|
You can read about GPT-2 and its staged release in our [original blog post](https://blog.openai.com/better-language-models/), [6 month follow-up post](https://openai.com/blog/gpt-2-6-month-follow-up/), and [final post](https://www.openai.com/blog/gpt-2-1-5b-release/).
|
||||||
|
|
||||||
See more details in our [blog post](https://blog.openai.com/better-language-models/).
|
We have also [released a dataset](https://github.com/openai/gpt-2-output-dataset) for researchers to study their behaviors.
|
||||||
|
|
||||||
|
<sup>*</sup> *Note that our original parameter counts were wrong due to an error (in our previous blog posts and paper). Thus you may have seen small referred to as 117M and medium referred to as 345M.*
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
|
|
||||||
This repository is meant to be a starting point for researchers and engineers to experiment with GPT-2.
|
This repository is meant to be a starting point for researchers and engineers to experiment with GPT-2.
|
||||||
|
|
||||||
|
For basic information, see our [model card](./model_card.md).
|
||||||
|
|
||||||
### Some caveats
|
### Some caveats
|
||||||
|
|
||||||
- GPT-2 models' robustness and worst case behaviors are not well-understood. As with any machine-learned model, carefully evaluate GPT-2 for your use case, especially if used without fine-tuning or in safety-critical applications where reliability is important.
|
- GPT-2 models' robustness and worst case behaviors are not well-understood. As with any machine-learned model, carefully evaluate GPT-2 for your use case, especially if used without fine-tuning or in safety-critical applications where reliability is important.
|
||||||
@@ -51,4 +55,4 @@ We are still considering release of the larger models.
|
|||||||
|
|
||||||
## License
|
## License
|
||||||
|
|
||||||
[MIT](./LICENSE)
|
[Modified MIT](./LICENSE)
|
||||||
|
1000
domains.txt
Normal file
1000
domains.txt
Normal file
File diff suppressed because it is too large
Load Diff
@@ -4,7 +4,7 @@ import requests
|
|||||||
from tqdm import tqdm
|
from tqdm import tqdm
|
||||||
|
|
||||||
if len(sys.argv) != 2:
|
if len(sys.argv) != 2:
|
||||||
print('You must enter the model name as a parameter, e.g.: download_model.py 117M')
|
print('You must enter the model name as a parameter, e.g.: download_model.py 124M')
|
||||||
sys.exit(1)
|
sys.exit(1)
|
||||||
|
|
||||||
model = sys.argv[1]
|
model = sys.argv[1]
|
||||||
@@ -16,7 +16,7 @@ subdir = subdir.replace('\\','/') # needed for Windows
|
|||||||
|
|
||||||
for filename in ['checkpoint','encoder.json','hparams.json','model.ckpt.data-00000-of-00001', 'model.ckpt.index', 'model.ckpt.meta', 'vocab.bpe']:
|
for filename in ['checkpoint','encoder.json','hparams.json','model.ckpt.data-00000-of-00001', 'model.ckpt.index', 'model.ckpt.meta', 'vocab.bpe']:
|
||||||
|
|
||||||
r = requests.get("https://storage.googleapis.com/gpt-2/" + subdir + "/" + filename, stream=True)
|
r = requests.get("https://openaipublic.blob.core.windows.net/gpt-2/" + subdir + "/" + filename, stream=True)
|
||||||
|
|
||||||
with open(os.path.join(subdir, filename), 'wb') as f:
|
with open(os.path.join(subdir, filename), 'wb') as f:
|
||||||
file_size = int(r.headers["content-length"])
|
file_size = int(r.headers["content-length"])
|
||||||
|
69
model_card.md
Normal file
69
model_card.md
Normal file
@@ -0,0 +1,69 @@
|
|||||||
|
# GPT-2 model card
|
||||||
|
|
||||||
|
Last updated: November 2019
|
||||||
|
|
||||||
|
Inspired by [Model Cards for Model Reporting (Mitchell et al.)](https://arxiv.org/abs/1810.03993), we’re providing some accompanying information about the GPT-2 family of models we're releasing.
|
||||||
|
|
||||||
|
## Model Details.
|
||||||
|
|
||||||
|
This model was developed by researchers at OpenAI to help us understand how the capabilities of language model capabilities scale as a function of the size of the models (by parameter count) combined with very large internet-scale datasets (WebText).
|
||||||
|
|
||||||
|
### Model date
|
||||||
|
|
||||||
|
February 2019, trained on data that cuts off at the end of 2017.
|
||||||
|
|
||||||
|
### Model type
|
||||||
|
|
||||||
|
Language model
|
||||||
|
|
||||||
|
### Model version
|
||||||
|
|
||||||
|
1.5 billion parameters: the fourth and largest GPT-2 version. We have also released 124 million, 355 million, and 774 million parameter models.
|
||||||
|
|
||||||
|
### Paper or other resource for more information
|
||||||
|
[Blog post](https://openai.com/blog/better-language-models/) and [paper](https://d4mucfpksywv.cloudfront.net/better-language-models/language_models_are_unsupervised_multitask_learners.pdf)
|
||||||
|
|
||||||
|
### Where to send questions or comments about the model
|
||||||
|
Please use this [Google Form](https://forms.gle/A7WBSbTY2EkKdroPA)
|
||||||
|
|
||||||
|
## Intended Uses:
|
||||||
|
|
||||||
|
### Primary intended uses
|
||||||
|
|
||||||
|
The primary intended users of these models are *AI researchers and practitioners*.
|
||||||
|
|
||||||
|
We primarily imagine these language models will be used by researchers to better understand the behaviors, capabilities, biases, and constraints of large-scale generative language models.
|
||||||
|
|
||||||
|
### Secondary uses
|
||||||
|
|
||||||
|
Here are some secondary use cases we believe are likely:
|
||||||
|
|
||||||
|
- **Writing assistance**: Grammar assistance, autocompletion (for normal prose or code)
|
||||||
|
- **Creative writing and art**: exploring the generation of creative, fictional texts; aiding creation of poetry and other literary art.
|
||||||
|
- **Entertainment**: Creation of games, chat bots, and amusing generations.
|
||||||
|
|
||||||
|
### Out-of-scope use cases
|
||||||
|
|
||||||
|
Because large-scale language models like GPT-2 do not distinguish fact from fiction, we don’t support use-cases that require the generated text to be true.
|
||||||
|
|
||||||
|
Additionally, language models like GPT-2 reflect the biases inherent to the systems they were trained on, so we do not recommend that they be deployed into systems that interact with humans unless the deployers first carry out a study of biases relevant to the intended use-case. We found no statistically significant difference in gender, race, and religious bias probes between 774M and 1.5B, implying all versions of GPT-2 should be approached with similar levels of caution around use cases that are sensitive to biases around human attributes.
|
||||||
|
|
||||||
|
## Evaluation Data
|
||||||
|
|
||||||
|
### Datasets
|
||||||
|
|
||||||
|
This model was trained on (and evaluated against) WebText, a dataset consisting of the text contents of 45 million links posted by users of the ‘Reddit’ social network. WebText is made of data derived from outbound links from Reddit and does not consist of data taken directly from Reddit itself. Before generating the dataset we used a blocklist to ensure we didn’t sample from a variety of subreddits which contain sexually explicit or otherwise offensive content.
|
||||||
|
|
||||||
|
To get a sense of the data that went into GPT-2, we’ve [published a list](domains.txt) of the top 1,000 domains present in WebText and their frequency. The top 15 domains by volume in WebText are: Google, Archive, Blogspot, GitHub, NYTimes, Wordpress, Washington Post, Wikia, BBC, The Guardian, eBay, Pastebin, CNN, Yahoo!, and the Huffington Post.
|
||||||
|
|
||||||
|
### Motivation
|
||||||
|
|
||||||
|
The motivation behind WebText was to create an Internet-scale, heterogeneous dataset that we could use to test large-scale language models against. WebText was (and is) intended to be primarily for research purposes rather than production purposes.
|
||||||
|
|
||||||
|
### Caveats and Recommendations
|
||||||
|
|
||||||
|
Because GPT-2 is an internet-scale language model, it’s currently difficult to know what disciplined testing procedures can be applied to it to fully understand its capabilities and how the data it is trained on influences its vast range of outputs. We recommend researchers investigate these aspects of the model and share their results.
|
||||||
|
|
||||||
|
Additionally, as indicated in our discussion of issues relating to potential misuse of the model, it remains unclear what the long-term dynamics are of detecting outputs from these models. We conducted [in-house automated ML-based detection research](https://github.com/openai/gpt-2-output-dataset/tree/master/detector) using simple classifiers, zero shot, and fine-tuning methods. Our fine-tuned detector model reached accuracy levels of approximately 95%. However, no one detection method is a panacea; automated ML-based detection, human detection, human-machine teaming, and metadata-based detection are all methods that can be combined for more confident classification. Developing better approaches to detection today will give us greater intuitions when thinking about future models and could help us understand ahead of time if detection methods will eventually become ineffective.
|
||||||
|
|
||||||
|
|
@@ -9,18 +9,19 @@ import tensorflow as tf
|
|||||||
import model, sample, encoder
|
import model, sample, encoder
|
||||||
|
|
||||||
def sample_model(
|
def sample_model(
|
||||||
model_name='117M',
|
model_name='124M',
|
||||||
seed=None,
|
seed=None,
|
||||||
nsamples=0,
|
nsamples=0,
|
||||||
batch_size=1,
|
batch_size=1,
|
||||||
length=None,
|
length=None,
|
||||||
temperature=1,
|
temperature=1,
|
||||||
top_k=0,
|
top_k=0,
|
||||||
|
top_p=1,
|
||||||
models_dir='models',
|
models_dir='models',
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Run the sample_model
|
Run the sample_model
|
||||||
:model_name=117M : String, which model to use
|
:model_name=124M : String, which model to use
|
||||||
:seed=None : Integer seed for random number generators, fix seed to
|
:seed=None : Integer seed for random number generators, fix seed to
|
||||||
reproduce results
|
reproduce results
|
||||||
:nsamples=0 : Number of samples to return, if 0, continues to
|
:nsamples=0 : Number of samples to return, if 0, continues to
|
||||||
@@ -58,7 +59,7 @@ def sample_model(
|
|||||||
hparams=hparams, length=length,
|
hparams=hparams, length=length,
|
||||||
start_token=enc.encoder['<|endoftext|>'],
|
start_token=enc.encoder['<|endoftext|>'],
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
temperature=temperature, top_k=top_k
|
temperature=temperature, top_k=top_k, top_p=top_p
|
||||||
)[:, 1:]
|
)[:, 1:]
|
||||||
|
|
||||||
saver = tf.train.Saver()
|
saver = tf.train.Saver()
|
||||||
|
@@ -9,18 +9,19 @@ import tensorflow as tf
|
|||||||
import model, sample, encoder
|
import model, sample, encoder
|
||||||
|
|
||||||
def interact_model(
|
def interact_model(
|
||||||
model_name='117M',
|
model_name='124M',
|
||||||
seed=None,
|
seed=None,
|
||||||
nsamples=1,
|
nsamples=1,
|
||||||
batch_size=1,
|
batch_size=1,
|
||||||
length=None,
|
length=None,
|
||||||
temperature=1,
|
temperature=1,
|
||||||
top_k=0,
|
top_k=0,
|
||||||
models_dir='models',
|
top_p=1,
|
||||||
|
models_dir='models',
|
||||||
):
|
):
|
||||||
"""
|
"""
|
||||||
Interactively run the model
|
Interactively run the model
|
||||||
:model_name=117M : String, which model to use
|
:model_name=124M : String, which model to use
|
||||||
:seed=None : Integer seed for random number generators, fix seed to reproduce
|
:seed=None : Integer seed for random number generators, fix seed to reproduce
|
||||||
results
|
results
|
||||||
:nsamples=1 : Number of samples to return total
|
:nsamples=1 : Number of samples to return total
|
||||||
@@ -36,7 +37,7 @@ def interact_model(
|
|||||||
while 40 means 40 words are considered at each step. 0 (default) is a
|
while 40 means 40 words are considered at each step. 0 (default) is a
|
||||||
special setting meaning no restrictions. 40 generally is a good value.
|
special setting meaning no restrictions. 40 generally is a good value.
|
||||||
:models_dir : path to parent folder containing model subfolders
|
:models_dir : path to parent folder containing model subfolders
|
||||||
(i.e. contains the <model_name> folder)
|
(i.e. contains the <model_name> folder)
|
||||||
"""
|
"""
|
||||||
models_dir = os.path.expanduser(os.path.expandvars(models_dir))
|
models_dir = os.path.expanduser(os.path.expandvars(models_dir))
|
||||||
if batch_size is None:
|
if batch_size is None:
|
||||||
@@ -61,7 +62,7 @@ def interact_model(
|
|||||||
hparams=hparams, length=length,
|
hparams=hparams, length=length,
|
||||||
context=context,
|
context=context,
|
||||||
batch_size=batch_size,
|
batch_size=batch_size,
|
||||||
temperature=temperature, top_k=top_k
|
temperature=temperature, top_k=top_k, top_p=top_p
|
||||||
)
|
)
|
||||||
|
|
||||||
saver = tf.train.Saver()
|
saver = tf.train.Saver()
|
||||||
|
@@ -22,7 +22,25 @@ def top_k_logits(logits, k):
|
|||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
def sample_sequence(*, hparams, length, start_token=None, batch_size=None, context=None, temperature=1, top_k=0):
|
def top_p_logits(logits, p):
|
||||||
|
"""Nucleus sampling"""
|
||||||
|
batch, _ = logits.shape.as_list()
|
||||||
|
sorted_logits = tf.sort(logits, direction='DESCENDING', axis=-1)
|
||||||
|
cumulative_probs = tf.cumsum(tf.nn.softmax(sorted_logits, axis=-1), axis=-1)
|
||||||
|
indices = tf.stack([
|
||||||
|
tf.range(0, batch),
|
||||||
|
# number of indices to include
|
||||||
|
tf.maximum(tf.reduce_sum(tf.cast(cumulative_probs <= p, tf.int32), axis=-1) - 1, 0),
|
||||||
|
], axis=-1)
|
||||||
|
min_values = tf.gather_nd(sorted_logits, indices)
|
||||||
|
return tf.where(
|
||||||
|
logits < min_values,
|
||||||
|
tf.ones_like(logits) * -1e10,
|
||||||
|
logits,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def sample_sequence(*, hparams, length, start_token=None, batch_size=None, context=None, temperature=1, top_k=0, top_p=1):
|
||||||
if start_token is None:
|
if start_token is None:
|
||||||
assert context is not None, 'Specify exactly one of start_token and context!'
|
assert context is not None, 'Specify exactly one of start_token and context!'
|
||||||
else:
|
else:
|
||||||
@@ -45,6 +63,7 @@ def sample_sequence(*, hparams, length, start_token=None, batch_size=None, conte
|
|||||||
next_outputs = step(hparams, prev, past=past)
|
next_outputs = step(hparams, prev, past=past)
|
||||||
logits = next_outputs['logits'][:, -1, :] / tf.to_float(temperature)
|
logits = next_outputs['logits'][:, -1, :] / tf.to_float(temperature)
|
||||||
logits = top_k_logits(logits, k=top_k)
|
logits = top_k_logits(logits, k=top_k)
|
||||||
|
logits = top_p_logits(logits, p=top_p)
|
||||||
samples = tf.multinomial(logits, num_samples=1, output_dtype=tf.int32)
|
samples = tf.multinomial(logits, num_samples=1, output_dtype=tf.int32)
|
||||||
return [
|
return [
|
||||||
next_outputs['presents'] if past is None else tf.concat([past, next_outputs['presents']], axis=-2),
|
next_outputs['presents'] if past is None else tf.concat([past, next_outputs['presents']], axis=-2),
|
||||||
|
Reference in New Issue
Block a user