Esempio n. 1
0
class Trainer():
    def __init__(self, jumpWay):

        self.Helpers = Helpers()
        self.Logging = Logging()
        self.jumpwayCl = jumpWay

        self._confs = self.Helpers.loadConfigs()
        self.LogFile = self.Logging.setLogFile(self._confs["AI"]["Logs"] +
                                               "Train/")

        self.Logging.logMessage(self.LogFile, "LogFile", "INFO",
                                "NLU Trainer LogFile Set")

        self.Model = Model()
        self.Data = Data(self.Logging, self.LogFile)
        self.intentMap = {}
        self.words = []
        self.classes = []
        self.dataCorpus = []

        self.setupData()
        self.setupEntities()

    def setupData(self):

        self.trainingData = self.Data.loadTrainingData()

        self.Logging.logMessage(self.LogFile, "Trainer", "INFO",
                                "Loaded NLU Training Data")

        self.words, self.classes, self.dataCorpus, self.intentMap = self.Data.prepareData(
            self.trainingData)
        self.x, self.y = self.Data.finaliseData(self.classes, self.dataCorpus,
                                                self.words)

        self.Logging.logMessage(self.LogFile, "TRAIN", "INFO",
                                "NLU Trainer Data Ready")

    def setupEntities(self):

        if self._confs["ClassifierSettings"]["Entities"] == "Mitie":

            self.entityExtractor = Entities()

            self.Logging.logMessage(self.LogFile, "TRAIN", "OK",
                                    "NLU Trainer Entity Extractor Ready")

            self.entityExtractor.trainEntities(
                self._confs["ClassifierSettings"]["Mitie"]["ModelLocation"],
                self.trainingData)

    def trainModel(self):

        while True:

            self.Logging.logMessage(self.LogFile, "TRAIN", "ACTION",
                                    "Ready To Begin Training ? (Yes/No)")

            userInput = input(">")

            if userInput == 'Yes': break
            if userInput == 'No': exit()

        humanStart, trainingStart = self.Helpers.timerStart()

        self.Logging.logMessage(self.LogFile, "TRAIN", "INFO",
                                "NLU Model Training At " + humanStart)

        self.jumpwayCl.publishToDeviceChannel(
            "Training", {
                "NeuralNet": "NLU",
                "Start": trainingStart,
                "End": "In Progress",
                "Total": "In Progress",
                "Message": "NLU Model Training At " + humanStart
            })
        self.Model.trainDNN(self.x, self.y, self.words, self.classes,
                            self.intentMap)

        trainingEnd, trainingTime, humanEnd = self.Helpers.timerEnd(
            trainingStart)

        self.Logging.logMessage(
            self.LogFile, "TRAIN", "OK", "NLU Model Trained At " + humanEnd +
            " In " + str(trainingEnd) + " Seconds")

        self.jumpwayCl.publishToDeviceChannel(
            "Training", {
                "NeuralNet":
                "NLU",
                "Start":
                trainingStart,
                "End":
                trainingEnd,
                "Total":
                trainingTime,
                "Message":
                "NLU Model Trained At " + humanEnd + " In " +
                str(trainingEnd) + " Seconds"
            })
Esempio n. 2
0
class Trainer():

    ###############################################################
    #
    # Sets up all default requirements and placeholders
    # needed for the NLU engine to run.
    #
    # - Helpers: Useful global functions
    # - JumpWay/jumpWayClient: iotJumpWay class and connection
    # - Logging: Logging class
    #
    ###############################################################

    def __init__(self, jumpWay):

        self.Helpers = Helpers()
        self._confs = self.Helpers.loadConfigs()
        self.LogFile = self.Helpers.setLogFile(self._confs["aiCore"]["Logs"] +
                                               "Train/")

        self.jumpwayCl = jumpWay

        self.intentMap = {}
        self.words = []
        self.classes = []
        self.dataCorpus = []

        self.Model = Model()
        self.Data = Data()

    def setupData(self):

        self.trainingData = self.Data.loadTrainingData()

        self.words, self.classes, self.dataCorpus, self.intentMap = self.Data.prepareData(
            self.trainingData)
        self.x, self.y = self.Data.finaliseData(self.classes, self.dataCorpus,
                                                self.words)

        self.Helpers.logMessage(self.LogFile, "TRAIN", "INFO",
                                "NLU Training Data Ready")

    def setupEntities(self):

        if self._confs["NLU"]["Entities"] == "Mitie":

            self.entityController = Entities()

            self.entityController.trainEntities(
                self._confs["NLU"]["Mitie"]["ModelLocation"],
                self.trainingData)

            self.Helpers.logMessage(self.LogFile, "TRAIN", "OK",
                                    "NLU Trainer Entities Ready")

    def trainModel(self):

        while True:

            self.Helpers.logMessage(self.LogFile, "TRAIN", "ACTION",
                                    "Ready To Begin Training ? (Yes/No)")

            userInput = input(">")

            if userInput == 'Yes': break
            if userInput == 'No': exit()

        self.setupData()
        self.setupEntities()

        humanStart, trainingStart = self.Helpers.timerStart()

        self.jumpwayCl.publishToDeviceChannel(
            "Training", {
                "NeuralNet": "NLU",
                "Start": trainingStart,
                "End": "In Progress",
                "Total": "In Progress",
                "Message": "NLU Model Training At " + humanStart
            })

        self.Model.trainDNN(self.x, self.y, self.words, self.classes,
                            self.intentMap)

        trainingEnd, trainingTime, humanEnd = self.Helpers.timerEnd(
            trainingStart)

        self.Helpers.logMessage(
            self.LogFile, "TRAIN", "OK", "NLU Model Trained At " + humanEnd +
            " In " + str(trainingEnd) + " Seconds")

        self.jumpwayCl.publishToDeviceChannel(
            "Training", {
                "NeuralNet":
                "NLU",
                "Start":
                trainingStart,
                "End":
                trainingEnd,
                "Total":
                trainingTime,
                "Message":
                "NLU Model Trained At " + humanEnd + " In " +
                str(trainingEnd) + " Seconds"
            })