From c2227f2c115ee536ffa22b32866d3a68437dba73 Mon Sep 17 00:00:00 2001 From: tslil Date: Wed, 6 Jan 2021 00:43:18 -0500 Subject: On the hunt for a better representation --- extract.sh | 50 ++++++++++++++++++++++++++------------------------ src/pptdb.c | 17 +++++++++-------- src/train5.py | 38 ++++++++++++++++++++------------------ 3 files changed, 55 insertions(+), 50 deletions(-) diff --git a/extract.sh b/extract.sh index 4ded1c0..60e01f9 100755 --- a/extract.sh +++ b/extract.sh @@ -10,38 +10,40 @@ and (result != '1/2-1/2');" extract() { cd data + if [ ! -f "$db_file" ]; then + wget "https://www.playtak.com/games_anon.db" + fi for size in 5 6; do echo Extracing games of size "$size"... sqlite3 "$db_file" "$(query $1 $size)" | shuf > "playtak-$size" done } -if [ ! -f "$db_file" ]; then - wget "https://www.playtak.com/games_anon.db" -fi - -extract notation,result +process() { + echo -e "Beginning to process data\n" + for i in 5 6; do + echo "Size $i..." + ./pptdb "$i" "data/playtak-$i" > "data/check-$i" + tail -n21 "data/check-$i" + echo Stripping overflows and illegal games... + grep -Fvxf "data/check-$i" "data/playtak-$i" > "data/good-playtak-$i" + echo -n "Generating training data... " + ./pptdb "$i" "data/good-playtak-$i" generate + echo "Shuffling data..." + shuf "data/training-$i.csv" > "data/shuf-$i.csv" + mv "data/shuf-$i.csv" "data/training-$i.csv" + echo + done +} -echo Preparing pptdb +# extract notation,result if [ ! -f "pptdb" ]; then - make pptdb + echo Preparing pptdb + make pptdb fi -echo -e "Beginning to process data\n" - -for i in 5 6; do - echo "Size $i..." - ./pptdb "$i" "data/playtak-$i" > "data/check-$i" - tail -n21 "data/check-$i" - echo Stripping overflows and illegal games... - grep -Fvxf "data/check-$i" "data/playtak-$i" > "data/good-playtak-$i" - echo -n "Generating training data... " - ./pptdb "$i" "data/good-playtak-$i" generate - echo "Shuffling data..." - shuf "data/training-$i.csv" > "data/shuf-$i.csv" - mv "data/shuf-$i.csv" "data/training-$i.csv" - echo -done +# process -shuf -n50000 data/good-playtak-5 > data/smalltak-5 && ./pptdb 5 data/smalltak-5 generate && \ -head -n1 data/training-5.csv > t && tail -n+2 data/training-5.csv > test.csv && shuf test.csv >> t && mv t test.csv +make pptdb +shuf -n10000 data/good-playtak-5 > data/smalltak-5 && ./pptdb 5 data/smalltak-5 generate && \ +head -n1 data/training-5.csv > t && tail -n+2 data/training-5.csv > data/train5.csv && shuf data/train5.csv >> t && mv t data/train5.csv diff --git a/src/pptdb.c b/src/pptdb.c index 712aabc..7a26ffd 100644 --- a/src/pptdb.c +++ b/src/pptdb.c @@ -19,28 +19,29 @@ write_input(void) { // Two layers of board_size * board_size: float t; int h; uint16_t mask; for (int k = 0; k < board_size * board_size; k++) { - // stacks encoded as balanced ternary, without caps and walls + // stacks encoded as balanced ternary t = 0; h = COUNT_AT(k); + if (h>0) { mask = 1<<(h-1); - /* if (STONE_AT(k) != STONE_FLAT) h--; */ while (h-->0) { - t += (colours[k] & mask) ? +1.0 : -1.0; - t /= 3; + t += (colours[k] & mask) ? +1 : -1; + t/=3; mask >>= 1; } } fprintf(training_fh,"%.6f,",t); } - int val; + float val; for (int k = 0; k < board_size * board_size; k++) { val = 0; if (COUNT_AT(k)) { - if (STONE_AT(k) == STONE_STANDING) val = (colours[k] & 1) ? +1 : -1; - else if (STONE_AT(k) == STONE_CAPSTONE) val = (colours[k] & 1) ? +2 : -2; + if (STONE_AT(k) == STONE_STANDING) val = (colours[k] & 1) ? +1/3 : -1/3; + else if (STONE_AT(k) == STONE_CAPSTONE) val = (colours[k] & 1) ? +1.0 : -1.0; + else val = (colours[k] & 1) ? +2/3 : -2/3; } - fprintf(training_fh,"%d,",val); + fprintf(training_fh,"%.6f,",val); } } diff --git a/src/train5.py b/src/train5.py index 225068b..3718fd0 100644 --- a/src/train5.py +++ b/src/train5.py @@ -12,40 +12,42 @@ from tensorflow.keras.layers import Dropout from tensorflow.keras.layers import Dense model = Sequential([ - Conv2D(5, kernel_size=3, padding='same', input_shape=(5, 5, 2)), - MaxPooling2D(pool_size=(2, 2), strides=None), - Activation("relu"), - Flatten(), - Dense(10, activation="relu"), - Dense(8, activation="relu"), - Dense(1, activation="sigmoid") - - # Dense(40, activation="relu", input_shape=(52,)), + # Conv2D(5, kernel_size=5, padding='same', input_shape=(5, 5, 2)), + # MaxPooling2D(pool_size=2, strides=2), + # Activation("relu"), + # Flatten(), + # Dense(25, activation="relu"), # Dense(10, activation="relu"), - # Dense(5, activation="relu"), # Dense(1, activation="sigmoid") + + Dense(30, activation="relu", input_shape=(52,)), # 30 + Dense(10, activation="relu"), # 10 + Dense(8, activation="relu"), # 8 + Dense(1, activation="sigmoid") ]) model.compile(loss='binary_crossentropy', optimizer='adam', metrics=['accuracy']) model.summary() -csv = pd.read_csv("data/test.csv") +csv = pd.read_csv("data/train5.csv") -training_data = csv.copy().head(5000) +training_data = csv.copy().head(40000) training_outcome = training_data.pop('Outcome') training_input = training_data.copy() -training_input = training_input.drop(columns=training_data.keys()[0:2]) +# training_input = training_input.drop(columns=training_data.keys()[0:2]) train = np.array(training_input) -train = train.reshape((training_input.shape[0], 5, 5, 2)) +# train = train.reshape((training_input.shape[0], 5, 5, 2)) -model.fit(train, training_outcome, epochs=20) +model.fit(train, training_outcome, epochs=100) -test_data = csv.copy().head(10000) -test_data = test_data.drop(columns=test_data.keys()[0:2]) +test_data = csv.copy().tail(50000) +# test_data = test_data.drop(columns=test_data.keys()[0:2]) test_outcome = test_data.pop('Outcome') test_input = np.array(test_data) -test_input.reshape((test_data.shape[0], 5, 5, 2)) +# test_input = test_input.reshape((test_data.shape[0], 5, 5, 2)) test_loss, test_acc = model.evaluate(test_input, test_outcome, verbose=2) print('\nTest accuracy:', test_acc) + +# Test accuracy: 0.67448 -- cgit v1.2.3