def predict(sentence): n_test = 1 test_vec_seq = [] test_seq = [] for i in range(n_test): test_text = sentence test_seq.append(test_text) test_doc = nlp(test_text) test_vec = [] for word in test_doc: test_vec.append(word.vector) test_vec_seq.append(test_vec) test_vec_seq = pad_vec_sequences(test_vec_seq) prediction = model.predict(test_vec_seq) label_predictions = np.zeros(prediction.shape) for i in range(n_test): m = max(prediction[i]) print(m) p = np.where(prediction[i] > 0.55 * m) q = np.where(prediction[i] == m) and np.where(prediction[i] > 0.34) label_predictions[i][p] = 1 label_predictions[i][q] = 2 #print (p,q) for i in range(n_test): for x in range(len(label_predictions[i])): if label_predictions[i][x] == 2: return sentence, labels[x] if len(set(label_predictions[i])) == 1: return sentence, "NO INTENT" return sentence, "NO INTENT"
def make_data_set(): ds = Dataset() print("Datasets loaded.") X_all = pad_vec_sequences(ds.X_all_vec_seq) Y_all = ds.Y_all #print (X_all.shape) x_train, x_test, y_train, y_test = model_selection.train_test_split(X_all,Y_all,test_size=0.2) y_train = pad_class_sequence(y_train, nb_classes) y_test = pad_class_sequence(y_test, nb_classes) y_test = np.array(y_test) x_train = np.asarray(x_train) x_train.ravel() y_train = np.asarray(y_train) y_train.ravel() return x_train,y_train,x_test,y_test
test_ent_seq = [] #list of lists of entities in each test query test_seq = [] #list of all test queries for i in range(n_test): print("\nEnter Query", i + 1, " to be classified:") test_text = input() test_seq.append(test_text) #vectorize text. test_doc = nlp(test_text) test_vec = [] for word in test_doc: test_vec.append(word.vector) test_vec_seq.append(test_vec) test_ent_seq.append(test_doc.ents) #convert all the sentences into matrices of equal size. test_vec_seq = pad_vec_sequences(test_vec_seq) #get predictions prediction = model.predict(test_vec_seq) label_predictions = np.zeros(prediction.shape) for i in range(n_test): m = max(prediction[i]) p = np.where(prediction[i] > 0.55 * m) # p collects possible sub intents q = np.where(prediction[i] == m) #q collects intent label_predictions[i][p] = 1 label_predictions[i][q] = 2 ''' label_predictions[i] is now a list of intent predictions for Query i, where: One of the following integer values is saved per intent label:
from keras.preprocessing import sequence from keras.models import Model from keras.layers import Dense, Dropout, Embedding, Input, merge from keras.layers.recurrent import LSTM, GRU from keras.layers import Concatenate from keras.utils import np_utils, generic_utils from keras import optimizers, metrics maxlen = 50 #sentences with length > maxlen will be ignored hidden_dim = 32 nb_classes = len(labels) ds = Dataset() print("Datasets loaded.") X_all = pad_vec_sequences(ds.X_all_vec_seq) Y_all = ds.Y_all #print (X_all.shape) x_train, x_test, y_train, y_test = model_selection.train_test_split( X_all, Y_all, test_size=0.2) y_train = pad_class_sequence(y_train, nb_classes) y_test = pad_class_sequence(y_test, nb_classes) #THE MODEL sequence = Input(shape=(maxlen, 384), dtype='float32', name='input') #forwards lstm forwards = LSTM(hidden_dim, activation='tanh', recurrent_activation='hard_sigmoid', use_bias=True, dropout=0.1,