All work

Engineering · C++17 · 2025

chess-nn

A neural network with no ML library, reading chess positions from FEN notation. It classifies a position into five states, from nothing to checkmate, and its mistakes explain what it actually learned.

Role
Network core and encoding, team of three
Stack
C++17 · no ML library
Proof
72.52% on 1,350 unseen positions
Links
GitHub
Confusion matrix on 1,350 held-out positions. Rows: expected class. Columns: predicted class.
NCWCBMWMB
N30148731510
CW611210410
CB450145032
MW51701990
MB30210213

Zero white and black mix-ups in 903 positions.

Rows: expected
Columns: predicted

Results

Evaluated on 1,350 positions the network never saw during training, across five balanced classes where chance is 20%: 72.52%, from 51,845 parameters. Checkmates are recognised about 90% of the time. Plain checks sit between 54% and 65%.

What the errors say

The network never once confuses white with black. Not a single check against white predicted as check against black, in either direction, across 903 non-empty positions. Every error lives on the other axis: how serious the threat is.

Colour is in the input. Each square gets one slot per piece type and colour, so “which king is in danger” is almost a lookup, and a dense layer learns it immediately.

Severity is not in the input. Checkmate means the king is attacked and no legal move escapes, which takes a search, and a feed-forward network over a flat board cannot simulate a move. So it approximates, and the approximation shows what it settled on: mates are visually stereotyped, a king on the edge with its escape squares covered, so they are easy to see. A check can be a single bishop on a long empty diagonal, a relationship between two squares that says nothing about the rest of the board.

The network learned chess geometry. It did not learn chess rules. And that says what would actually help: an input carrying attack maps, or an architecture with a notion of board topology, rather than a bigger network.

What is implemented

Forward and backward propagation written by hand, five activations with their derivatives, a numerically stable softmax, cross-entropy, Xavier initialisation, L2 regularisation and mini-batch training. The only includes are the C++ standard library.

Credits

A three-person project, first built as a school assignment. I wrote the network core, the FEN encoding and parsing and the command-line tool, and ran the training. Johannes Houndete rewrote the matrix storage as a flat buffer, which made it five times faster, and Junior Djahoui built the validation tooling.