This repository has been archived by the owner on Jan 12, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathTrain.py
156 lines (124 loc) · 4.43 KB
/
Train.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
############################################################################################
#
# The MIT License (MIT)
#
# GeniSys NLU Engine Trainer
# Copyright (C) 2018 Adam Milton-Barker (AdamMiltonBarker.com)
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
#
# Title: GeniSys NLU Engine Trainer
# Description: Trains the GeniSys NLU Engine
# Configuration: required/confs.json
# Training Data: data/training.json
# Last Modified: 2018-09-29
#
############################################################################################
import sys, os, json, re, time, warnings
import JumpWayMQTT.Device as jumpWayDevice
from tools.Helpers import Helpers
from tools.Logging import Logging
from tools.Data import Data
from tools.Model import Model
from tools.Mitie import Entities
if not sys.warnoptions:
warnings.simplefilter("ignore")
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"})