def predict(sess, saver, graph, message): main_path = os.path.join(os.curdir, "test_NLU") input_vocab_path = os.path.join(main_path, 'vocab1', 'in_vocab') intent_vocab_path = os.path.join(main_path, 'vocab1', 'intent_vocab') slot_vocab_path = os.path.join(main_path, 'vocab1', 'slot_vocab') in_vocab = loadVocabulary(input_vocab_path) intent_vocab = loadVocabulary(intent_vocab_path) slot_vocab = loadVocabulary(slot_vocab_path) idx2word = in_vocab['rev'] msg = graph.get_tensor_by_name("inputs:0") seq_len = graph.get_tensor_by_name("sequence_length:0") intent = graph.get_tensor_by_name("intent_output:0") slot = graph.get_tensor_by_name("slot_output:0") #crf_score = graph.get_tensor_by_name("score_output:0") #preprocess new data for prediction #englishfy new_msg = "" for char in message: new_msg = new_msg + char + " " message = new_msg.rstrip() #input idx inp = sentenceToIds(message, in_vocab) a = np.array(inp) b = [] b.append(a) c = np.array(b) #print(c) #seq_len j = [] k = len(inp) j.append(k) l = np.array(j) #print(l) #predict intent pred_intent_array = sess.run(intent, feed_dict={msg: c, seq_len: l}) #print(pred_intent_array) pred_intent_idx = np.argmax(pred_intent_array) #print(pred_intent_idx) #implement top 3 option for debugging #show the accuracy of intent prediction print(pred_intent_array[0][pred_intent_idx] * 100) idx2intent = intent_vocab["rev"] pred_intent_word = idx2intent[pred_intent_idx] print(pred_intent_word) #predict slots pred_slot_array_array = sess.run(slot, feed_dict={msg: c, seq_len: l}) #print(pred_slot_array_array) #show the accuracy of slot prediction pred_slot_word = [] for idx in range(k): pred_slot_word.append(slot_vocab['rev'][pred_slot_array_array[0][idx]]) print(pred_slot_word)
full_valid_path = os.path.join("./data", arg.dataset, arg.valid_data_path) createVocabulary( os.path.join(full_train_path, arg.input_file), os.path.join(arg.vocab_path, "in_vocab"), ) createVocabulary( os.path.join(full_train_path, arg.slot_file), os.path.join(arg.vocab_path, "slot_vocab"), ) createVocabulary( os.path.join(full_train_path, arg.intent_file), os.path.join(arg.vocab_path, "intent_vocab"), ) in_vocab = loadVocabulary(os.path.join(arg.vocab_path, "in_vocab")) slot_vocab = loadVocabulary(os.path.join(arg.vocab_path, "slot_vocab")) intent_vocab = loadVocabulary(os.path.join(arg.vocab_path, "intent_vocab")) def createModel( input_data, input_size, sequence_length, slot_size, intent_size, layer_size=128, isTraining=True, ): cell_fw = tf.contrib.rnn.BasicLSTMCell(layer_size) cell_bw = tf.contrib.rnn.BasicLSTMCell(layer_size)
print('use atis dataset') else: print('use own dataset: ', arg.dataset) full_train_path = os.path.join('./data', arg.dataset, arg.train_data_path) full_test_path = os.path.join('./data', arg.dataset, arg.test_data_path) full_valid_path = os.path.join('./data', arg.dataset, arg.valid_data_path) createVocabulary(os.path.join(full_train_path, arg.input_file), os.path.join(arg.vocab_path, 'in_vocab')) createVocabulary(os.path.join(full_train_path, arg.slot_file), os.path.join(arg.vocab_path, 'slot_vocab')) createVocabulary(os.path.join(full_train_path, arg.intent_file), os.path.join(arg.vocab_path, 'intent_vocab'), no_pad=True) in_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'in_vocab')) slot_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'slot_vocab')) intent_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'intent_vocab')) def createModel(input_data, input_size, sequence_length, slots, slot_size, intent_size, layer_size=128, isTraining=True): cell_fw = tf.contrib.rnn.BasicLSTMCell(layer_size) cell_bw = tf.contrib.rnn.BasicLSTMCell(layer_size)
remove_da_attn = True else: print('unknown model type!') exit(1) #full path to data will be: ./data + train/test/valid full_train_path = os.path.join(arg.data_path,arg.train_data_path) full_test_path = os.path.join(arg.data_path,arg.test_data_path) full_valid_path = os.path.join(arg.data_path,arg.valid_data_path) layer_size = arg.layer_size batch_size = arg.batch_size print('*'*20+model_type+' '+str(layer_size)+'*'*20) vocab_path = arg.vocab_path in_vocab = loadVocabulary(os.path.join(vocab_path, 'in_vocab')) da_vocab = loadVocabulary(os.path.join(vocab_path, 'da_vocab')) def createModel(input_data, input_size, sequence_length, da_size, decoder_sequence_length, layer_size = 256, isTraining = True): cell_fw = tf.contrib.rnn.BasicLSTMCell(layer_size) cell_bw = tf.contrib.rnn.BasicLSTMCell(layer_size) if isTraining == True: cell_fw = tf.contrib.rnn.DropoutWrapper(cell_fw, input_keep_prob=0.5, output_keep_prob=0.5) cell_bw = tf.contrib.rnn.DropoutWrapper(cell_bw, input_keep_prob=0.5, output_keep_prob=0.5) embedding = tf.get_variable('embedding', [input_size, layer_size]) inputs = tf.nn.embedding_lookup(embedding, input_data) inputs = tf.reduce_sum(inputs,2)
exit(1) elif arg.dataset == 'snips': print('use snips dataset') elif arg.dataset == 'atis': print('use atis dataset') else: print('use own dataset: ',arg.dataset) full_train_path = os.path.join('./data',arg.dataset,arg.train_data_path) full_test_path = os.path.join('./data',arg.dataset,arg.test_data_path) full_valid_path = os.path.join('./data',arg.dataset,arg.valid_data_path) createVocabulary(os.path.join(full_train_path, arg.input_file), os.path.join(arg.vocab_path, 'in_vocab')) createVocabulary(os.path.join(full_train_path, arg.slot_file), os.path.join(arg.vocab_path, 'slot_vocab')) createVocabulary(os.path.join(full_train_path, arg.intent_file), os.path.join(arg.vocab_path, 'intent_vocab')) in_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'in_vocab')) slot_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'slot_vocab')) intent_vocab = loadVocabulary(os.path.join(arg.vocab_path, 'intent_vocab')) def createModel(input_data, input_size, sequence_length, slot_size, intent_size, layer_size = 128, isTraining = True): cell_fw = tf.contrib.rnn.BasicLSTMCell(layer_size) cell_bw = tf.contrib.rnn.BasicLSTMCell(layer_size) if isTraining == True: cell_fw = tf.contrib.rnn.DropoutWrapper(cell_fw, input_keep_prob=0.5, output_keep_prob=0.5) cell_bw = tf.contrib.rnn.DropoutWrapper(cell_bw, input_keep_prob=0.5, output_keep_prob=0.5) embedding = tf.get_variable('embedding', [input_size, layer_size]) inputs = tf.nn.embedding_lookup(embedding, input_data)
full_train_path = os.path.join('../input_data', arg.dataset, arg.train_data_path, arg.input_file) full_valid_path = os.path.join('../input_data', arg.dataset, arg.valid_data_path, arg.input_file) full_test_path = os.path.join('../input_data', arg.dataset, arg.test_data_path, arg.input_file) full_inference_path = os.path.join('../input_data', arg.dataset, arg.test_data_path, arg.inference_file) full_inference_label_path = os.path.join('../input_data', arg.dataset, arg.test_data_path, arg.inference_label_file) createVocabulary("../input_data/" + arg.dataset + "/" + arg.embed_path, "../input_data/" + arg.dataset + "/in_vocab", pad=True, unk=True) in_vocab = loadVocabulary("../input_data/" + arg.dataset + "/in_vocab") logging.info("vocab created") # Create Training Model with tf.variable_scope('triplet_model'): global_step = tf.Variable(0, trainable=False, name='global_step') model = TripletModel(arg) outputs = model.build_model() with tf.variable_scope('loss'): cos_an, cos_ap = outputs loss = tf.maximum(0.0, cos_an - cos_ap + arg.margin) params = tf.trainable_variables() gradients = tf.gradients(loss, params) clipped_gradients, norm = tf.clip_by_global_norm(gradients, 5.0)