Skip to content

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Repository files navigation

charCNN-hangman

A character-level convolutional neural network that plays Hangman. Given the current board — the partially revealed word plus the letters already guessed wrong — the model predicts which letter to guess next. No dictionary lookups, no hand-written heuristics: it learns letter patterns directly from data.

How it plays

Hangman as a learning problem: at every turn the board is a word with some letters hidden (_ p p _ e for apple) and a set of wrong guesses so far. The model scores all 26 letters, and the game engine guesses the highest-scoring letter it hasn't already tried. A game is won when the whole word is revealed and lost after 6 wrong guesses.

The model is trained on the multi-label version of this task: for a given masked board, every hidden letter is a correct target, so the network learns to spread probability across all plausible next reveals rather than committing to one.

Model — CharCNN

~5.8M parameters. The input is a single 66-dim vector: 40 slots for the word (letter indices, 0 = hidden, 263 = padding) concatenated with a 26-dim binary mask of wrong guesses.

word indices (40) ── embedding (+ learned positional embedding)
                       │
                       ▼
        5 × [ Conv1d(k=3) → BatchNorm → GLU ]     channels: 64→128→256→384→512→768
                       │
                       ▼
        attention pooling (1×1 conv → softmax over positions → weighted sum)  →  768-d
                       │
        concat with wrong-guess mask (26)  →  794-d
                       │
        FC 794→1024 → LayerNorm → ReLU → dropout
        FC 1024→512 → LayerNorm → ReLU → dropout
        FC 512→26   →  per-letter logits

Design choices worth noting:

  • GLU (gated linear unit) conv blocks let the network gate information along the sequence, which works well for the local letter-pattern cues Hangman relies on.
  • Attention pooling collapses the variable-length word into a fixed vector by learning which positions matter, instead of naive mean/max pooling.
  • The wrong-guess mask is injected after pooling, so the final layers can explicitly avoid re-recommending dead letters and use the misses as negative evidence.

Repository layout

File Description
CharCNN_final.py The CharCNN model definition (run it directly to print the parameter count).
training_CharCNN.ipynb Training loop: BCEWithLogitsLoss, Adam (lr 3e-3), batch 256, 20 epochs, 90/10 train/test split. Includes top-1 letter accuracy and accuracy bucketed by how much of the word is revealed.
Dataset_generation.ipynb Builds the training set from a word list — masks random subsets of each word's letters and samples realistic wrong guesses from a smoothed letter-frequency distribution. Saves an .npz of masked_words, wrong_guesses, targets.
simulate_hangman.py Plays full games end-to-end against a word list and reports overall win rate plus a breakdown by word length.
charcnnfin.pt Trained model checkpoint (loads cleanly into CharCNN).

Quickstart

pip install torch numpy tqdm

Play out games with the trained model:

python simulate_hangman.py \
    --model-path charcnnfin.pt \
    --words-path test_words.txt \
    --n-games 10000 \
    --seed 42

test_words.txt is any newline-separated list of lowercase words to evaluate on (words longer than 40 characters are skipped).

Inspect the model:

python CharCNN_final.py        # prints total parameter count

Data pipeline & training (reproduce from scratch)

  1. Run Dataset_generation.ipynb on a word list to produce an .npz with three arrays:
    • masked_words — (N, 40) letter indices with hidden positions set to 0,
    • wrong_guesses — (N, 26) binary mask of incorrect letters,
    • targets — (N, 26) multi-hot label of the hidden letters.
  2. Point load_dataset() in training_CharCNN.ipynb at that file and run the notebook to train and checkpoint the model.
  3. Evaluate with simulate_hangman.py.

The notebooks were written for Google Colab (they reference Google Drive paths); update the paths to run locally.

Notes

  • Words are capped at 40 characters everywhere (MAX_WORD_LEN); the letter index space reserves 0 for hidden positions and 263 for padding.
  • Guessing is greedy — the highest-scoring not-yet-guessed letter each turn. Swapping in a different policy (e.g. sampling) only touches simulate_hangman.py.

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages