gpt2-reuters

A small GPT-2 style causal language model trained from randomly initialised weights on the Reuters articles dataset. No pretrained GPT weights were used. It was trained for an Advanced Machine Learning (self-supervised learning) assignment and is intended for study, not for production use.

Model details

Item Value
Architecture GPT2LMHeadModel (decoder-only transformer)
Parameters 18.12M
Layers (n_layer) 6
Hidden size (n_embd) 256
Attention heads (n_head) 8
Context length (n_positions) 256 tokens
Vocabulary size 52,000
Dropout (embedding / attention / residual) 0.1 / 0.1 / 0.1
Tied input and output embeddings Yes

Most of the parameters sit in the embedding table (52,000 x 256 is about 13.3M), so the transformer blocks themselves are a small part of the model.

Data

  • Dataset: ingeniumacademy/reuters_articles (train 17,262, validation 2,158, test 2,158 articles; columns title and body).

  • Tokenizer: ingeniumacademy/gpt2-reuters-tokenizer (byte-level BPE trained on this corpus), included in this repository.

  • Pre-processing: each article is turned into one "full text" string:

    <title>\n<body><|endoftext|>
    

    All articles are concatenated into one token stream and cut into fixed blocks of 256 tokens (the leftover tail of each batch is dropped). Labels are a copy of the input ids; the model shifts them by one position internally for next-token prediction.

  • The title and body are joined with a single newline. Prompts at inference time should use the same separator (see usage below), because a different separator (for example a double newline) is tokenized differently and falls outside what the model saw in training.

Training

Setting Value
Objective Causal language modelling (next-token prediction)
Framework Hugging Face transformers Trainer (v5.22.1)
Epochs 5
Batch size 32 sequences of 256 tokens
Learning rate 5e-4, cosine schedule, 100 warmup steps
Optimizer AdamW (default), weight decay 0.1, gradient clipping 1.0
Seed 42
Hardware Google Colab, single NVIDIA Tesla T4
Training time about 1,390 s (23 minutes), 1,860 steps

Training and validation loss were logged every 100 steps.

Results

Split Loss Perplexity
Train (last logged) 4.03 56.2
Validation 4.382 80.0
Test 4.167 64.5

Perplexity is exp(loss). Validation and test sets contain different articles, which is why their losses differ.

How to use

import torch
from transformers import AutoTokenizer, GPT2LMHeadModel

repo = "vihanga-munasinghe/gpt2-reuters"
tokenizer = AutoTokenizer.from_pretrained(repo)
model = GPT2LMHeadModel.from_pretrained(repo).eval()

title = "AEL INDUSTRIES INC <AELNA.O> FIRST QTR NET"
prompt = title.strip() + "\n"          # single newline, same as in training

ids = tokenizer(prompt, return_tensors="pt").input_ids
with torch.no_grad():
    out = model.generate(ids, max_new_tokens=100, do_sample=True, top_p=0.9,
                         temperature=0.8, pad_token_id=tokenizer.eos_token_id)

print(tokenizer.decode(out[0][ids.shape[1]:], skip_special_tokens=True))

Keep prompt plus generated text within 256 tokens, which is the model's context length.

Limitations

  • Small data and model. The training corpus is only about 3 million tokens, far smaller than what language models are usually trained on, so the model learns the style and format of the corpus much better than facts or meaning.
  • Output is fluent in form, not reliable in content. It reproduces the layout of Reuters briefs (for example "Shr ... vs ...", "Revs ... mln") and the closing "Reuter" line, but figures, names and events are invented and often inconsistent or repetitive.
  • Domain and era. The text is 1980s newswire, with its abbreviations, upper-case headlines and company tickers. Prompts far from this style (for example literary text) give poor results.
  • Context limit. The context window is 256 tokens.
  • Not for real use. Do not use the output as a source of facts about companies or markets.

Files

  • model.safetensors, config.json, generation_config.json: model weights and configuration
  • tokenizer.json, tokenizer_config.json, vocab.json, merges.txt, special_tokens_map.json: tokenizer
  • README.md: this model card

Acknowledgements

Dataset and tokenizer by the ingeniumacademy Hugging Face account. Built with Hugging Face transformers, datasets and accelerate.

Downloads last month
83
Safetensors
Model size
18.1M params
Tensor type
F32
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Dataset used to train vihanga-munasinghe/gpt2-reuters