-
Notifications
You must be signed in to change notification settings - Fork 2
/
Model.py
139 lines (107 loc) · 4.34 KB
/
Model.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
######################################################################################################
#
# Organization: Asociacion De Investigacion En Inteligencia Artificial Para La Leucemia Peter Moss
# Repository: GeniSysAI
# Project: Natural Language Understanding Engine
#
# Author: Adam Milton-Barker (AdamMiltonBarker.com)
#
# Title: Model Class
# Description: Model helper functions.
# License: MIT License
# Last Modified: 2020-10-01
#
######################################################################################################
import json
import os
import warnings
warnings.simplefilter(action='ignore', category=FutureWarning)
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
import numpy as np
import tensorflow as tf
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)
from Classes.Helpers import Helpers
from Classes.Data import Data
class Model():
""" Model Class
Model helper functions.
"""
def __init__(self):
""" Initializes the class. """
self.Helpers = Helpers("Model")
self.Data = Data()
self.Helpers.logger.info("Model class initialized.")
def createDNN(self, x, y):
""" Sets up the DNN layers """
tf_model = tf.keras.models.Sequential([
tf.keras.layers.Dense(
self.Helpers.confs["NLU"]['FcUnits'], activation='relu', input_shape=[len(x[0])]),
tf.keras.layers.Dense(
self.Helpers.confs["NLU"]['FcUnits'], activation='relu'),
tf.keras.layers.Dense(
self.Helpers.confs["NLU"]['FcUnits'], activation='relu'),
tf.keras.layers.Dense(
self.Helpers.confs["NLU"]['FcUnits'], activation='relu'),
tf.keras.layers.Dense(
len(y[0]), activation=self.Helpers.confs["NLU"]['Activation'])
],
"GeniSysAI")
tf_model.summary()
self.Helpers.logger.info("Network initialization complete.")
return tf_model
def trainDNN(self, x, y, words, classes, intentMap):
""" Trains the DNN """
tf_model = self.createDNN(x, y)
optimizer = tf.keras.optimizers.Adam(lr=self.Helpers.confs["NLU"]["LR"],
decay=self.Helpers.confs["NLU"]["Decay"])
tf_model.compile(optimizer=optimizer,
loss='binary_crossentropy',
metrics=[tf.keras.metrics.BinaryAccuracy(name='acc'),
tf.keras.metrics.Precision(name='precision'),
tf.keras.metrics.Recall(name='recall'),
tf.keras.metrics.AUC(name='auc')])
tf_model.fit(x, y, epochs=self.Helpers.confs["NLU"]['Epochs'],
batch_size=self.Helpers.confs["NLU"]['BatchSize'])
self.saveModelData(
self.Helpers.confs["NLU"]['Model']['Data'],
{
'words': words,
'classes': classes,
'x': x,
'y': y,
'intentMap': [intentMap]
},
tf_model)
def saveModelData(self, path, data, tmodel):
""" Saves the model data """
with open(self.Helpers.confs["NLU"]['Model']['Model'], "w") as file:
file.write(tmodel.to_json())
self.Helpers.logger.info(
"Model JSON saved " + self.Helpers.confs["NLU"]['Model']['Model'])
with open(self.Helpers.confs["NLU"]['Model']['Data'], "w") as outfile:
json.dump(data, outfile)
tmodel.save_weights(self.Helpers.confs["NLU"]['Model']['Weights'])
self.Helpers.logger.info(
"Weights saved " + self.Helpers.confs["NLU"]['Model']['Weights'])
def buildDNN(self, x, y):
""" Loads the DNN model """
with open(self.Helpers.confs["NLU"]['Model']['Model']) as file:
m_json = file.read()
tmodel = tf.keras.models.model_from_json(m_json)
tmodel.load_weights(self.Helpers.confs["NLU"]['Model']['Weights'])
self.Helpers.logger.info("Model loaded ")
return tmodel
def predict(self, tmodel, parsedSentence, trainedWords, trainedClasses):
""" Makes a prediction """
predictions = [[index, confidence] for index, confidence in enumerate(
tmodel.predict([[
self.Data.makeBagOfWords(
parsedSentence,
trainedWords)]])[0])]
predictions.sort(key=lambda x: x[1], reverse=True)
classification = []
for prediction in predictions:
classification.append((trainedClasses[prediction[0]], prediction[1]))
return classification