-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdstc2_intent_prediction_function.py
81 lines (71 loc) · 3.25 KB
/
dstc2_intent_prediction_function.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
from keras.backend.tensorflow_backend import set_session
import tensorflow as tf
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list = '0'
set_session(tf.Session(config=config))
import fasttext
from .intent_models import cnn_word_model
from .intent_recognizer_class import IntentRecognizer
import os
import numpy as np
import re
SEED = 23
np.random.seed(SEED)
tf.set_random_seed(SEED)
class IntentRecognizerDSTC2(object):
def __init__(self, dpath):
fasttext_model_file = os.path.join(dpath, 'dstc2_fasttext_model_100.bin')
self.fasttext_model = fasttext.load_model(fasttext_model_file)
self.confident_threshold = 0.5
self.text_size = 15
self.embedding_size = 100
self.kernel_sizes = [1, 2, 3]
f = open(os.path.join(dpath, 'intents.txt'), 'r')
lines = f.readlines()
self.intents = np.array([line.rstrip() for line in lines])
self.n_classes = self.intents.shape[0]
f.close()
print('\n___Considered intents___\n', self.intents)
self.Recognizer = IntentRecognizer(self.intents, fasttext_embedding_model=self.fasttext_model, n_splits=1)
self.Recognizer.init_network_parameters([{'coef_reg_cnn': 0.00028397089135487048,
'coef_reg_den': 0.0048999053790332739,
'filters_cnn': 170,
'dense_size': 141, 'dropout_rate': 0.4051841045170547}])
self.Recognizer.init_learning_parameters([{'batch_size': 30,
'lear_rate': 0.24946701435260424,
'lear_rate_decay': 0.36829336803894619,
'epochs': 68}])
self.Recognizer.init_model(cnn_word_model, self.text_size, self.embedding_size,
self.kernel_sizes, add_network_params=None)
self.Recognizer.load_model(fname=os.path.join(dpath, 'best_model__dstc2_1'),
loss='binary_crossentropy')
print('___Intent recognizer initialized___')
def predict(self, requests):
train_predictions = self.Recognizer.predict([requests])[0]
return train_predictions
def _predictions2text(self, predictions):
y = []
for sample in predictions:
to_add = np.where(sample > self.confident_threshold)[0]
if len(to_add) > 0:
y.append(self.intents[to_add])
else:
y.append([self.intents[np.argmax(sample)]])
y = np.asarray(y)
return y
def _text2predictions(self, predictions):
eye = np.eye(self.n_classes)
y = []
for sample in predictions:
curr = np.zeros(self.n_classes)
for intent in sample:
curr += eye[np.where(self.intents == intent)[0]].reshape(-1)
y.append(curr)
y = np.asarray(y)
return y
def _proba2labels(self, predictions):
labels = predictions.copy()
for i in range(predictions.shape[0]):
labels[i, :] = 1 * (labels[i, :] > self.confident_threshold)
return labels