This repository contains an implementation of a model to play the classic Hangman challenge using a Transformer-based approach. The environment for the game is provided in utils.py.
The objective is to design a model that can successfully predict hidden letters in words, as in the Hangman game, by leveraging the Transformer encoder architecture.
The dataset used is english-words.70, which includes approximately 33,000 English words.
- Source: http://wordlist.aspell.net/
- Preprocessing: Only words with lengths between 4 and 20 letters are selected for this task.
- Details of the data processing can be found in the
words_processing.ipynbnotebook.
-
Input Representation:
- The input is a tokenized word, where a fraction of the letters are masked.
- The model predicts the probabilities for each letter at the masked positions.
-
Model Architecture:
- A Transformer encoder is used to process the input.
- The model outputs probabilities for each letter, which are averaged across the masked positions to generate predictions.
-
Masking Strategy:
- Surprisingly, using a fixed mask rate during training outperformed using a varying mask rate, even though varying the mask rate is closer to the actual task.
- Tests indicate that a fixed mask rate of 50% yielded the best results.
-
Initial Guesses:
- Attempts to improve the first guess using letter frequency statistics did not improve the success rate.
- Fully masked words (as in the start of the game) were not included in the training data, since this seems to be unnecessary.
-
Self-Attention Mechanism:
- Adding a self-attention mechanism to predict the probabilities of letters across the entire word, rather than simply calculating the average probabilities across the masked positions, did not enhance performance.
-
Reinforcement Learning:
- The current model cannot utilize feedback from failed guesses during the game.
- Incorporating Reinforcement Learning (RL), e.g. Proximal Policy Optimization (PPO), could help address this limitation.
-
Short Word Analysis:
- The model struggles with short words. Understanding the reasons behind this and modifying the training strategy or architecture could improve performance.
-
Hyperparameter Tuning:
- Testing larger embedding dimensions, additional encoder layers, and more attention heads could yield better results.
Special thanks to @MorvanZhou for his insightful lessons on NLP. Check out his work at https://mofanpy.com/.

