blob: 592b0b16b2737ca8056bc40074cedac2f8eb17c5 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
|
#include "ct1973.h"
float flattened[CONV_NUM+2];
float dense1[DENSE1_NUM];
float dense2[DENSE2_NUM];
float output[OUTPUT_NUM];
#define RELU(x) ((x) = ((x)<0)?0:(x))
float
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+by+(ky+bx)*board_size;
// 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<<c)) ? +0.50 : -0.50;
}
}
flattened[kern+KERN_NUM*(bx+by*KERN_OSIZE)]
+= lookup*conv2d_weights[kern][ky][kx][c];
}
}
}
RELU(flattened[kern+KERN_NUM*(bx+by*KERN_OSIZE)]);
}
}
}
// Add input of flat counts
flattened[CONV_NUM] = (float)(white_count & 127)/21.0;
flattened[CONV_NUM+1] = (float)(black_count & 127)/21.0;
/* ------------------ *
* First dense layer *
* ------------------ */
for (uint8_t d1 = 0; d1 < DENSE1_NUM; d1++) {
dense1[d1] = dense1_biases[d1];
for (uint8_t fl = 0; fl < CONV_NUM+2; fl++) {
dense1[d1] += flattened[fl]*dense1_weights[d1][fl];
}
RELU(dense1[d1]);
}
/* ------------------- *
* Second dense layer *
* ------------------- */
for (uint8_t d2 = 0; d2 < DENSE2_NUM; d2++) {
dense2[d2] = dense2_biases[d2];
for (uint8_t d1 = 0; d1 < DENSE1_NUM; d1++) {
dense2[d2] += dense1[d1]*dense2_weights[d2][d1];
}
RELU(dense2[d2]);
}
/* ------------- *
* Output layer *
* ------------- */
float norm = 0;
for (uint8_t out = 0; out < OUTPUT_NUM; out++) {
output[out] = output_biases[out];
for (uint8_t d2 = 0; d2 < DENSE2_NUM; d2++) {
output[out] += dense2[d2]*output_weights[out][d2];
}
output[out] = exp(output[out]);
norm += output[out];
}
return output[1]/norm;
}
|