diff options
| author | tslil <tslil@posteo.de> | 2026-08-30 19:46:07 +0100 |
|---|---|---|
| committer | tslil <tslil@posteo.de> | 2026-09-02 12:13:01 +0100 |
| commit | 241156da6f35cb888253e6b8814e7b61a76b4fc7 (patch) | |
| tree | e562029445b1473cc89590793f32c561540d621c /include/nn.c | |
| parent | 696c3808220ea9d8c604939894a2d509f05cc4c6 (diff) | |
TD(lambda), and a big rework
NOTE: 6x6 weights are as-yet untrained
On the performance side:
* switch from linked-list chaining hash table to direct mapping
* cache check_win by hash
* persist transposition tables across plies (this was a big deal and difficult to puzzle out)
* add align directives to the network floats
* prepare_nn_input gained a small LUT to avoid branching (i suppose you might call this 1D embedding)
* apply mtune=native for the native builds
On the correctness side:
* Zobrist has now includes the ply parity
* the network now predicts something in [-1, 1] using tanh (approximated by polynomials)
* the training object is now MSE because we're learning a value estimate
* use .clangd instead of compile_commands.json
On the training side:
* completely forgo human-derived data set
* implement td(lambda), self-play, tournaments
Diffstat (limited to 'include/nn.c')
| -rw-r--r-- | include/nn.c | 124 |
1 files changed, 0 insertions, 124 deletions
diff --git a/include/nn.c b/include/nn.c deleted file mode 100644 index a837a59..0000000 --- a/include/nn.c +++ /dev/null @@ -1,124 +0,0 @@ -/* - This file is part of ct. - - This program is free software: you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation, either version 3 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. - - You should have received a copy of the GNU General Public License - along with ct. If not, see <https://www.gnu.org/licenses/>. -*/ - -#include "nn.h" -#include "tak.h" -#include "weights_5.h" -#include "weights_6.h" - -// =================================================================== -// Helpers -// =================================================================== - -#define RELU(x) ((x) = ((x) < 0) ? 0 : (x)) - -static inline void prepare_input(float *cur_board, tak_state_p stat); - -// =================================================================== -// Implementation of small neural networks -// =================================================================== - -float nn1986_evaluate_black_win(tak_state_p state) { - static float cur_board[FIVE_INP_NUM]; - static float dense1[FIVE_DENSE_NUM]; - static float output[2]; - - prepare_input(cur_board, state); - /* ------------------ * - * First dense layer * - * ------------------ */ - for (unsigned int d1 = 0; d1 < FIVE_DENSE_NUM; d1++) { - dense1[d1] = five_dense_biases[d1]; - for (unsigned int fl = 0; fl < FIVE_INP_NUM; fl++) { - dense1[d1] += cur_board[fl] * five_dense_weights[d1][fl]; - } - RELU(dense1[d1]); - } - /* ------------- * - * Output layer * - * ------------- */ - for (uint8_t k = 0; k < 2; k++) { - output[k] = five_output_bias[k]; - for (unsigned int d2 = 0; d2 < FIVE_DENSE_NUM; d2++) { - output[k] += dense1[d2] * five_output_weights[k][d2]; - } - RELU(output[k]); - } - const float norm = output[0] + output[1]; - return (2 * output[0] / norm) - 1; -} - -float nn2690_evaluate_black_win(tak_state_p state) { - static float cur_board[SIX_INP_NUM]; - static float dense1[SIX_DENSE_NUM]; - static float output[2]; - - prepare_input(cur_board, state); - /* ------------------ * - * First dense layer * - * ------------------ */ - for (unsigned int d1 = 0; d1 < SIX_DENSE_NUM; d1++) { - dense1[d1] = six_dense_biases[d1]; - for (unsigned int fl = 0; fl < SIX_INP_NUM; fl++) { - dense1[d1] += cur_board[fl] * six_dense_weights[d1][fl]; - } - RELU(dense1[d1]); - } - /* ------------- * - * Output layer * - * ------------- */ - for (uint8_t k = 0; k < 2; k++) { - output[k] = six_output_bias[k]; - for (unsigned int d2 = 0; d2 < SIX_DENSE_NUM; d2++) { - output[k] += dense1[d2] * six_output_weights[k][d2]; - } - RELU(output[k]); - } - const float norm = output[0] + output[1]; - return (2 * output[0] / norm) - 1; -} - -// =================================================================== -// Helper implementation -// =================================================================== - -static inline void prepare_input(float *cur_board, tak_state_p state) { - /* --------------- * - * Populate input * - * --------------- */ - for (unsigned int y = 0; y < state->board_size; y++) { - for (unsigned int x = 0; x < state->board_size; x++) { - const unsigned int loc = x + y * state->board_size; - const unsigned int count = COUNT_AT(state, loc); - float lookup = 0; - if (count > 0) { - if (STONE_AT(state, loc) == STONE_STANDING) { - lookup = (state->colours[loc] & 1) ? +0.25 : -0.25; - } else if (STONE_AT(state, loc) == STONE_CAPSTONE) { - lookup = (state->colours[loc] & 1) ? +1.00 : -1.00; - } else { - lookup = (state->colours[loc] & 1) ? +0.50 : -0.50; - } - } - cur_board[3 + loc] = lookup; - } - } - // Add input of flat counts and ply parity - cur_board[0] = (state->ply & 1) ? 1 : -1; - cur_board[1] = (float)(state->white_count & 127) / 21.0; - cur_board[2] = (float)(state->black_count & 127) / 21.0; -} |
