Can a language model learn to play chess? I investigate this question by training a dedicated (small) language model on chess games using Transformers (and LSTM).
We define 3 types of formats for a game
- the original PGN format
- an intermediary format, which fills the missing information in the PGN format (inferring them by replaying the game)
- training formats, which encode the game in a way that is suitable for training
Original file format is PGN, coming from
https://figshare.com/articles/dataset/Chess_Database/4276523
and used for paper
https://journals.plos.org/plosone/article?id=10.1371/journal.pone.0168213
As an intermediary format, we define a more systematic format for a move
player_turn_piece_origin_destination###info
This intermediary format fills the missing information that is not in the PGN format but can be inferred by replaying the game.
It is used by the core/chess module to replay a game and check the validity of a move, track the board, etc.
We consider different types of training format that can be used for training and produced from the intermediary format.
In data/sequences.txt, we use
piece_origin_destination
where origin and destination are encoded as a1, a2, ..., h8
Example : Pd2d4Pd7d5Pc2c4Pe7e6Nb1c3Ng8f6Pc4d5Pe6d5Bc1g5Bf8e7Pe2e3Nf6e4Bg5e7
In data/sequences64.txt, we encode each square as a number from 1 to 64, separate players by '-' and turns by '='.
Example : 25,27,-,30,28,=,17,19,-,38,37,=,8,18,-,55,45,=,19,28,-,37,28,=,16,52,-,47,38,=,33,34,-,45,35,=,52,38,-,35,18
chess_PNG.pyfor processing games in the PGN format : parse_PGN_game, parse_PGN_move, check_parsed_PGN_move_validity, find_origin (per piece), find_among_candidateschess.pyfor general purpose chess functions : play_game, perform_move, is_check, would_move_lead_to_check, check_move_is_valid
-
CreateSequences.pyreads the intermediary format and produces the sequences.txt training format -
PlayGame.pyreads one PGN game, parse and convert it and plays it. -
ProcessGames.pyreads all PGN and outputs normalized format file containing all games -
ReplayGame.pytries to replay a game from the normalized file