示例#1
0
    def preExplore(self, nodeList, states):
        # play crt move
        DarkLogic.apply(self._threadId, self._actionId)

        # check if it is a node which leads to loss
        if DarkLogic.isAlreadyPlayed(self._threadId):
            self._value = Node.VAL_MAX
        elif not DarkLogic.canBeDemonstrated(self._threadId):
            self._value = Node.VAL_MAX
            if not DarkLogic.isEvaluated(self._threadId):
                self._isLoss = True
        # check if it is a node which leads to win
        elif DarkLogic.isDemonstrated(self._threadId):
            self._value = 0
            # stop reflexion because AI found a demonstration
            Node._ai.stopThread(self._threadId)
        else:
            actions = DarkLogic.getActions(self._threadId)
            for action in actions:
                self._sons[action] = None
            nodeList.append(self)
            if self._ai.canEvaluate(DarkLogic.getState(self._threadId)):
                states.append(Node._ai.getTrueState(self._threadId))
            else:
                self._aiValue = Node.VAL_INIT
        self._isEvaluated = True

        # unplay crt move
        DarkLogic.unapply(self._threadId)
示例#2
0
 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)
示例#3
0
    def eval(self, threadIdx):
        # play crt move
        DarkLogic.apply(threadIdx, self._actionId)

        # check if it is a node which leads to loss
        if DarkLogic.isAlreadyPlayed(threadIdx):
            self._value = Node.VAL_MAX
        elif not DarkLogic.canBeDemonstrated(threadIdx):
            self._value = Node.VAL_MAX
            if not DarkLogic.isEvaluated(threadIdx):
                self._isLoss = True
        # check if it is a node which leads to win
        elif DarkLogic.isDemonstrated(threadIdx):
            self._value = 0
            # stop reflexion because AI found a demonstration
            Node._ai.stopThread(threadIdx)
        else:
            self._subValue = Node._ai.eval([DarkLogic.getState(threadIdx)],
                                           threadIdx)
        self._isEvaluated = True

        # unplay crt move
        DarkLogic.unapply(threadIdx)
示例#4
0
    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("_______________________________________________________________________________________")
示例#5
0
 def getTrueState(self, threadIdx):
     return [makeTrueState(DarkLogic.getState(threadIdx))] + self._trueRuleStates