A machine learning pipeline for training a GPT-2 model to predict poker actions from game states. The project uses transformer-based language modeling to learn poker decision-making from hand history data.
This project trains a neural network to predict poker actions (FOLD, CALL, RAISE) given the current game state. The model learns from real poker hand histories using a masked language modeling approach where the context (game state) is provided and the model must predict the action.
-
Clone the repository
git clone https://github.com/YOUR_USERNAME/pokerAI.git cd pokerAI -
Create virtual environment
python -m venv venv venv\Scripts\activate # On Windows source venv/bin/activate # On Linux/Mac
-
Install dependencies
pip install -r requirements.txt
-
Prepare your data
- Place your poker hand history data in
data/hands.txt - Each line should be a complete hand history in the format:
POSITION,STACK,CARDS,PLAYER_INFO,ACTION
- Place your poker hand history data in
Cleans the raw poker data and creates train/test splits:
- Removes redundant "0BB" from FOLD actions
- Splits data into 95% training / 5% validation
- Saves cleaned data to
data/hands_fold_update.txt
python data/prepare_data.pyTrains a custom BPE tokenizer on the poker domain:
- Vocabulary size: 4000 tokens
- Special tokens:
<|startoftext|>,<|pad|> - Saves tokenizer to
tokenizer/directory
python data/train_tokenizer.pyTrains a simple bigram model for comparison:
- Provides a baseline for model performance
- Expected validation loss: ~1.53
python data/train_bigram.pyOption A: Raw PyTorch Loop (data/build_model.py)
- GPT-2 architecture (6 layers, 256 hidden dimensions, 8 heads)
- Context length: 192 tokens
- Masked training: only predicts the action, not the context
- 3 epochs with AdamW optimizer (lr=3e-4)
python data/build_model.pyOption B: TRL Trainer (data/TRL_model.py)
- Uses Hugging Face TRL library for supervised fine-tuning
- Same model architecture as raw loop
- Prompt/completion format for masked training
- Saves model to
model_out_trl/
python data/TRL_model.py- Model Type: GPT-2 (decoder-only transformer)
- Parameters: ~2.6M
- Layers: 6
- Hidden Size: 256
- Attention Heads: 8
- Context Window: 192 tokens
- Vocabulary Size: 4000 (domain-specific)
The model uses masked language modeling:
- Input: Full hand history (game state + action)
- Masking: All tokens up to the last comma (the game state) are masked
- Prediction: Model learns to predict only the action tokens
- This ensures the model focuses on decision-making, not memorizing game states
Each hand history line should follow this format:
POSITION,STACK,CARDS,PLAYER1_INFO,PLAYER2_INFO,...,ACTION
Example:
BTN,1.5BB,9d 10c,P1:101.0BB/0.0BB,P2:100.23BB/0.0BB,FOLD
pokerAI/
├── data/
│ ├── prepare_data.py # Data cleaning and splitting
│ ├── train_tokenizer.py # Custom tokenizer training
│ ├── train_bigram.py # Baseline model training
│ ├── build_model.py # GPT-2 training (raw loop)
│ ├── TRL_model.py # GPT-2 training (TRL)
│ ├── hands.txt # Raw input data (not in repo)
│ └── hands_fold_update.txt # Cleaned data (not in repo)
├── tokenizer/ # Trained tokenizer (not in repo)
├── model_out_trl/ # Trained model (not in repo)
├── requirements.txt # Python dependencies
└── README.md # This file
- Bigram baseline: Validation loss ~1.53
- GPT-2 model: Expected to significantly outperform baseline
- Compare final validation loss against baseline to assess improvement
- Large files (data, models, tokenizer) are excluded from git via .gitignore
- These can be regenerated by running the training scripts
- Model training requires CUDA GPU for reasonable speed
- Adjust batch size and learning rate based on your hardware
[Add your license here]