Self-play PPO

Reinforcement learning on top of a cloned (or baseline) policy, done the way that doesn't overfit.

PPO (Proximal Policy Optimization) is the default reinforcement learning algorithm for this kind of training. It's stable, well-understood, and the kit ships a worked example you can run as-is.

This page is mostly about the how and the when — the script details live in the kit (examples/03_selfplay_ppo.py for poker).

Start from somewhere, not from scratch

Train PPO from a warm start:

  • A cloned policy (from imitation).
  • A platform baseline (baselines/baseline.npz for poker).
  • Your own previous agent.

Starting from random weights works in principle but is dramatically slower and less stable. Always warm-start if you can.

python examples/03_selfplay_ppo.py --init cloned.npz --out run1
# or
python examples/03_selfplay_ppo.py --init baselines/baseline.npz --out run1

--init accepts a .npz agent file or a .pt torch checkpoint. The network keeps the file's layer sizes, activation and normalizer.

Mix the opponent pool

The single biggest mistake in self-play is training against one opponent. Your network will find a counter to that one opponent, then lose to everything else.

Mix your pool: built-in bots (calling station, equity bot, random) + frozen snapshots of your own agent at different points in training. The example scripts do this by default. Resist the urge to simplify the pool.

If you want to specialize against one opponent (e.g. the current ladder leader), that's a different training run — use --opponents to pin it, understand you're building a specialist, and don't expect it to climb the ladder as a generalist.

How long to train

  • Minutes for a toy run to confirm the setup works.
  • Hours to get a visible improvement over the baseline.
  • Days on many cores for a serious contender. The example 04_parallel_train.py runs across every core on your machine.

Don't fixate on training duration — fixate on evaluate.py scores between runs. If two days of training didn't improve the eval, it didn't improve the agent.

Evaluating during training

Evaluate every few hours:

python tools/evaluate.py runs/current/agent.npz \
    --vs baselines/baseline.npz \
    --deals 5000 --jobs 8

Watch the trend across checkpoints. Keep the best one, not the last one.

Hyperparameters you'll touch

Most PPO defaults in the example are sensible. The ones you'll actually tune:

  • Learning rate. Lower is safer for continuing from a warm start.
  • Entropy bonus. Higher = more exploration, more mixed strategies, harder to read by opponents. Low entropy with a warm start often converges to a near-deterministic policy that gets exploited.
  • Batch size and rollout length. Scale with your core count. More cores = more throughput.
  • Opponent-pool size. 4–8 opponents is a reasonable middle ground. Too few → specialist risk. Too many → noisier gradient.

Signs it's working

  • Eval against a fixed baseline improves over checkpoints.
  • Per-button shares (in check_agent.py's report) stay reasonably balanced.
  • Entropy doesn't collapse to near-zero.
  • Loss curves are stable, not spiking.

Signs it's not

  • One button creeping toward 100% share.
  • Eval vs. the baseline stays flat for hours.
  • Loss diverges (NaN weights → file won't even load).
  • Agent beats the one opponent you trained against by huge margins but loses to everyone else.

Build a hardening phase

After the main run, consider a short "hardening" phase: add a frozen exploiter (a copy trained specifically to beat your current agent) to the pool and keep training with higher entropy. The baseline shipped with the poker kit was hardened this way for the last ~5 hours.

Outcome: a less predictable agent that's harder for specialists to counter. Costs you some win rate against weak opponents in exchange for robustness.