Hey all,
Just an interesting thought that I had, for classical evaluation.
Many NNUEs use halfkp as an input layer, with every input being relative to a specific king square.
I'm curious - has anyone tried "King piece square tables?" i.e. piece square tables relative to king location? I'd imagine it would improve a program's knowledge of king safety. It would have to be tuned sort of like a neural network, but it might give a small rating boost if tuned correctly.
Thoughts?
King-Piece-Square-Tables
Moderator: Ras
-
- Posts: 80
- Joined: Fri Jul 29, 2022 1:30 am
- Full name: Aaron Li
-
- Posts: 28353
- Joined: Fri Mar 10, 2006 10:06 am
- Location: Amsterdam
- Full name: H G Muller
Re: King-Piece-Square-Tables
This is basically what NNUE are, right? Except that you have some 256 such PST, and a small NN to combine their scores.
This is primarily a Shogi technique, though. In Shogi a King typically gets chased all over the board. And most pieces are slow-moving, which means you might as well not have them at all if they are far away from the Kings.
In Chess the Kings usually sit statically in their fortress. King Safety is not very different when having a King on h1 as it is when that same King would be on g1. A single move would convert one position into the other. Evaluation is supposed to score strategic traits of a position, i.e. aspects that are likely to persist for a long time. If a King would be very unsafe on g1, and completely save on h1, the compromised safety on g1 would not be worth very much to the opponent, because this 'advantage' will evaporate on the next move. So it isn't worth more than a tempo.
It would make sense to have different PST depending on which direction the player castled. E.g. for King at or next to b1 or g1, or elsewere.
This is primarily a Shogi technique, though. In Shogi a King typically gets chased all over the board. And most pieces are slow-moving, which means you might as well not have them at all if they are far away from the Kings.
In Chess the Kings usually sit statically in their fortress. King Safety is not very different when having a King on h1 as it is when that same King would be on g1. A single move would convert one position into the other. Evaluation is supposed to score strategic traits of a position, i.e. aspects that are likely to persist for a long time. If a King would be very unsafe on g1, and completely save on h1, the compromised safety on g1 would not be worth very much to the opponent, because this 'advantage' will evaporate on the next move. So it isn't worth more than a tempo.
It would make sense to have different PST depending on which direction the player castled. E.g. for King at or next to b1 or g1, or elsewere.
-
- Posts: 80
- Joined: Fri Jul 29, 2022 1:30 am
- Full name: Aaron Li
Re: King-Piece-Square-Tables
Yeah, that makes a lot of sense. Perhaps I will try different piece square tables for different combinations of kingside and queenside castles.
The halfKP style neural network always seemed a little weird to me. Maybe someone will discover a different/better approach.
The halfKP style neural network always seemed a little weird to me. Maybe someone will discover a different/better approach.
-
- Posts: 87
- Joined: Thu Oct 07, 2021 12:48 am
- Location: Warsaw, Poland
- Full name: Michal Witanowski
Re: King-Piece-Square-Tables
I just implemented this in my engine and it gave 230 (+-40) Elo improvement over regular piece square table when evaluation is purely PSQT-based (if I enabled NNUE back, it was only 10 Elo gain).
You can see how PSQT looks like here: https://github.com/Witek902/Caissa/blob ... d/PSQT.cpp
It's 2*10*32*64=40960 weights in total. x2 for middlegame/endgame. I'm normalizing king position to A-D files.
You can clearly see that PSQT learned some basic king safety: notice pawn weights for king on a1 - it tries to preserve pawns around king.
The other interesting thing is lowered queen importance when white king is approaching to 8th rank (or black to 1st rank).
You can see how PSQT looks like here: https://github.com/Witek902/Caissa/blob ... d/PSQT.cpp
It's 2*10*32*64=40960 weights in total. x2 for middlegame/endgame. I'm normalizing king position to A-D files.
You can clearly see that PSQT learned some basic king safety: notice pawn weights for king on a1 - it tries to preserve pawns around king.
The other interesting thing is lowered queen importance when white king is approaching to 8th rank (or black to 1st rank).
Author of Caissa Chess Engine: https://github.com/Witek902/Caissa
-
- Posts: 915
- Joined: Sun Dec 27, 2020 2:40 am
- Location: Bremen, Germany
- Full name: Thomas Jahn
Re: King-Piece-Square-Tables
Interesting!
Both versions are tapered e.g. in both you are interpolating between a set of midgame and endgame tables?
what does that mean? what if your king is not on one of these 4 files?
That's a lot of weights! How many positions do you use for tuning the PSTs?
-
- Posts: 87
- Joined: Thu Oct 07, 2021 12:48 am
- Location: Warsaw, Poland
- Full name: Michal Witanowski
Re: King-Piece-Square-Tables
Yes.
If the white king is on rank E-H, I flip the board horizontally so when reading PSQT values, the pieces are always relative to the king being on A-D. Same for black king (independently). I think the same thing is done in Stockfish's NNUE. I could have full 2x10x64x64 table, but it's 2x bigger and I noticed that values in "king on a1" vs. "king on h1" were very similar (just flipped horizontally).
80 million. Training takes couple minutes. The problem is when king is on further ranks, because those positions are much rare. I think "white king on a8" positions are 100x less frequent than "white king on g1" in my data set, so the training takes much longer for the former...
Author of Caissa Chess Engine: https://github.com/Witek902/Caissa
-
- Posts: 80
- Joined: Fri Jul 29, 2022 1:30 am
- Full name: Aaron Li
Re: King-Piece-Square-Tables
Perhaps you could maybe generalize any square on the opponent's half of the board?Witek wrote: ↑Sat Oct 08, 2022 1:43 amYes.
If the white king is on rank E-H, I flip the board horizontally so when reading PSQT values, the pieces are always relative to the king being on A-D. Same for black king (independently). I think the same thing is done in Stockfish's NNUE. I could have full 2x10x64x64 table, but it's 2x bigger and I noticed that values in "king on a1" vs. "king on h1" were very similar (just flipped horizontally).
80 million. Training takes couple minutes. The problem is when king is on further ranks, because those positions are much rare. I think "white king on a8" positions are 100x less frequent than "white king on g1" in my data set, so the training takes much longer for the former...
-
- Posts: 263
- Joined: Wed Jun 16, 2021 2:08 am
- Location: Berlin
- Full name: Jost Triller
Re: King-Piece-Square-Tables
Yes, in Nalwald. When I remove them (i.e. use normal piece square tables), Nalwald performs ~100 Elo worse. At the time of implementing it for the first time, it might have been worth less Elo. Maybe ~50. Wrote about it a bit more here.
Currently, I use 4 million positions for training.