def parser(): DarkLogic.init(0) db = Database("Test/dbtest.csv") dbStates = db.getDatas() print("Total number of theorems in database: " + str(len(dbStates))) for dbState in dbStates.values(): thCreated = DarkLogic.makeTheorem(dbState.theoremName(), dbState.theoremContent()) assert thCreated, "Theorem name: " + dbState.theoremName() + ", " \ "content: " + dbState.theoremContent() + " has not been created" dlContent = DarkLogic.toStrTheorem() assert dlContent == dbState.theoremContent(), "Bad parsing! content in darklogic is '"\ + dlContent + "', but original was " + dbState.theoremContent()
class NeuralAI(AI): MaxNbNode = 100 NbDiffOperators = 7 OperatorParams = NbDiffOperators + 2 NbOperators = 30 NbTerms = NbOperators OpeStateSize = NbOperators + NbDiffOperators MaxDepth = 25 MultExamples = 500 MaxGameBefLearning = 50 INIT_LR = 4 * 10 ** -6 ModelFile = "AI/deepAIModel" DbName = "Database/deepaiMemory.csv" def __init__(self, maxInstanceIdx, secondTimeout): super().__init__(maxInstanceIdx, secondTimeout, name="NeuralAI") self._theoremName = "" self._theorem = "" # get Rule States self._trueRuleStates = [] ruleStates = DarkLogic.getRuleStates() for ruleState in ruleStates: self._trueRuleStates.append(makeTrueState(ruleState)) self._inputSize = (len(self._trueRuleStates) + 1) * NeuralAI.NbOperators * 19 self._storeNodes = [] self._db = Database(NeuralAI.DbName) self._gamesSinceLastLearning = 0 if file_io.file_exists(NeuralAI.ModelFile): print("load DeepAI brain model") self._model = keras.models.load_model(NeuralAI.ModelFile) else: # create model print("create new DeepAI brain model") self._model = createModel(len(self._trueRuleStates) + 1) compileModel(self._model, NeuralAI.INIT_LR) self._model.save(NeuralAI.ModelFile) self._model = extractTestModel(self._model) self._modelMutex = Lock() self._elo = 1076 # 1418 # self._train() def getTrueState(self, threadIdx): return [makeTrueState(DarkLogic.getState(threadIdx))] + self._trueRuleStates """def getTrueState(self): return [makeTrueState(DarkLogic.getState())] + self._trueRuleStates""" def setTheoremInfo(self): self._theoremName = DarkLogic.theoremName() self._theorem = DarkLogic.toNormStrTheorem() """ nodeLists: - type is list of list of node """ def evaluate(self, nodes, trueStates): # evaluate states trueStates = np.array(trueStates) self._modelMutex.acquire() out = self._model.predict(trueStates, batch_size=len(trueStates), workers=multiprocessing.cpu_count(), use_multiprocessing=True) self._modelMutex.release() realOuts = eval(out) for node, realOut in zip(nodes, realOuts): node.setAIValue(realOut) def explore(self, dbNode, threadId): self._crtNode.exploreDeep(dbNode.actions(), threadId) def _train(self): x = [] y = [] print("DeepAI is preparing for training...") # node.getTrainNodes(x, y) dbStates = self._db.getDatas() nbExcludedTh = 0 class_nb = {} for cl in range(NeuralAI.MaxDepth + 1): class_nb[cl] = 0 print("Total number of theorems in database: " + str(len(dbStates))) dbStateIdx = -1 remDbStates = list(dbStates.values()) rand.shuffle(remDbStates) # NbMax = 200000 NbMax = 200000 if NbMax < len(dbStates): NbMaxUnevaluatedThm = NbMax - self._db.nbEvaluatedThm() if NbMax > self._db.nbEvaluatedThm() else 0 NbMaxEvaluatedThm = NbMax - NbMaxUnevaluatedThm else: NbMaxUnevaluatedThm = len(dbStates) - self._db.nbEvaluatedThm() NbMaxEvaluatedThm = self._db.nbEvaluatedThm() print("Must select " + str(NbMaxUnevaluatedThm) + " unevaluated theorems") print("Must select " + str(NbMaxEvaluatedThm) + " evaluated theorems") NbEvaluated = 0 NbUnevaluated = 0 lastEvalPrint = 0 lastUnevalPrint = 0 for dbState in remDbStates: dbStateIdx += 1 if NbEvaluated > lastEvalPrint and NbEvaluated % 10000 == 0: lastEvalPrint = NbEvaluated print(str(NbEvaluated) + " evaluated theorems have been seen") if NbUnevaluated > lastUnevalPrint and NbUnevaluated % 10000 == 0: lastUnevalPrint = NbUnevaluated print(str(NbUnevaluated) + " unevaluated theorems have been seen") DarkLogic.makeTheorem(dbState.theoremName(), dbState.theoremContent()) state = DarkLogic.getState() DarkLogic.clearAll() if len(state.operators()) > NeuralAI.NbOperators: if dbState.isEvaluated() and NbMaxEvaluatedThm == self._db.nbEvaluatedThm(): NbMaxUnevaluatedThm += 1 NbMaxEvaluatedThm -= 1 continue if dbState.isEvaluated(): if NbEvaluated == NbMaxEvaluatedThm: continue cl = dbState.value() if dbState.value() < NeuralAI.MaxDepth else NeuralAI.MaxDepth class_nb[cl] += 1 l = list(range(len(self._trueRuleStates))) rand.shuffle(l) x.append([makeTrueState(state), l]) # y.append(nthColounmOfIdentiy(cl)) y.append(cl) NbEvaluated += 1 if NbUnevaluated == NbMaxUnevaluatedThm and NbEvaluated == NbMaxEvaluatedThm: break else: if NbUnevaluated == NbMaxUnevaluatedThm: continue l = list(range(len(self._trueRuleStates))) rand.shuffle(l) x.append([makeTrueState(state), l]) # y.append(createZeroTab(DeepAI.MaxDepth + 1)) y.append(-1) NbUnevaluated += 1 if NbUnevaluated == NbMaxUnevaluatedThm and NbEvaluated == NbMaxEvaluatedThm: break print("Selected " + str(NbUnevaluated) + " unevaluated theorems") print("Selected " + str(NbEvaluated) + " evaluated theorems") # if we keep some examples if len(x): # check class_weight class_nb[-1] = 1 / NbUnevaluated print("Keep " + str(len(x)) + " examples") class_weights = {} for val in class_nb: nb_cl = class_nb[val] if nb_cl >= len(x) - 1: print("[WARNING] Useless to train if almost all examples are from one class! Exit") return if nb_cl != 0: class_weights[val] = 1 / nb_cl else: class_weights[val] = 0 # shuffle examples print("shuffle " + str(len(x)) + " examples ...") randList = list(range(len(x))) newX = [] newY = [] newValues = [] for pos in range(len(x)): newX.append(x[pos]) newY.append(y[pos]) x = newX y = newY values = newValues # prepare for training batch_size = 100 nb_epochs = 1000 pos = int(0.9 * len(x)) # x = np.array(x) # y = np.array(y) values = np.array(values) x_train = x[:pos] x_test = x[pos:] y_train = y[:pos] y_test = y[pos:] print("training on " + str(len(x_train)) + " examples") # earlyStop = keras.callbacks.EarlyStopping(monitor='val_loss', min_delta=0.001, patience=20, verbose=1) trainBatches_x = [] trainBatches_y = [] testBatches_x = [] testBatches_y = [] batch_x = [] batch_y = [] # prepare train batches for k in range(len(x_train)): batch_x.append(x_train[k]) batch_y.append(y_train[k]) if len(batch_x) == batch_size: trainBatches_x.append(batch_x) batch_x = [] trainBatches_y.append(batch_y) batch_y = [] if len(batch_x) > 0: trainBatches_x.append(batch_x) batch_x = [] trainBatches_y.append(batch_y) batch_y = [] # prepare test batches for k in range(len(x_test)): batch_x.append(x_test[k]) batch_y.append(y_test[k]) if len(batch_x) == batch_size: testBatches_x.append(batch_x) batch_x = [] testBatches_y.append(batch_y) batch_y = [] if len(batch_x) > 0: testBatches_x.append(batch_x) batch_x = [] testBatches_y.append(batch_y) batch_y = [] # fit lr = NeuralAI.INIT_LR minLoss = 10 ** 100 lastDecLoss = 0 # last epoch since loss has decreased # init minValLoss print("Validation of current model") if file_io.file_exists(NeuralAI.ModelFile): # load best model print("load last model") self._model = keras.models.load_model(NeuralAI.ModelFile) compileModel(self._model, lr) print("__________________________________________________________________________") crtMinValLoss, val_acc = validation(self._model, testBatches_x, testBatches_y, batch_size, class_weights, self._trueRuleStates, self._inputSize) print("VAL_LOSS = " + str(crtMinValLoss)) print("VAL_ACCURACY = " + str(val_acc)) minValLoss = 10 ** 100 lastDecValLoss = 0 # last epoch since loss has decreased print("create new model") self._model = createModel(len(self._trueRuleStates) + 1) compileModel(self._model, lr) for epoch in range(nb_epochs): print("epoch n°" + str(epoch + 1) + "/" + str(nb_epochs)) # training... loss, accuracy = training(self._model, trainBatches_x, trainBatches_y, batch_size, class_weights, self._trueRuleStates, self._inputSize) print("LOSS = " + str(loss)) print("ACCURACY = " + str(accuracy)) if loss < minLoss: print("LOSS decreasing!") minLoss = loss lastDecLoss = 0 else: print("LOSS increasing!") lastDecLoss += 1 # validation... val_loss, val_accuracy = validation(self._model, testBatches_x, testBatches_y, batch_size, class_weights, self._trueRuleStates, self._inputSize) print("VAL_LOSS = " + str(val_loss)) print("VAL_ACCURACY = " + str(val_accuracy)) if val_loss < minValLoss: print("VAL_LOSS decreasing") minValLoss = val_loss lastDecValLoss = 0 if minValLoss < crtMinValLoss: print("Improvement compared to old model!!!") crtMinValLoss = minValLoss else: print("VAL_LOSS increasing") lastDecValLoss += 1 if lastDecLoss == 3: lr = lr / 10 print("adapt learning rate: " + str(lr)) compileModel(self._model, lr) lastDecLoss = 0 minLoss = loss # keep latest loss for minimal loss print("new current minimal loss: "+str(minLoss)) if lastDecValLoss == 10: print("Early-stopping!") break if val_loss <= crtMinValLoss: print("Save model") self._model.save(NeuralAI.ModelFile) print("_______________________________________________________________________________________") if file_io.file_exists(NeuralAI.ModelFile): # load best model print("load best model") self._model = keras.models.load_model(NeuralAI.ModelFile) self._model = extractTestModel(self._model) print("_______________________________________________________________________________________") def _storeCrtNode(self): self._storeNodes.append(self._crtNode) def meditate(self): if len(self._storeNodes) > 0: self._gamesSinceLastLearning += 1 DarkLogic.makeTheorem(self._theoremName, self._theorem) # update if deepAI found a demonstration revNodes = self._storeNodes[::-1] if revNodes[0].value() < NeuralAI.MaxDepth: val = revNodes[0].value() for k in range(1, len(revNodes)): node = revNodes[k] node.setValue(val + k) self._db.export(self._storeNodes[0].getDbStates()) if self._gamesSinceLastLearning == NeuralAI.MaxGameBefLearning: self._train() self._gamesSinceLastLearning = 0 self._storeNodes.clear() DarkLogic.clearAll() super().meditate() def getTrainingStates(self, val, x, y): trueState = makeTrueState(DarkLogic.getState()) hasToMult = True if val > NeuralAI.MaxDepth: val = NeuralAI.MaxDepth + 1 hasToMult = False trueOut = nthColounmOfIdentiy(val) # print("shape = "+str(np.shape(trueOut))) mult = NeuralAI.MultExamples if hasToMult else 1 for k in range(mult): crtState = [trueState] l = list(range(len(self._trueRuleStates))) rand.shuffle(l) for pos in l: crtState.append(self._trueRuleStates[pos]) x.append(crtState) y.append(trueOut) def getBestNode(self): return self._crtNode.getDeepBestNode() def value(self): if self._crtNode.isAIValuated(): return self._crtNode.aiValue() return self._crtNode.value() def canEvaluate(self, state): return len(state.operators()) < NeuralAI.NbOperators and len(state.terms()) < NeuralAI.NbTerms