0% found this document useful (0 votes)
17 views3 pages

Custom Text Generation with GPT-2

This document discusses how to make custom AI-generated text using GPT-2. It describes OpenAI's release of GPT-2, a large language model for text generation, and how Neil Shepperd created a fork allowing the model to be fine-tuned on custom datasets. The author then created the gpt-2-simple Python package to streamline the fine-tuning and text generation workflow. The document explains how GPT-2 works as a black box text generator and provides recommendations for fine-tuning the model. It also describes using a Colaboratory notebook with gpt-2-simple to easily fine-tune GPT-2 on a custom dataset.

Uploaded by

zikit.ben.david
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views3 pages

Custom Text Generation with GPT-2

This document discusses how to make custom AI-generated text using GPT-2. It describes OpenAI's release of GPT-2, a large language model for text generation, and how Neil Shepperd created a fork allowing the model to be fine-tuned on custom datasets. The author then created the gpt-2-simple Python package to streamline the fine-tuning and text generation workflow. The document explains how GPT-2 works as a black box text generator and provides recommendations for fine-tuning the model. It also describes using a Colaboratory notebook with gpt-2-simple to easily fine-tune GPT-2 on a custom dataset.

Uploaded by

zikit.ben.david
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

How To Make Custom AI-Generated Text

With GPT-2
September 4, 2019 10 min read​ ​AI​,​ ​Text Generation





In February 2019,​ ​OpenAI​ released​ ​a paper​ describing GPT-2, a AI-based text-generation


model based on the​ ​Transformer architecture​ and trained on massive amounts of text all around
the internet. From a text-generation perspective, the included demos were very impressive: the
text is coherent over a long horizon, and grammatical syntax and punctuation are near-perfect.

At the same time, the Python code which allowed anyone to download the model (albeit smaller
versions out of concern the full model can be abused to mass-generate fake news) and the
TensorFlow​ code to load the downloaded model and generate predictions was​ ​open-sourced on
GitHub​.

Neil Shepperd created​ ​a fork​ of OpenAI’s repo which contains additional code to allow
finetuning​ the existing OpenAI model on custom datasets. A​ ​notebook​ was created soon after,
which can be copied into​ ​Google Colaboratory​ and clones Shepperd’s repo to finetune GPT-2
backed by a free GPU. From there, the proliferation of GPT-2 generated text took off:
researchers such as Gwern Branwen made​ ​GPT-2 Poetry​ and Janelle Shane made​ ​GPT-2
Dungeons and Dragons character bios​.

I waited to see if anyone would make a tool to help streamline this finetuning and text
generation workflow, a la​ ​textgenrnn​ which I had made for recurrent neural network-based text
generation. Months later, no one did. So I did it myself. Enter​ ​gpt-2-simple​, a Python package
which wraps Shepperd’s finetuning code in a functional interface and adds ​many​ utilities for
model management and generation control.

Thanks to gpt-2-simple and​ ​this Colaboratory Notebook​, you can easily finetune GPT-2 on your
own dataset with a simple function, and generate text to your own specifications!

How GPT-2 Works


OpenAI has released three flavors of GPT-2 models to date: the “small” 124M parameter model
(500MB on disk), the “medium” 355M model (1.5GB on disk), and recently the 774M model
(3GB on disk). These models are ​much​ larger than what you see in typical AI tutorials and are
harder to wield: the “small” model hits GPU memory limits while finetuning with consumer
GPUs, the “medium” model requires additional training techniques before it could be finetuned
on server GPUs without going out-of-memory, and the “large” model ​cannot be finetuned at all
with current server GPUs before going OOM, even with those techniques.

The actual Transformer architecture GPT-2 uses is very complicated to explain (here’s a​ ​great
lecture​). For the purposes of finetuning, since we can’t modify the architecture, it’s easier to
think of GPT-2 as a​ ​black box​, taking in inputs and providing outputs. Like​ ​previous forms of text
generators​, the inputs are a sequence of tokens, and the outputs are the probability of the next
token in the sequence, with these probabilities serving as weights for the AI to pick the next
token in the sequence. In this case, both the input and output tokens are​ ​byte pair encodings​,
which instead of using character tokens (slower to train but includes case/formatting) or word
tokens (faster to train but does not include case/formatting) like most RNN approaches, the
inputs are “compressed” to the shortest combination of bytes including case/formatting, which
serves as a compromise between both approaches but unfortunately adds randomness to the
final generation length. The byte pair encodings are later decoded into readable text for human
generation.

The pretrained GPT-2 models were trained on websites linked from​ ​Reddit​. As a result, the
model has a very strong grasp of the English language, allowing this knowledge to transfer to
other datasets and perform well with only a minor amount of additional finetuning. Due to the
English bias in encoder construction, languages with non-Latin characters like Russian and​ ​CJK
will perform poorly in finetuning.
When finetuning GPT-2, I recommend using the 124M model (the default) as it’s the best
balance of speed, size, and creativity. If you have large amounts of training data (>10 MB), then
the 355M model may work better.

gpt-2-simple And Colaboratory


In order to better utilize gpt-2-simple and showcase its features, I created my​ ​own Colaboratory
Notebook​, which can be copied into your own Google account. A Colaboratory Notebook is
effectively a​ ​Jupyter Notebook​ running on a free (w/ a Google Account) virtual machine with an
Nvidia server GPU attached (​randomly​ a K80 or a T4; T4 is ideal) that normally can be
cost-prohibitive.

Common questions

Powered by AI

When selecting the appropriate GPT-2 model size for fine-tuning, one should consider the balance between model size and available resources. For limited datasets, the 124M model is preferable as it balances speed, size, and creativity. For larger datasets exceeding 10 MB, the 355M model could be more effective due to its enhanced capacity to capture nuanced data patterns. Consideration of GPU resources and memory capacity is also crucial, as larger models require more computational power .

The main challenges with fine-tuning larger versions of the GPT-2 model include GPU memory limitations, which even consumer GPUs encounter with the "small" model, and more severe limitations with "medium" and "large" models. The "medium" model requires additional training techniques to be finetuned without causing server GPUs to run out of memory, while the "large" model cannot be finetuned with current setups. These can be mitigated by employing advanced training techniques such as gradient checkpointing, mixed precision training, or by using alternative hardware with greater memory capacity .

GPT-2's transformer architecture enhances text generation coherence through its self-attention mechanisms, which allow the model to weigh the importance of different tokens and capture contextual information over long sequences. This is a significant improvement from earlier models like RNNs, which struggle with capturing dependencies over longer text sections. The result is that GPT-2 can maintain contextual relevancy and produce more logically consistent narratives .

Byte pair encoding (BPE) improves GPT-2's performance by compromising between the slower training of character tokens (including case/formatting) and the faster training of word tokens (which exclude case/formatting). BPE compresses input into the shortest combination of bytes while maintaining necessary formatting, thus enhancing efficiency and maintaining readability in the generated text. This reduces the randomness in generation length compared to pure character or word-based models, making it more effective for the extensive data used in GPT-2 .

The gpt-2-simple package enhances usability by providing a wrapper around the existing GPT-2 finetuning code, thereby simplifying the process and adding utilities for model management and generation control. It allows users to easily finetune GPT-2 on their own datasets using a simple functional interface, thus making custom text generation more accessible without needing in-depth understanding of the underlying complex code .

Google Colaboratory offers significant advantages for fine-tuning GPT-2 models by providing free access to virtual machines with Nvidia server GPUs, which are typically cost-prohibitive. Users can leverage a Jupyter notebook-like environment for running experiments with powerful resources such as T4 GPUs. This setup removes hardware limitations faced by traditional local setups, making the process accessible to a wider audience without financial burden .

GPT-2 may perform poorly with languages using non-Latin characters because the model's encoder is biased towards the English language, having been pretrained on English-text-heavy datasets such as those linked from Reddit. This bias in the encoding process limits the model's ability to accurately capture and generate text in languages with different character sets like Russian or CJK .

The ability to easily generate text using GPT-2 poses risks such as the potential propagation of fake news and misinformation. Since GPT-2 is capable of producing coherent, human-like text quickly and at scale, it might be misused to fabricate credible-sounding articles, manipulate information, and deceive audiences, exacerbating the spread of false narratives .

The open-source nature of GPT-2's code and tools such as gpt-2-simple facilitates innovation by allowing researchers and developers to access, modify, and experiment with the model without barriers. This openness has led to widespread adaptation and improvement, enabling custom applications and fostering community-driven contributions to enhance capabilities and explore novel applications in AI text generation .

Janelle Shane and Gwern Branwen significantly contributed to the proliferation of GPT-2 generated text through creative applications. Shane created Dungeons and Dragons character bios, while Branwen composed poetry using GPT-2. Their works demonstrated the model's potential for unique content creation and inspired further exploration and use of GPT-2 across diverse textual productions .

You might also like