gpt2-text-generation / PartB_README.md
Maryam033's picture
Upload 2 files
7123c78 verified
|
Raw History Blame Contribute Delete
4.86 kB

Part B: GPT-2 Decoder-Only Transformer - Improved News Text Generation

This project uses a pre-trained GPT-2 model (openai-community/gpt2), a decoder-only Transformer, to continue the beginning of an English sentence. It improves on a basic WikiText-2 version in three ways: a cleaner dataset, better decoding settings, and optional fine-tuning with a measured result.

GPT-2 predicts the next token from the previous tokens, again and again (autoregressive generation), so it can write a continuation of any prompt.

What is improved

Problem in the basic version Improvement here
WikiText-2 raw text has tokens such as @-@ and spaces before punctuation, so prompts look broken AG News: clean English news, further cleaned (HTML codes, agency prefixes such as "Reuters - ")
Plain sampling often repeats phrases Better decoding: repetition penalty and no repeated 3-grams
No way to measure quality Perplexity before and after fine-tuning, and a repetition score (distinct-3)
Output is generic Optional fine-tuning on 12,000 news articles

Dataset

AG News (categories: World, Sports, Business, Sci/Tech). Original corpus: http://www.di.unipi.it/~gulli/AG_corpus_of_news_articles.html

File Rows Use
data/ag_news_generation_prompts.csv 6,865 Test articles. The first 10 words of each article are the prompt. Also used to measure perplexity.
data/ag_news_finetune_train.csv 12,000 Training articles (3,000 per category) for the optional fine-tuning. Never overlaps with the test articles.

Cleaning: HTML codes and entities converted, backslashes removed, agency and dateline prefixes removed, extra spaces fixed, articles under 20 words and duplicates removed.

If the CSV files are not uploaded to Colab, the notebook downloads the original AG News files from GitHub and builds both files automatically.

How to run (Google Colab)

  1. Open colab.research.google.com and choose File -> Upload notebook. Upload Part_B_GPT2_Improved_NewsGeneration.ipynb.
  2. Choose Runtime -> Change runtime type -> T4 GPU.
  3. (Optional) Drag the two CSV files from data/ into the Colab Files panel.
  4. Run the cells in order with Shift + Enter.
  5. Set FINE_TUNE = False in Section 6 if you only want the pre-trained model.

Notebook sections

Section What it does
1-2 Install libraries, set the seed (42), check the GPU
3 Load or build the AG News CSV files
4 Load pre-trained GPT-2
5 Baseline perplexity, then plain sampling vs improved decoding
6 Optional fine-tuning (2 epochs, about 5 to 10 minutes on a T4)
7 Perplexity before and after
8 Generate text with the final model
9 Try your own sentence
10 Save the results as CSV files
11 Upload the model to Hugging Face

Settings

Decoding

Setting Plain sampling Improved
Temperature 1.0 0.8
Top-k off 50
Top-p off 0.92
Repetition penalty off 1.2
No repeated n-gram size off 3

Fine-tuning: 2 epochs, learning rate 5e-5, batch size 16, max length 128 tokens, weight decay 0.01, seed 42.

Results

Fill these in from your notebook output.

Perplexity on 300 held-out test articles (lower is better, Section 7)

Model Perplexity
Pre-trained GPT-2 ...
Fine-tuned GPT-2 ...

Repetition score over 30 prompts (distinct-3, higher means less repetition, Section 5)

Decoding distinct-3
Plain sampling ...
Improved decoding ...

Example

Prompt Generated text
... ...

Project files

.
β”œβ”€β”€ Part_B_GPT2_Improved_NewsGeneration.ipynb
β”œβ”€β”€ requirements.txt
β”œβ”€β”€ README.md
└── data/
    β”œβ”€β”€ ag_news_generation_prompts.csv
    └── ag_news_finetune_train.csv

Upload to Hugging Face

Run Section 11 of the notebook (it needs a Hugging Face Write token). If you fine-tuned the model, say so in its model card. If you set FINE_TUNE = False, the uploaded model is an unchanged copy of the pre-trained GPT-2.

Limitations

  • Generated text can be factually wrong. It is not real news.
  • Output changes on every run because of random sampling.
  • GPT-2 can repeat itself and can reflect biases from its training data.
  • English only. Works best with news-style prompts.
  • Fine-tuning adapts the model to news style. It does not make it more truthful.

Citation

Zhang, X., Zhao, J., LeCun, Y. (2015). Character-level Convolutional Networks for Text Classification. NIPS 2015. (AG News)

GPT-2: Radford et al. (2019), OpenAI. Language Models are Unsupervised Multitask Learners.

Author

Maryam Zubair Ahmed