GPT-2 Language Model Trained from Scratch on Reuters Articles

Model Details

The objective was to train a small GPT-2 language model from scratch using the Reuters news articles dataset. The model uses Causal Language Modeling (CLM), where it learns to predict the next token based on the preceding tokens in a text sequence.

The model was initialized with random weights without using any pretrained GPT-2 model weights. Training and evaluation were performed using Hugging Face Transformers and PyTorch.

Model Sources

Uses

The model is intended for educational and experimental purposes, particularly for understanding self-supervised learning and autoregressive text generation.

The model can be used for:

  • Generating news-like text from an input prompt.
  • Predicting the next token in a sequence.
  • Analyzing next-token probability distributions.
  • Evaluating generated text using perplexity.
  • Studying the effects of training epochs and learning rates.

How to Get Started with the Model

The model can be loaded using Hugging Face Transformers.

from transformers import AutoTokenizer, GPT2LMHeadModel

model_id = "Pathumi/reuters-gpt2-from-scratch"

tokenizer = AutoTokenizer.from_pretrained(model_id)
model = GPT2LMHeadModel.from_pretrained(model_id)

prompt = "The company announced"

inputs = tokenizer(prompt, return_tensors="pt")

output = model.generate(
    **inputs,
    max_new_tokens=50,
    do_sample=True,
    temperature=0.8,
    top_k=50,
    pad_token_id=tokenizer.eos_token_id
)

print(tokenizer.decode(output[0], skip_special_tokens=True))

Training Details

Training Data

The model was trained using the Reuters Articles dataset available on Hugging Face.

Dataset Split Number of Articles
Training 17,262
Validation 2,158
Testing 2,158

Each article contains two fields: title and body.

Training Procedure

Preprocessing

The title and body of each Reuters article were combined into a single text sequence.

The combined text was tokenized using the ingeniumacademy/gpt2-reuters-tokenizer, which has a vocabulary size of 52,000 tokens.

The maximum sequence length was set to 128 tokens to reduce computational requirements.

Model Configuration

Parameter Value
Model Architecture GPT-2
Transformer Layers 4
Attention Heads 4
Embedding Dimension 256
Vocabulary Size 52,000
Maximum Sequence Length 128
Total Parameters 16,504,320
Weight Initialization Random
Training Objective Causal Language Modeling

Training Hyperparameters

Three training configurations were evaluated.

Configuration Epochs Learning Rate
Baseline 2 0.0005
Experiment 1 5 0.0005
Experiment 2 2 0.0002

The batch size was 8 for both training and evaluation. A fixed random seed of 42 was used across experiments to improve reproducibility.

Training was performed using the Hugging Face Trainer API with GPU acceleration and FP16 mixed precision.

Technical Specifications

Hardware: Google Colab GPU runtime.

Software:

  • Python
  • PyTorch
  • Hugging Face Transformers 4.57.3
  • Hugging Face Datasets 4.4.1
  • Hugging Face Accelerate 1.12.0
Downloads last month
83
Safetensors
Model size
16.5M params
Tensor type
F32
ยท
Inference Providers NEW
This model isn't deployed by any Inference Provider. ๐Ÿ™‹ Ask for provider support

Dataset used to train Pathumi/reuters-gpt2-from-scratch