diff options
-rw-r--r-- | imago/engine/keras/neuralNetwork.py | 27 | ||||
-rw-r--r-- | imago/sgfParser/astNode.py | 1 | ||||
-rw-r--r-- | imago/sgfParser/sgf.py | 11 | ||||
-rwxr-xr-x | imago/sgfParser/sgflex.py | 2 | ||||
-rwxr-xr-x | imago/sgfParser/sgfyacc.py | 36 | ||||
-rwxr-xr-x | sgfyacc.py | 67 |
6 files changed, 112 insertions, 32 deletions
diff --git a/imago/engine/keras/neuralNetwork.py b/imago/engine/keras/neuralNetwork.py index 7a712f2..d0b7798 100644 --- a/imago/engine/keras/neuralNetwork.py +++ b/imago/engine/keras/neuralNetwork.py @@ -4,19 +4,23 @@ import os.path import tensorflow as tf from tensorflow import keras -from tensorflow.keras.models import Sequential +from tensorflow.keras.models import Sequential, load_model from tensorflow.keras.layers import Activation, Dense from tensorflow.keras.optimizers import Adam from tensorflow.keras.metrics import categorical_crossentropy -modelFile = 'models/medical_trial_model.h5' +modelFile = 'models/imagoKerasModel.h5' +DEF_BOARD_SIZE = 9 class NeuralNetwork: - def __init__(self, boardSize): + def __init__(self, modelPath="", boardSize=DEF_BOARD_SIZE): self.path = modelFile + if modelPath != "": + self.model = self._loadModel(modelPath) + else: + self.model = self._initModel(boardSize) - def _initModel(self, modelPath, boardSize=9): - # Save full model + def _initModel(self, boardSize=DEF_BOARD_SIZE): model = Sequential([ Dense(units=16, activation='relu', input_shape=(boardSize,boardSize)), Dense(units=32, activation='relu'), @@ -31,7 +35,10 @@ class NeuralNetwork: metrics=['accuracy'] ) - model.fit( + return model + + def _trainModel(self, boardSize=DEF_BOARD_SIZE): + self.model.fit( x=scaled_train_samples, y=train_labels, validation_split=0.1, @@ -41,12 +48,12 @@ class NeuralNetwork: verbose=2 ) - model.save(modelPath) - def _loadModel(self, modelPath): # Load model if os.path.isfile(modelPath): - from tensorflow.keras.models import load_model - model = load_model(modelPath) + return load_model(modelPath) else: raise Exception("Keras neural network file not found at %s" % modelPath) + + def _saveModel(self, modelPath): + self.model.save(modelPath) diff --git a/imago/sgfParser/astNode.py b/imago/sgfParser/astNode.py index ec28c2a..e92223b 100644 --- a/imago/sgfParser/astNode.py +++ b/imago/sgfParser/astNode.py @@ -1,4 +1,3 @@ -from imago.gameLogic.gameTree import GameTree from imago.gameLogic.gameData import GameData from imago.gameLogic.gameMove import GameMove from imago.gameLogic.gameState import Player diff --git a/imago/sgfParser/sgf.py b/imago/sgfParser/sgf.py index b5c0be2..3263650 100644 --- a/imago/sgfParser/sgf.py +++ b/imago/sgfParser/sgf.py @@ -1,11 +1,10 @@ """Module for reading and writing of SGF files.""" -from imago.gameLogic.gameTree import GameTree from imago.gameLogic.gameMove import GameMove -# def loadGameTree(filename): +def loadGameTree(filename): # PLY? -# """Parses a GameTree instance from a source SGF file.""" -# sgf = open(filename, "r") -# -# a = GameTree() + """Parses a GameTree instance from a source SGF file.""" + sgf = open(filename, "r") + + a = GameTree() diff --git a/imago/sgfParser/sgflex.py b/imago/sgfParser/sgflex.py index 07fb404..e102447 100755 --- a/imago/sgfParser/sgflex.py +++ b/imago/sgfParser/sgflex.py @@ -53,6 +53,8 @@ def t_DOUBLE(t): def t_PROPVALUE(t): r'\[[^\]]*\]' + import pdb + pdb.set_trace() t.value = t.value.strip('[').strip(']') return t diff --git a/imago/sgfParser/sgfyacc.py b/imago/sgfParser/sgfyacc.py index bd00df6..e48e6b9 100755 --- a/imago/sgfParser/sgfyacc.py +++ b/imago/sgfParser/sgfyacc.py @@ -1,4 +1,4 @@ -#!/bin/python +#!/usr/bin/python # -------------------------------------- # sgyacc.py @@ -7,8 +7,8 @@ import ply.yacc as yacc -from sgflex import tokens -from astNode import ASTNode, Property +from imago.sgfParser.sgflex import tokens +from imago.sgfParser.astNode import ASTNode, Property def p_tree(p): '''tree : LPAREN node RPAREN @@ -47,15 +47,21 @@ def p_error(_): """Error rule for syntax errors""" print("Syntax error in input!") -# Build the parser -parser = yacc.yacc() - -while True: - try: - s = input('calc > ') - except EOFError: - break - if not s: - continue - result = parser.parse(s) - print(result.toString()) +def main(): + + # Build the parser + parser = yacc.yacc() + + s = "" + while True: + try: + s = input('calc > ') + except EOFError: + break + if not s: + continue + result = parser.parse(s) + print(result.toString()) + +if __name__ == '__main__': + main() diff --git a/sgfyacc.py b/sgfyacc.py new file mode 100755 index 0000000..e48e6b9 --- /dev/null +++ b/sgfyacc.py @@ -0,0 +1,67 @@ +#!/usr/bin/python + +# -------------------------------------- +# sgyacc.py +""" Parser for SGF """ +# -------------------------------------- + +import ply.yacc as yacc + +from imago.sgfParser.sgflex import tokens +from imago.sgfParser.astNode import ASTNode, Property + +def p_tree(p): + '''tree : LPAREN node RPAREN + | LPAREN tree RPAREN''' + p[0] = p[2] + +def p_node_sequence(p): + '''node : node node''' + p[1].addToSequence(p[2]) + p[0] = p[1] + +def p_node_tree(p): + '''node : node tree''' + p[1].children.append(p[2]) + p[0] = p[1] + +def p_node(p): + 'node : SCOLON' + p[0] = ASTNode() + +def p_node_prop(p): + 'node : node property' + p[1].props[p[2].name] = p[2].value + p[0] = p[1] + +def p_property(p): + 'property : PROPID PROPVALUE' + p[0] = Property(p[1], p[2]) + +def p_property_value(p): + 'property : property PROPVALUE' + p[1].addValue(p[2]) + p[0] = p[1] + +def p_error(_): + """Error rule for syntax errors""" + print("Syntax error in input!") + +def main(): + + # Build the parser + parser = yacc.yacc() + + s = "" + while True: + try: + s = input('calc > ') + except EOFError: + break + if not s: + continue + result = parser.parse(s) + print(result.toString()) + +if __name__ == '__main__': + main() |