Answers.Body: <p>Probably the answer lays in the training. Can you compare with a pre-trained network, such as the one used by Stockfish?</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-29T12:35:17.860Z
Answers.Text: I've just ran some tests on my neural network and the one from stockfish. It seems their mean squared error to the actual depth 20 stockfish are both on the same magnitude in centipawns: 1.29 for stockfish's net and 1.88 for my network. As for the cnn, i get 1.70. This is not much better than the nnue network. Therefore, i think i have introduced some bias in my initial performance measurements, as the conv net doesn't perform as good as i thought.
  Anyways, thank you for the idea! Also, if you know about some source that compares cnn to mlp for chess evaluation, please let me know!
Answers.UserDisplayName: Nonlinear
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-04-28T20:04:47.407Z
Answers.LastActivityDate: 2024-04-28T20:04:47.407Z
Answers.UserDisplayName: Geert Bosch
Answers.Body: <p>Maybe the following is too basic for you, but I decided to respond anyway: Chess engine procedures and apps related to NNUE or other approaches in engine learning are mostly published in Github repositories, often by the engine authors themselves. Here 2 links you want to check which goes deeper into NNUE:</p>
  <p><a href="https://github.com/FireFather/sf-nnue-aio/blob/master/readme.txt" rel="nofollow noreferrer">https://github.com/FireFather/sf-nnue-aio/blob/master/readme.txt</a></p>
  <p><a href="https://github.com/nodchip/Stockfish/releases/tag/stockfish-nnue-2020-05-30" rel="nofollow noreferrer">https://github.com/nodchip/Stockfish/releases/tag/stockfish-nnue-2020-05-30</a></p>
  <p>Greetings!</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-29T21:20:14.227Z
Answers.Text: Thanks! Personally, i've been mostly refering to this document: https://github.com/official-stockfish/nnue-pytorch/blob/master/docs/nnue.md
Answers.UserDisplayName: Nonlinear
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2024-05-29T14:08:36.307Z
Answers.LastActivityDate: 2024-05-29T14:21:00.620Z
Answers.LastEditDate: 2024-05-29T14:21:00.620Z
Answers.UserDisplayName: MikeTrans
Body: <p>I've been working on my own chess engine for a while now. It uses standard minimax search. I've trained a convolutional neural network as a static evaluation function, and I've had some good results with it. However recently I decided to learn more about NNUE. I've managed to implement it in my engine, and in terms of speed it is about 20 times faster than the conv net. However, i am having trouble with the accuracy of the network. It performs very poorly and doesn't generalize well, even though it has more parameters than the conv network.
  Here are the details of both networks:</p>
  <p>Model: &quot;CONV&quot;</p>
  <div class="s-table-container"><table class="s-table">
  <thead>
  <tr>
  <th style="text-align: right;">Layer (type)</th>
  <th>Output Shape</th>
  <th>Param</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td style="text-align: right;">input_1 (InputLayer)</td>
  <td>[(None, 8, 8, 12)]</td>
  <td>0</td>
  </tr>
  <tr>
  <td style="text-align: right;">conv2d (Conv2D)</td>
  <td>(None, 4, 4, 768)</td>
  <td>231168</td>
  </tr>
  <tr>
  <td style="text-align: right;">conv2d_1 (Conv2D)</td>
  <td>(None, 2, 2, 384)</td>
  <td>2654592</td>
  </tr>
  <tr>
  <td style="text-align: right;">conv2d_2 (Conv2D)</td>
  <td>(None, 1, 1, 192)</td>
  <td>295104</td>
  </tr>
  <tr>
  <td style="text-align: right;">flatten (Flatten)</td>
  <td>(None, 192)</td>
  <td>0</td>
  </tr>
  <tr>
  <td style="text-align: right;">dense (Dense)</td>
  <td>(None, 96)</td>
  <td>18528</td>
  </tr>
  <tr>
  <td style="text-align: right;">output_layer (Dense)</td>
  <td>(None, 1)</td>
  <td>97</td>
  </tr>
  </tbody>
  </table></div>
  <p>Total params: 3,199,489</p>
  <p>Trainable params: 3,199,489</p>
  <p>Non-trainable params: 0</p>
  <hr />
  <p>Model: &quot;NNUE&quot;</p>
  <div class="s-table-container"><table class="s-table">
  <thead>
  <tr>
  <th>Layer (type)</th>
  <th>Output Shape</th>
  <th>Param</th>
  <th>Connected to</th>
  </tr>
  </thead>
  <tbody>
  <tr>
  <td>stm_perspective (InputLayer)</td>
  <td>[(None, 40960)]</td>
  <td>0</td>
  <td>[]</td>
  </tr>
  <tr>
  <td>nstm_perspective (InputLayer)</td>
  <td>[(None, 40960)]</td>
  <td>0</td>
  <td>[]</td>
  </tr>
  <tr>
  <td>dense_1 (Dense)</td>
  <td>(None, 255)</td>
  <td>10445055</td>
  <td>['stm[0][0]', 'nstm[0][0]']</td>
  </tr>
  <tr>
  <td>concatenate (Concatenate)</td>
  <td>(None, 510)</td>
  <td>0</td>
  <td>['dense_1[0][0]', 'dense_1[1][0]']</td>
  </tr>
  <tr>
  <td>dense_2 (Dense)</td>
  <td>(None, 32)</td>
  <td>16352</td>
  <td>['concatenate[0][0]']</td>
  </tr>
  <tr>
  <td>dense_3 (Dense)</td>
  <td>(None, 32)</td>
  <td>1056</td>
  <td>['dense_2[0][0]']</td>
  </tr>
  <tr>
  <td>output_layer (Dense)</td>
  <td>(None, 1)</td>
  <td>33</td>
  <td>['dense_3[0][0]']</td>
  </tr>
  </tbody>
  </table></div>
  <p>Total params: 10,462,496</p>
  <p>Trainable params: 10,462,496</p>
  <p>Non-trainable params: 0</p>
  <hr />
  <p>The features of the conv network are (square_x, square_y, piece), and the nnue network uses the halfkp feature set. Both networks are trained on a dataset of positions with their evaluations normalized between 0 and 1. The conv net is trained on about 160M positions. I've stopped the training of the nnue net at 72M because it doesn't seem to improve anymore.</p>
  <p>On the same positions, here are the mean squared error of the two neural networks:</p>
  <p>NNUE: 0.1829</p>
  <p>CONV: 0.0374</p>
  <p>Does anybody know what could be going wrong here? I find it stange that a network with more parameters performs worse than a network with less parameters. Am I doing something wrong? Is the nnue net lacking training data? Or is it normal that conv nets perform so much better?</p>
  
Comments.ContentLicense: CC BY-SA 4.0
Comments.CreationDate: 2024-05-15T23:58:34.763Z
Comments.Text: I have just started learning about NNUE, so you probably know more than me. But my understanding is that an NNUE is optimised for speed, rather than accuracy, which means it can be used as the evaluation function in a classical alpha-beta search engine. What you are seeing might just be the inevitable loss of accuracy resulting from this trade-off.
Comments.UserDisplayName: TonyK
Tags: <engines><analysis><programming><evaluation>
Title: How to train an efficiently updatable neural network (NNUE)?
ThreadText: # How to train an efficiently updatable neural network (NNUE)?
  
  (Asked by: username_0 on 2024-03-09)
  
  I've been working on my own chess engine for a while now. It uses standard minimax search. I've trained a convolutional neural network as a static evaluation function, and I've had some good results with it. However recently I decided to learn more about NNUE. I've managed to implement it in my engine, and in terms of speed it is about 20 times faster than the conv net. However, i am having trouble with the accuracy of the network. It performs very poorly and doesn't generalize well, even though it has more parameters than the conv network.
  Here are the details of both networks:
  
  Model: "CONV"
  
  | Layer (type) | Output Shape | Param |
  | --- | --- | --- |
  | input\_1 (InputLayer) | [(None, 8, 8, 12)] | 0 |
  | conv2d (Conv2D) | (None, 4, 4, 768) | 231168 |
  | conv2d\_1 (Conv2D) | (None, 2, 2, 384) | 2654592 |
  | conv2d\_2 (Conv2D) | (None, 1, 1, 192) | 295104 |
  | flatten (Flatten) | (None, 192) | 0 |
  | dense (Dense) | (None, 96) | 18528 |
  | output\_layer (Dense) | (None, 1) | 97 |
  
  Total params: 3,199,489
  
  Trainable params: 3,199,489
  
  Non-trainable params: 0
  
  ---
  
  Model: "NNUE"
  
  | Layer (type) | Output Shape | Param | Connected to |
  | --- | --- | --- | --- |
  | stm\_perspective (InputLayer) | [(None, 40960)] | 0 | [] |
  | nstm\_perspective (InputLayer) | [(None, 40960)] | 0 | [] |
  | dense\_1 (Dense) | (None, 255) | 10445055 | ['stm[0][0]', 'nstm[0][0]'] |
  | concatenate (Concatenate) | (None, 510) | 0 | ['dense\_1[0][0]', 'dense\_1[1][0]'] |
  | dense\_2 (Dense) | (None, 32) | 16352 | ['concatenate[0][0]'] |
  | dense\_3 (Dense) | (None, 32) | 1056 | ['dense\_2[0][0]'] |
  | output\_layer (Dense) | (None, 1) | 33 | ['dense\_3[0][0]'] |
  
  Total params: 10,462,496
  
  Trainable params: 10,462,496
  
  Non-trainable params: 0
  
  ---
  
  The features of the conv network are (square\_x, square\_y, piece), and the nnue network uses the halfkp feature set. Both networks are trained on a dataset of positions with their evaluations normalized between 0 and 1. The conv net is trained on about 160M positions. I've stopped the training of the nnue net at 72M because it doesn't seem to improve anymore.
  
  On the same positions, here are the mean squared error of the two neural networks:
  
  NNUE: 0.1829
  
  CONV: 0.0374
  
  Does anybody know what could be going wrong here? I find it stange that a network with more parameters performs worse than a network with less parameters. Am I doing something wrong? Is the nnue net lacking training data? Or is it normal that conv nets perform so much better?
  
  **Tags:** engines, analysis, programming, evaluation
  
  > **Comments:**
  > **username_1**: I have just started learning about NNUE, so you probably know more than me. But my understanding is that an NNUE is optimised for speed, rather than accuracy, which means it can be used as the evaluation function in a classical alpha-beta search engine. What you are seeing might just be the inevitable loss of accuracy resulting from this trade-off.  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_2 on 2024-04-28. Score: 1
  
  Probably the answer lays in the training. Can you compare with a pre-trained network, such as the one used by Stockfish?
  
  > **Comments:**
  > **username_0**: I've just ran some tests on my neural network and the one from stockfish. It seems their mean squared error to the actual depth 20 stockfish are both on the same magnitude in centipawns: 1.29 for stockfish's net and 1.88 for my network. As for the cnn, i get 1.70. This is not much better than the nnue network. Therefore, i think i have introduced some bias in my initial performance measurements, as the conv net doesn't perform as good as i thought. Anyways, thank you for the idea! Also, if you know about some source that compares cnn to mlp for chess evaluation, please let me know!  
  
  ---
  
  ### Answer by username_3 on 2024-05-29. Score: 1
  
  Maybe the following is too basic for you, but I decided to respond anyway: Chess engine procedures and apps related to NNUE or other approaches in engine learning are mostly published in Github repositories, often by the engine authors themselves. Here 2 links you want to check which goes deeper into NNUE:
  
  <https://github.com/FireFather/sf-nnue-aio/blob/master/readme.txt>
  
  <https://github.com/nodchip/Stockfish/releases/tag/stockfish-nnue-2020-05-30>
  
  Greetings!
  
  > **Comments:**
  > **username_0**: Thanks! Personally, i've been mostly refering to this document: https://github.com/official-stockfish/nnue-pytorch/blob/master/docs/nnue.md  
  
  ---
  
  