aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/cnn_train.py59
-rw-r--r--src/pptdb.c2
2 files changed, 13 insertions, 48 deletions
diff --git a/src/cnn_train.py b/src/cnn_train.py
index 0580b03..1264e9f 100644
--- a/src/cnn_train.py
+++ b/src/cnn_train.py
@@ -40,27 +40,17 @@ from tensorflow.keras.layers import Convolution2D
from tensorflow.keras.layers import Flatten
-# We need something that's close to logistic, but cheaper to compute.
-# (12.0+x+50.0*x/(x*x+10.0))/24.0, clipped between 0 and 1
-# as it would otherwise exceed this range at +- 4.6 or so
+# We need something that's close to 2*logistic-1, but cheaper to
+# compute: (12+x+50*x/(x*x+10))/12-1, clipped between -1 and 1 as it
+# would otherwise exceed this range at +- 4.6 or so
def clipped_pade_logistic(x):
val = add(k(12.0),
add(x, multiply(k(50.0),
divide(x, add(square(x), k(10.0))))))
- return clip_by_value(divide(val, k(24.0)), 0.0, 1.0)
+ return clip_by_value(add(k(-1.0), divide(val, k(12.0))), -1.0, +1.0)
-# A cheaper version of tanh, x/6+25*x/(6*(2*x*x+5)),
-# clipped between -1 and 1.
-def clipped_pade_tanh(x):
- val = add(divide(x, k(6.0)),
- multiply(k(25.0),
- divide(x, multiply(k(6.0), add(k(5.0),
- multiply(k(2.0), square(x)))))))
- return clip_by_value(val, -1.0, 1.0)
-
-
-def make_model(size, magic=[12, 9, 9]):
+def make_model(size, magic=[12, 11, 8]):
# Our model for the stacks, a small CNN
stack_shape = (size, size, 6)
stack_input = Input(shape=stack_shape)
@@ -76,11 +66,7 @@ def make_model(size, magic=[12, 9, 9]):
model = Dense(magic[2], activation="relu", use_bias=True)(model)
model = Dense(1, activation=clipped_pade_logistic, use_bias=True)(model)
model = Model(inputs=[stack_model.input, flats_input], outputs=model)
- model.compile(optimizer='adam',
- loss='mean_squared_error',
- # loss='mean_absolute_error',
- # loss='mean_absolute_percentage_error',
- metrics=['accuracy'])
+ model.compile(optimizer='adam', loss='mean_squared_error')
model.summary()
return model
@@ -89,7 +75,7 @@ def load_data(size):
shape = (-1, size, size, 6)
# Load training data
tr_fn = "training-"+str(size)+".csv"
- training_csv = pd.read_csv(tr_fn) #.head(100000)
+ training_csv = pd.read_csv(tr_fn)
training_data = training_csv
training_stack_input = np.array(training_data.iloc[:, 2:-1]).reshape(shape, order='F')
training_flats_input = np.array(training_data.iloc[:, 0:2])
@@ -113,36 +99,17 @@ def train(size, model, data, iterations=1, epochs=10):
model.fit(training_input, training_outcome, epochs=epochs,
validation_data=(val_input, val_outcome),
verbose=True)
- v_loss, v_accuracy = model.evaluate(val_input, val_outcome,
- verbose=False, batch_size=16)
- t_loss, t_accuracy = model.evaluate(training_input, training_outcome,
- verbose=False, batch_size=32)
- results += [((v_loss, t_loss), (v_accuracy, t_accuracy))]
+ v_loss = model.evaluate(val_input, val_outcome,
+ verbose=False, batch_size=16)
+ t_loss = model.evaluate(training_input, training_outcome,
+ verbose=False, batch_size=32)
+ results += [(v_loss, t_loss)]
print("\nScores")
for i in range(len(results)):
print("Iteration {0}: {1}".format(i+1, results[i]))
return results
-def model_size(model):
- return sum([np.prod(get_value(w).shape) for w in model.trainable_weights])
-
-
-def magic_search(data):
- results = []
- for width in range(9, 18):
- for dense1 in range(9, 16):
- for dense2 in range(9, max(32, dense1*2)):
- m = make_model(5, [width, dense1, dense2])
- if model_size(m) < 1990:
- results += [([width, dense1, dense2],
- train(5, m, data, iterations=1, epochs=20))]
- print("\nSummary")
- for r in results:
- print("Parameters {0}: {1}".format(r[0], r[1][0]))
- return results
-
-
def write_weights(model):
def fix(val):
string = str(np.array(val).tolist())
@@ -185,5 +152,3 @@ data = load_data(5)
model = make_model(5, [12, 11, 8])
results = train(5, model, data, iterations=5, epochs=10)
write_weights(model)
-
-# results = magic_search(data)
diff --git a/src/pptdb.c b/src/pptdb.c
index ecf5caa..557b894 100644
--- a/src/pptdb.c
+++ b/src/pptdb.c
@@ -198,7 +198,7 @@ int main(int argc, char **argv) {
reset_state(size);
// Store the outcome of this game. Black win = 1
if (line[read-4] == '0') outcome_black = 1;
- else outcome_black = 0;
+ else outcome_black = -1;
// Parse the line
r = parse_line(line,read-4);
// Adjust counts if we're not generating training data