NNUE Question - King Placements

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: NNUE Question - King Placements

Post by syzygy »

Pedro wrote: Wed Oct 28, 2020 12:23 pm Guys, I'm a layman, but how is the Stockfish neural network trained? We know that Leela's network is trained from zero, but is NNUE training supervised? How is it done?
It is trained at positions evaluated by Stockfish with shallow searches. Originally by "classical Stockfish" and now by NNUE Stockfish. (And this is about as much as I know about it.)
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: NNUE Question - King Placements

Post by Daniel Shawul »

syzygy wrote: Fri Oct 23, 2020 10:58 pm
Gerd Isenberg wrote: Fri Oct 23, 2020 10:25 pm After some thinking, I believe the weights are not shared between two halves and the initial NNUE graph with its weight enumeration was more correct and I was too hasty to change it.

The upper black half has black king placement times 5 black piece types on 64 squares, and the white half, white king placement times 5 white pieces on 64 squares. Otherwise, with 10 pieces in both halves there would be 2 x 41024 inputs.
The weights are shared by the two halves.

The white-king half has 10x64x64 weights for (piece type, piece square, white-king square).
The black-king half also has 10x64x64 weights for (piece type, piece square, black-king square).

The weight are shared in that the "white-king weight" for (pt, sq, wksq) equals the "black-king weight" for (pt ^ 8, sq ^ 63, bksq) where pt ^ 8 flips the piece's color and sq ^ 63 rotates the board by 180 degrees. It is correct that sq ^ 56 would be more logical, but at the moment it is still sq ^ 63 (in Stockfish). Changing that would require a newly trained net.
Isn't it possible to half the number of weights in the input layer by considering file symmetry? i.e. 10x32x64 weights shared by each king.
The symmetry breaks for FRC chess but should be Ok for standard chess.
syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: NNUE Question - King Placements

Post by syzygy »

Daniel Shawul wrote: Sun Nov 01, 2020 1:08 am
syzygy wrote: Fri Oct 23, 2020 10:58 pm
Gerd Isenberg wrote: Fri Oct 23, 2020 10:25 pm After some thinking, I believe the weights are not shared between two halves and the initial NNUE graph with its weight enumeration was more correct and I was too hasty to change it.

The upper black half has black king placement times 5 black piece types on 64 squares, and the white half, white king placement times 5 white pieces on 64 squares. Otherwise, with 10 pieces in both halves there would be 2 x 41024 inputs.
The weights are shared by the two halves.

The white-king half has 10x64x64 weights for (piece type, piece square, white-king square).
The black-king half also has 10x64x64 weights for (piece type, piece square, black-king square).

The weight are shared in that the "white-king weight" for (pt, sq, wksq) equals the "black-king weight" for (pt ^ 8, sq ^ 63, bksq) where pt ^ 8 flips the piece's color and sq ^ 63 rotates the board by 180 degrees. It is correct that sq ^ 56 would be more logical, but at the moment it is still sq ^ 63 (in Stockfish). Changing that would require a newly trained net.
Isn't it possible to half the number of weights in the input layer by considering file symmetry? i.e. 10x32x64 weights shared by each king.
The symmetry breaks for FRC chess but should be Ok for standard chess.
Yes it is. And without castling rights being taken into account I don't see how it would break either FRC or standard chess.

Of course with the current "rotate" instead of "flip" approach, there is already some kind of implicit assumption of symmetry. If you would force the king to be in a-d, the difference between flip and rotate disappears.

A future architecture probably will take castling rights into account, but once castling rights have been used or lost, symmetry returns. I guess the most practical way to use the fact that positions without castling rights are symmetric is to train the network also on mirror images of training positions. (I don't know if this is already being done.)
Daniel Shawul
Posts: 4185
Joined: Tue Mar 14, 2006 11:34 am
Location: Ethiopia

Re: NNUE Question - King Placements

Post by Daniel Shawul »

syzygy wrote: Sun Nov 01, 2020 3:00 pm Yes it is. And without castling rights being taken into account I don't see how it would break either FRC or standard chess.
Edit: Ignore the below post -- I now see you are talking about the case where castling rights are not available.
I leave the post here just incase someone is confused about FRC castling like I did :)

----
I thought it wouldn't break for FRC too, but Aloril convinced me it does break for FRC with an example.

Take two vertically symmetrical positions:
https://lichess.org/analysis/4rkr1/pppp ... KQkq_-_0_1
https://lichess.org/analysis/1rkr4/pp1p ... KQkq_-_0_1

You can deliver a checkmate in the first position by castling short.
But you can not do it on the second position, because "castling short" is really a long one taking the king and rook to the other half of the board.
IIRC the fact that in standard chess the king is on the right half of the board when it has castling rights makes it work, but for FRC this breaks.
syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: NNUE Question - King Placements

Post by syzygy »

Daniel Shawul wrote: Sun Nov 01, 2020 6:39 pm
syzygy wrote: Sun Nov 01, 2020 3:00 pm Yes it is. And without castling rights being taken into account I don't see how it would break either FRC or standard chess.
Edit: Ignore the below post -- I now see you are talking about the case where castling rights are not available.
I leave the post here just incase someone is confused about FRC castling like I did :)
I was talking about the situation where the NNUE evaluation does not take into account castling rights at all (which is the current situation).

If the evaluation makes no difference between castling being available and castling not being available, then I don't see how FRC can break any harder by making the net symmetric than normal chess.
User avatar
hgm
Posts: 27819
Joined: Fri Mar 10, 2006 10:06 am
Location: Amsterdam
Full name: H G Muller

Re: NNUE Question - King Placements

Post by hgm »

How are these NNUE trained? Is that just a matter of Texel tuning, on a set of quiet positions from games with a known result?
User avatar
Look
Posts: 366
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: NNUE Question - King Placements

Post by Look »

hgm wrote: Sat Oct 24, 2020 10:13 pm It would probably do well, but what I am afraid of is that in comparison it would not do very much better than ignoring the King location alltogether (ie. an NNUE network built on PST rather than KPST). Because it is always implied reasonably accurately where the Kings can be found. For Chess and Shogi it matters much more where the King is, as it can be anywhere, and typically moves to one of the corners. The PST would be completely different for Kings in such different locations. So I was thinking in what way the computational effort could be used in a more relevant way instead.
[...]

A NN with KPST is a tactical one , IMHO. One can develop a strategical NN with what you say , PST. Only piece and it's legal or pseudo legal moves can be considered. In the end the output of two networks (tactical/positional) could be combined with a small NN in eval.
Farewell.
User avatar
Look
Posts: 366
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: NNUE Question - King Placements

Post by Look »

[...]
Look wrote: Fri Nov 06, 2020 8:36 am
A NN with KPST is a tactical one , IMHO. One can develop a strategical NN with what you say , PST. Only piece and it's legal or pseudo legal moves can be considered. In the end the output of two networks (tactical/positional) could be combined with a small NN in eval.
To clarify, I would say legal or pseudo legal moves whose to squares are empty ones.
Farewell.
User avatar
Look
Posts: 366
Joined: Thu Jun 05, 2014 2:14 pm
Location: Iran
Full name: Mehdi Amini

Re: NNUE Question - King Placements

Post by Look »

As an example for positional NN consider this:

Stockfish 11 64-bit against Houdini 6 64-bit
in CCRL Blitz

[d]2r1r1k1/1p2qpp1/p2p1b2/3P4/n1P4p/1N3P2/PPRQB1PP/1R5K w - - 0 23

Here Stockfish played Bf1 and went on to lose the game with this passive bishop. Alternative is f4. White gains moves like Bg4. According to my brief analysis f4 is better than Bf1.
Farewell.
Sopel
Posts: 389
Joined: Tue Oct 08, 2019 11:39 pm
Full name: Tomasz Sobczyk

Re: NNUE Question - King Placements

Post by Sopel »

syzygy wrote: Sun Nov 01, 2020 3:00 pm
If you would force the king to be in a-d, the difference between flip and rotate disappears.
This sentence is a hidden gem. It sparked me to try and force each perspective in HalfKP to put the king in e..h files, mirroring the board for this perspective if the king is on the a..d files instead. Not only does this reduce the size of the network by a factor of 2 but is also, so far, the best way to cut the size of the net I have found - there is no visible impact on the strength and it even learns faster near the start. One could think that it may have disastrous effect because sometimes white's perspective is flipped while black's isn't, but miraculously this is not an issue at all!

blue = standard, orange = this stuff above
Image
dangi12012 wrote:No one wants to touch anything you have posted. That proves you now have negative reputations since everyone knows already you are a forum troll.

Maybe you copied your stockfish commits from someone else too?
I will look into that.