From 18496a371a2204e8c1b448921a87108f4aa7ada3 Mon Sep 17 00:00:00 2001 From: tslil clingman Date: Sat, 23 Jan 2021 18:20:05 -0500 Subject: Attempting Zobrist hashing --- include/cnn1986.c | 114 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 114 insertions(+) create mode 100644 include/cnn1986.c (limited to 'include/cnn1986.c') diff --git a/include/cnn1986.c b/include/cnn1986.c new file mode 100644 index 0000000..039dddd --- /dev/null +++ b/include/cnn1986.c @@ -0,0 +1,114 @@ +#include "cnn1986.h" +#include "weights.h" + +// =================================================================== +// Implementation of a small convolutional neural network +// =================================================================== + +static float flattened[CONV_NUM+2]; +static float dense1[DENSE1_NUM]; +static float dense2[DENSE2_NUM]; + +#ifndef DETERMINISTIC +union u_f { + uint32_t u; + float f; +}; + +static union u_f fudge; + +#define RANDF { \ + XORSHIFT; \ + fudge.u = 0x3f800000 | RANDOM32 >> 10; \ + fudge.f = (fudge.f - 1.5) * 0.01; \ + } +#endif + +#define RELU(x) ((x) = ((x)<0)?0:(x)) + +float cnn1986_evaluate_black_win(void) { + /* ------------------ * + * Convolution layer * + * ------------------ */ + // for each kernel + for (uint8_t kern = 0; kern < KERN_NUM; kern++) { + // the stride is 1, march across the board + for (uint8_t bx = 0; bx < KERN_OSIZE; bx++) { + for (uint8_t by = 0; by < KERN_OSIZE; by++) { + flattened[kern+KERN_NUM*(bx+by*KERN_OSIZE)] = + conv2d_biases[kern]; + // Compute the convolution for this position + for (uint8_t ky = 0; ky < KERN_SIZE; ky++) { + for (uint8_t kx = 0; kx < KERN_SIZE; kx++) { + for (uint8_t c = 0; c < KERN_CHAN; c++) { + // Where we are on the board + const uint8_t loc = kx+bx+(ky+by)*5; + // Look up what's on the board at this location, and + // multiply it. For c=0 we have to do some extra work + float lookup = 0; + if (COUNT_AT(loc)>c) { + if (c==0) { + if (STONE_AT(loc) == STONE_STANDING) { + lookup = (colours[loc] & 1) ? +0.25 : -0.25; + } else if (STONE_AT(loc) == STONE_CAPSTONE) { + lookup = (colours[loc] & 1) ? +1.00 : -1.00; + } else { + lookup = (colours[loc] & 1) ? +0.50 : -0.50; + } + } else { + lookup = (colours[loc] & (1< 1.0) { + return 1.0; + } + else if (output < 0.0) { + return -1.0; + } + return 2*output-1.0; +} -- cgit v1.2.3