def train(): model = ChatBotModel(False,config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() init = tf.global_variables_initializer() with tf.Session() as sess: print('Running Session') sess.run(init) _check_restore_parameters(sess,saver) iteration = model.global_step.eval() total_loss = 0 while True: skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) encoder_inputs,decoder_inputs,decoder_masks = data.get_batch(data_buckets[bucket_is],bucket_id,batch_size=config.BATCH_SIZE) start = time.time() _,step_loss,_ = run_step(sess,model,encoder_inputs,decoder_inputs,decoder_masks,bucket_id,False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format(iteration,total_loss/skip_step,time.time() - start)) start = time.time() total_loss = 0 saver.save(sess,os.path.join(config.CPT_PATH,'chatbot'),global_step=model.global_step) if iteration % (10 * skip_step) == 0: _eval_test_set(sess,model,test_buckets) start = time.time() sys.stdout.flush()
def chat(question): """ In test mode, we don"t to create the backward path. """ _, enc_vocab = data_utils.load_vocab( os.path.join(config.DATA_PATH, "vocab.enc")) # `inv_dec_vocab` <type "list">: id2word. inv_dec_vocab, _ = data_utils.load_vocab( os.path.join(config.DATA_PATH, "vocab.dec")) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) check_restore_parameters(sess, saver) output_file = open(os.path.join(config.DATA_PATH, config.TERMINAL_OUTPUT), "a+", encoding="utf-8") # Decode from standard input. max_length = config.BUCKETS[-1][0] print( "Welcome to TensorBro. Say something. Enter to exit. Max length is", max_length) line = question if hasattr(line, "decode"): # If using Python 2 # FIXME: UnicodeError when deleting Chinese in terminal. line = line.decode("utf-8") if len(line) > 0 and line[-1] == "\n": line = line[:-1] if not line: pass output_file.write("HUMAN ++++ " + line + "\n") # Get token-ids for the input sentence. token_ids = data_utils.sentence2id(enc_vocab, line) if len(token_ids) > max_length: print("Max length I can handle is:", max_length) # line = _get_user_input() pass # Which bucket does it belong to? bucket_id = find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data_utils.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = construct_response(output_logits, inv_dec_vocab) print(response) output_file.write("BOT ++++ " + response + "\n") output_file.write("=============================================\n") output_file.close()
def train(): """ Train the bot. """ # test_buckets, data_buckets: <type "list">: # [[[[Context], [Response]], ], ]] # test_buckets[0]: first bucket # test_buckets[0][0]: first pair of the first bucket # test_buckets[0][0][0], test_buckets[0][0][1]: Context and response # test_buckets[0][0][0][0]: word index of the first words # train_buckets_scale: list of increasing numbers from 0 to 1 that # we"ll use to select a bucket. len(train_buckets_scale) = len(BUCKETS) test_buckets, data_buckets, train_buckets_scale = _get_buckets() # in train mode, we need to create the backward path, so forward_only is False model = ChatBotModel(False, config.BATCH_SIZE) # build graph model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: print("Running session...") sess.run(tf.global_variables_initializer()) check_restore_parameters(sess, saver) iteration = model.global_step.eval() total_loss = 0 logging.info("Training...") try: while True: skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) encoder_inputs, decoder_inputs, decoder_masks = data_utils.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: logging.info( "Training @ iter {:d}: loss {:.4f}, time {:.4f}". format(iteration, total_loss / skip_step, time.time() - start)) total_loss = 0 saver.save(sess, os.path.join(config.CPT_PATH, "chatbot"), global_step=model.global_step) if iteration % (10 * skip_step) == 0: logging.info("Testing...") # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) sys.stdout.flush() except KeyboardInterrupt: logging.info("Training interrupted.")
def chat(): """ in test mode, we don't to create the backward path """ _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = open( os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+') # Decode from standard input. max_length = config.BUCKETS[-1][0] print( 'Welcome to TensorBro. Say something. Enter to exit. Max length is', max_length) # store a line history for 3 lines conversation_history = [] line_history = ['', '', ''] while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] # update the line_history line_history.append(line) line_history.pop(0) # create line from the line history line = ''.join(line_history) if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) if (len(token_ids) > max_length): print('Max length I can handle is:', max_length) line = _get_user_input() continue # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = _construct_response(output_logits, inv_dec_vocab) print(response) conversation_history.append((line, response)) output_file.write('BOT ++++ ' + response + '\n') output_file.write('=============================================\n') output_file.close()
def train(): """ Train the bot """ test_buckets, data_buckets, train_buckets_scale = get_buckets() model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: print('Running session') sess.run(tf.global_variables_initializer()) check_restore_parameters(sess, saver) iteration = model.global_step.eval() total_loss = 0 file_writer = tf.summary.FileWriter( os.path.join(config.LOG_PATH, 'tensorboard'), sess.graph) training_loss_summary = tf.Summary() testing_loss_summary = tf.Summary() while True: skip_step = get_skip_step(iteration) bucket_id = get_random_bucket(train_buckets_scale) encoder_inputs, decoder_inputs, decoder_masks = data_utils.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format( iteration, total_loss / skip_step, time.time() - start)) bucket_value = training_loss_summary.value.add() bucket_value.tag = "training_loss_bucket_%d" % bucket_id bucket_value.simple_value = step_loss file_writer.add_summary(training_loss_summary, model.global_step.eval()) start = time.time() total_loss = 0 saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss eval_test_set(sess, model, test_buckets, testing_loss_summary, file_writer) start = time.time() sys.stdout.flush()
def train(): """ Train the bot """ test_buckets, data_buckets, train_buckets_scale = _get_buckets() # 버킷별로 샘플을 채워서 읽어온다!! # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: print('Running session') sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) # 세션을 리스토어 할 수 있으면 해오고 iteration = model.global_step.eval() # global step을 불러온다. total_loss = 0 while True: skip_step = _get_skip_step( iteration) # skip_step을 얻어온다. 100보다 적으면 30, 아니면 100 bucket_id = _get_random_bucket( train_buckets_scale) # 버킷 아이디를 랜덤으로 고른다 # 선택된 버킷으로부터 batch_size만큼의 배치를 받아온다 encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() # step run!! forward_only = False _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 # skip_step마다 누적해두었던 loss와 걸린 시간을 보고한다 # 그리고 다시 초기화 # 세션 저장 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format( iteration, total_loss / skip_step, time.time() - start)) start = time.time() total_loss = 0 saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) # skip_step의 10번을 돌았으면 test 버킷에서 버킷 id 별로 테스트를 한번씩 돈다 if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) start = time.time() sys.stdout.flush()
def chat(): """ in test mode, we don't to create the backward path """ # index2word , word2index _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) # 배치 사이즈는 하나 (forward only) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = open( os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+') # Decode from standard input. max_length = config.BUCKETS[-1][0] # 유저가 타이핑할 수 있는 최대 길이는 버킷의 최대길이 print( 'Welcome to TensorBro. Say something. Enter to exit. Max length is', max_length) while True: line = _get_user_input() # 시스템 인풋을 받아온다 if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': # 아무것도 타이핑 안하면 브레이크 break output_file.write('HUMAN ++++ ' + line + '\n') # 아웃풋 파일에 한줄씩 기록 # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) # 문장 하나를 index로 if (len(token_ids) > max_length): # 만약 최대 길이보다 더 받았으면 다시 타이핑 받게 한다 print('Max length I can handle is:', max_length) line = _get_user_input() continue # Which bucket does it belong to? bucket_id = _find_right_bucket( len(token_ids)) # 입력 시퀀스의 길이에 맞는 버킷(최소) id 골라온다 # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( [(token_ids, [])], # 디코더 인풋은 x 전부 패딩되서 들어가는듯 bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) # forward_only == True response = _construct_response( output_logits, inv_dec_vocab) # id2word로 복구해서 다시 리스폰스로 print(response) output_file.write('BOT ++++ ' + response + '\n') output_file.write('=============================================\n') output_file.close()
def chat(self, model_iteration=None): """ in test mode, we don't to create the backward path """ _, enc_vocab = self.reader.load_vocab( os.path.join(self.cfg['PROCESSED_PATH'], 'vocab.enc')) inv_dec_vocab, _ = self.reader.load_vocab( os.path.join(self.cfg['PROCESSED_PATH'], 'vocab.dec')) model = ChatBotModel(config=self.cfg, forward_only=True, batch_size=1) model.build_graph() saver = tf.train.Saver() self.sess.run(tf.global_variables_initializer()) self._check_restore_parameters(saver, iteration=model_iteration) output_file = open( os.path.join(self.cfg['PROCESSED_PATH'], self.cfg['OUTPUT_FILE']), 'a+') # Decode from standard input. max_length = self.cfg['BUCKETS'][-1][0] print( 'Welcome to TensorBro. Say something. Enter to exit. Max length is', max_length) while True: line = self._get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = self.reader.sentence2id(enc_vocab, str(line)) if len(token_ids) > max_length: print('Max length I can handle is:', max_length) line = self._get_user_input() continue # Which bucket does it belong to? bucket_id = self._find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = self.reader.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = self.run_step(model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = self._construct_response(output_logits, inv_dec_vocab) print(response) output_file.write('BOT ++++ ' + response + '\n') output_file.write('=============================================\n') output_file.close()
def chat(): """ in test mode, we don't create the backward path :return: """ _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab_enc')) inv_dec_vocab, _ = data.load_vocab((os.path.join(config.PROCESSED_PATH, 'vocab_dec'))) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = open( os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+') # Decode from standard input max_length = config.BUCKETS[-1][0] print( 'I am ChatBot. Proceed to chat. Enter of exit. Max length is {0}'. format(max_length)) while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) if len(token_ids) > max_length: print('Max length I can handle is: {0}'.format(max_length)) line = _get_user_input() continue # Which bucket does this go in?? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = _construct_response((output_logits, inv_dec_vocab)) print(response) output_file.write('BOT ++++ ' + response + '\n') output_file.write('===============================================\n') output_file.close()
def train(): """ Train the bot """ test_buckets, data_buckets, train_buckets_scale = _get_buckets() # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: print('Running session') sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) # iteration (global_step.eval()) gives us the total number of time gradient update has been done iteration = model.global_step.eval() total_loss = 0 while True: skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format( iteration, total_loss / skip_step, time.time() - start)) start = time.time() total_loss = 0 print('Saving weights ...') saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss print('Running eval after {} steps/iterations'.format( iteration)) _eval_test_set(sess, model, test_buckets) start = time.time() sys.stdout.flush() # My DEBUG if iteration == 15000: stop = "here"
def initchat(): _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, '../vocab_nostart.txt')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, '../vocab_nostart.txt')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) return sess, enc_vocab, inv_dec_vocab, model
def train(): """ Train the bot """ test_buckets, data_buckets, train_buckets_scale = _get_buckets() # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, config.BATCH_SIZE) w = model.build_graph() #print(name) #ip=raw_input() saver = tf.train.Saver() with tf.Session() as sess: print('Running session') sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) iteration = model.global_step.eval() total_loss = 0 while True: #skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 #if iteration % skip_step == 0: if 1: print('Iter {}: loss {}, time {}'.format( iteration, total_loss, time.time() - start)) start = time.time() total_loss = 0 saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) #if iteration % (10 * skip_step) == 0: if 1: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) start = time.time() sys.stdout.flush() vectorw1 = sess.run(w) print(vectorw1)
def __chat_init(self): _, self.__enc_vocab = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.enc')) self.__inv_dec_vocab, _ = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.dec')) self.__model = ChatBotModel(True, batch_size=1) self.__model.build_graph() saver = tf.train.Saver() self.__session = tf.Session() self.__session.run(tf.global_variables_initializer()) _check_restore_parameters(self.__session, saver) # Decode from standard input. self.max_length = config.BUCKETS[-1][0]
def train(): """ Train the bot. :return: """ test_buckets, data_buckets, train_buckets_scale = _get_buckets() # In train mode, we need to construct the backwards path, so forward_only must be False. model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: print('Running session.') sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) iteration = model.global_step.eval() total_loss = 0 while True: skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iteration {0}: loss {1}, time {2}'.format( iteration, total_loss / skip_step, time.time() - start)) start = time.time() total_loss = 0 saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) start = time.time() sys.stdout.flush()
def chat(input_cmd): """ in test mode, we don't to create the backward path """ _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = open( os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+') # Decode from standard input. max_length = config.BUCKETS[-1][0] # input_cmd = _get_user_input() if len(input_cmd) > 0 and input_cmd[-1] == '\n': input_cmd = input_cmd[:-1] output_file.write('Input: ' + input_cmd + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(input_cmd)) if len(token_ids) > max_length: input_cmd = input_cmd[max_length] # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = _construct_response(output_logits, inv_dec_vocab) output_file.write('Response: ' + response + '\n') output_file.close() return response
def chat(): """ in test mode, we don't to create the backward path """ _, enc_vocab = data_utils.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data_utils.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) check_restore_parameters(sess, saver) output_file = open( '/Users/EleanorLeung/Documents/CITS4404/chatbot/output_convo.txt', 'a+') # Decode from standard input. max_length = config.BUCKETS[-1][0] print('Talk to me! Enter to exit. Max length is', max_length) while True: line = str.encode(get_user_input()) if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN: ' + str(line) + '\n') token_ids = data_utils.sentence2id(enc_vocab, line) if len(token_ids) > max_length: print('Max length I can handle is:', max_length) line = get_user_input() continue bucket_id = find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data_utils.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = construct_response(output_logits, inv_dec_vocab) print(response) output_file.write('BOT: ' + response + '\n') output_file.write('=============================================\n') output_file.close()
def chat(): """ in test mode, we don't to create the backward path """ _, enc_vocab = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = open(os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+') # Decode from standard input. max_length = config.BUCKETS[-1][0] print('Welcome to TensorBro. Say something. Enter to exit. Max length is', max_length) while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) if (len(token_ids) > max_length): print('Max length I can handle is:', max_length) line = _get_user_input() continue # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch([(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = _construct_response(output_logits, inv_dec_vocab) print(response) output_file.write('BOT ++++ ' + response + '\n') output_file.write('=============================================\n') output_file.close()
def __init__(self): _, self.enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) self.inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() self.model = model saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) self.sess = sess self.max_length = config.BUCKETS[-1][0]
def train(): """ Train the bot """ test_buckets, data_buckets = _get_buckets() # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, 256) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: print('Running session') sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) iteration = model.global_step.eval() total_loss = 0 while True: skip_step = _get_skip_step(iteration) encoder_inputs, decoder_inputs, decoder_masks = utils.get_batch( data_buckets, FLAGS.max_encode_length, FLAGS.max_decode_length, batch_size=256) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format( iteration, total_loss / skip_step, time.time() - start)) start = time.time() total_loss = 0 saver.save(sess, os.path.join("runs", 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) start = time.time() sys.stdout.flush()
def chat(): _,enc_load = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab_enc')) inv_dec_vocab,_ = data.load_vocab(os.path.join(config.PROCESSED_PATH,'vocab.dec')) model = ChatBotModel(True,batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess,saver) output_file = open(os.path.join(config.PROCESSED_PATH,config.OUTPUT_FILE),'a+') max_length = config.BUCKETS[-1][0] print('Welcome to TensorBro, Say something. Enter to exit. Max length is ', max_length) while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') token_ids = data.sentence2id(enc_vocab,str(line)) if (len(token_ids) > max_length): print('Max length I can handle is:' max_length) line = _get_user_input() continue bucket_id = _find_right_bucket(len(token_ids)) encoder_inputs,decoder_inputs,decoder_masks = data.get_batch([(token_ids , [])],bucket_id,batch_size=1) _,_,output_logits = run_step(sess,model,encoder_inputs,decoder_inputs,decoder_masks,bucket_id,True) response = _construct_response(output_logits,inv_dec_vocab) print(response) output_file.write('BOT ++++ ' + response + '\n') output_file.write('===================================\n') output_file.close()
def translate(): _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) # Decode from standard input. max_length = config.BUCKETS[-1][0] print('Please enter sentence in English') while True: line = _get_user_input() if len(line) > 0 and line[-1] == u'\n': line = line[:-1] if line == '': break # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, line) if (len(token_ids) > max_length): token_ids = token_ids[:max_length] # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = _construct_response(output_logits, inv_dec_vocab) print(response)
def accept_incoming_connections(): """Setup model""" _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) """Sets up handling for incoming clients.""" print("Waiting for connection...") while True: client, client_address = SERVER.accept() output_file = codecs.open(os.path.join( config.PROCESSED_PATH, config.OUTPUT_FILE + str(client_address) + '.txt'), encoding='utf-8', mode='a+') print("%s:%s has connected." % client_address) client.send( bytes( "Greetings from the BOT! Now type your name and press enter!", "utf8")) addresses[client] = client_address Thread(target=handle_client, args=( client, enc_vocab, inv_dec_vocab, model, saver, sess, output_file, )).start()
def create_chitchat_bot(self): """Initializes self.chitchat_bot with some conversational model.""" # Hint: you might want to create and train chatterbot.ChatBot here. # It could be done by creating ChatBot with the *trainer* parameter equals # "chatterbot.trainers.ChatterBotCorpusTrainer" # and then calling *train* function with "chatterbot.corpus.english" param ######################## #### YOUR CODE HERE #### ######################## _, self.enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) self.inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) self.model = ChatBotModel(True, batch_size=1) self.model.build_graph() self.sess = tf.Session() saver = tf.train.Saver() self.sess.run(tf.global_variables_initializer()) _check_restore_parameters(self.sess, saver)
def seq_pred(question): _, enc_vocab = data_utils.load_vocab(os.path.join(config.DATA_PATH, "vocab.enc")) inv_dec_vocab, _ = data_utils.load_vocab(os.path.join(config.DATA_PATH, "vocab.dec")) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) check_restore_parameters(sess, saver) max_length = config.BUCKETS[-1][0] line = question if hasattr(line, "decode"): # If using Python 2 # FIXME: UnicodeError when deleting Chinese in terminal. line = line.decode("utf-8") if len(line) > 0 and line[-1] == "\n": line = line[:-1] if not line: pass token_ids = data_utils.sentence2id(enc_vocab, line) if len(token_ids) > max_length: line = question pass bucket_id = find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data_utils.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = construct_response(output_logits, inv_dec_vocab) answer = response return answer
def train(): """ Train the bot """ test_buckets, data_buckets, train_buckets_scale = _get_buckets() # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: print('Running session') sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) iteration = model.global_step.eval() total_loss = 0 while True: skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) encoder_inputs, decoder_inputs, decoder_masks = data.get_batch(data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format(iteration, total_loss/skip_step, time.time() - start)) start = time.time() total_loss = 0 saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) start = time.time() sys.stdout.flush()
def chat(): """ in test mode, we don't to create the backward path """ _, enc_vocab = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = open(os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+') # Decode from standard input. max_length = config.BUCKETS[-1][0] print('Welcome to TensorBro. Say something. Enter to exit. Max length is', max_length) if config.BEAM_SEARCH: while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) if (len(token_ids) > max_length): print('Max length I can handle is:', max_length) line = _get_user_input() continue # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch([(token_ids, [])], bucket_id, batch_size=1) # add beam search parameter to run_Step path, symbol, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) k = output_logits[0] paths = [] for kk in range(config.BEAM_SIZE): paths.append([]) curr = range(config.BEAM_SIZE) num_steps = len(path) for i in range(num_steps-1, -1, -1): for kk in range(config.BEAM_SIZE): paths[kk].append(symbol[i][curr[kk]]) curr[kk] = path[i][curr[kk]] responses = set() for kk in range(config.BEAM_SIZE): response = _construct_beam_response(paths[kk], inv_dec_vocab) if response not in responses: responses.add(response) print('response: ', response) output_file.write('BOT ++++ ' + response + '\n') else: while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) if (len(token_ids) > max_length): print('Max length I can handle is:', max_length) line = _get_user_input() continue # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch([(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) if config.ANTI_LM: dummy_encoder_inputs = [np.array([config.PAD_ID]) for _ in range(len(encoder_inputs))] _, _, output_logits_t = run_step(sess, model, dummy_encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) # only apply antilm up to a certain point in the decoder input gamma = int(config.GAMMA*len(decoder_inputs)) antilm_mask = np.array([1*( _ < gamma) for _ in range(len(decoder_inputs))]).reshape((-1,1,1)) output_logits -= config.LAMBDA*(output_logits_t*antilm_mask) response = _construct_response(output_logits, inv_dec_vocab) print('response: ', response) output_file.write('BOT ++++ ' + response + '\n') output_file.write('=============================================\n') output_file.close()
def train(): """ Train the bot """ print('train begin.' + time.ctime() + "," + config.getThreadId()) sessionconfig = tf.ConfigProto() sessionconfig.gpu_options.allow_growth = True ps_hosts = config.PS_HOSTS.split(",") worker_hosts = config.WORKER_HOSTS.split(",") cluster = tf.train.ClusterSpec({"ps": ps_hosts, "worker": worker_hosts}) server = tf.train.Server(cluster, job_name=config.JOB_NAME, task_index=config.TASK_INDEX, config=sessionconfig) if config.JOB_NAME == "ps": print("server join") server.join() elif config.JOB_NAME == "worker": #with tf.device('/cpu:0'): with tf.device( tf.train.replica_device_setter( worker_device="/job:worker/task:%d" % config.TASK_INDEX, cluster=cluster)): test_buckets, data_buckets, train_buckets_scale = _get_buckets() # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() summary_op = tf.summary.merge_all() init_op = tf.global_variables_initializer() sv = tf.train.Supervisor( is_chief=(config.TASK_INDEX == 0), logdir=config.CPT_PATH, init_op=init_op, summary_op=summary_op, #summary_op=None, saver=saver, global_step=model.global_step, save_model_secs=60) with sv.prepare_or_wait_for_session(master=server.target, config=sessionconfig) as sess: print('Running session' + time.ctime() + "," + config.getThreadId()) # print("_check_restore_parameters begin " + time.ctime()) # _check_restore_parameters(sess, saver) # print("_check_restore_parameters end " + time.ctime()) iteration = model.global_step.eval() print("iteration:" + str(iteration)) total_loss = 0 start = time.time() bucketSizeList = [] while not sv.should_stop(): skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) bucketSizeList.append(bucket_id) encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format( iteration, total_loss / skip_step, (int)(time.time() - start)) + "s, " + time.ctime() + ", globalStep:" + str(model.global_step.eval())) if (iteration > 60): _calculateSpeed(start, bucketSizeList) #saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) sys.stdout.flush() bucketSizeList = [] total_loss = 0 start = time.time() print("exeception happen. exit") sv.stop()
class ChatBot: def __init__(self, mode = 'chat'): if not os.path.isdir(config.PROCESSED_PATH): data.prepare_raw_data() data.process_data() # create checkpoints folder if there isn't one already data.make_dir(config.CPT_PATH) if(mode == "chat"): self.__chat_init() def __chat_init(self): _, self.__enc_vocab = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.enc')) self.__inv_dec_vocab, _ = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.dec')) self.__model = ChatBotModel(True, batch_size=1) self.__model.build_graph() saver = tf.train.Saver() self.__session = tf.Session() self.__session.run(tf.global_variables_initializer()) _check_restore_parameters(self.__session, saver) # Decode from standard input. self.max_length = config.BUCKETS[-1][0] def chat(self, line): # Get token-ids for the input sentence. token_ids = data.sentence2id(self.__enc_vocab, str(line)) if (len(token_ids) > self.max_length): return "Would you mind to be more concise? I can't understand" # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch([(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(self.__session, self.__model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) return _construct_response(output_logits, self.__inv_dec_vocab) def train(self): """ Train the bot """ test_buckets, data_buckets, train_buckets_scale = _get_buckets() # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) iteration = model.global_step.eval() total_loss = 0 while True: skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) encoder_inputs, decoder_inputs, decoder_masks = data.get_batch(data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) start = time.time() _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format(iteration, total_loss/skip_step, time.time() - start)) start = time.time() total_loss = 0 saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) start = time.time() sys.stdout.flush()
def chat(): """ in test mode, we don't to create the backward path """ _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = open( os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+') # Decode from standard input. max_length = config.BUCKETS[-1][0] print( 'Welcome to TensorBro. Say something. Enter to exit. Max length is', max_length) if config.BEAM_SEARCH: while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) if (len(token_ids) > max_length): print('Max length I can handle is:', max_length) line = _get_user_input() continue # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) if config.ANTI_LM: # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, target_weights = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # do beam search and antilm together # Get output logits for the sentence. beams, new_beams, results = [(1, 0, { 'eos': 0, 'dec_inp': decoder_inputs, 'prob': 1, 'prob_ts': 1, 'prob_t': 1 })], [], [ ] # initialize beams as (log_prob, empty_string, eos) dummy_encoder_inputs = [ np.array([config.PAD_ID]) for _ in range(len(encoder_inputs)) ] for dptr in range(len(decoder_inputs) - 1): if dptr > 0: target_weights[dptr] = [1.] beams, new_beams = new_beams[:args.beam_size], [] if config.DEBUG: print("=====[beams]=====", beams) heapq.heapify(beams) # since we will remove something for prob, _, cand in beams: if cand['eos']: results += [(prob, 0, cand)] continue # normal seq2seq if config.DEBUG: print( cand['prob'], " ".join([ dict_lookup(inv_dec_vocab, w[0]) for w in cand['dec_inp'] ])) # all_prob_ts = model_step(encoder_inputs, cand['dec_inp'], dptr, target_weights, bucket_id) _, _, all_prob_ts = run_step( sess, model, encoder_inputs, cand['dec_inp'], target_weights, bucket_id, True) if config.ANTI_LM: # anti-lm # all_prob_t = model_step(dummy_encoder_inputs, cand['dec_inp'], dptr, target_weights, bucket_id) _, _, all_prob_t = run_step( sess, model, dummy_encoder_inputs, cand['dec_inp'], target_weights, bucket_id, True) # adjusted probability all_prob = all_prob_ts - config.LAMBDA * np.array( all_prob_t ) #+ args.n_bonus * dptr + random() * 1e-50 else: all_prob_t = [0] * len(all_prob_ts) all_prob = all_prob_ts # suppress copy-cat (respond the same as input) if dptr < len(token_ids): all_prob[token_ids[dptr]] = all_prob[ token_ids[dptr]] * 0.01 # for debug use # if config.DEBUG: return all_prob, all_prob_ts, all_prob_t # beam search for c in np.argsort( all_prob)[::-1][:args.beam_size]: new_cand = { 'eos': (c == config.EOS_ID), 'dec_inp': [(np.array([c]) if i == (dptr + 1) else k) for i, k in enumerate(cand['dec_inp'])], 'prob_ts': cand['prob_ts'] * all_prob_ts[c], 'prob_t': cand['prob_t'] * all_prob_t[c], 'prob': cand['prob'] * all_prob[c], } new_cand = ( new_cand['prob'], random(), new_cand ) # stuff a random to prevent comparing new_cand try: if (len(new_beams) < config.BEAM_SIZE): heapq.heappush(new_beams, new_cand) elif (new_cand[0] > new_beams[0][0]): heapq.heapreplace(new_beams, new_cand) except Exception as e: print("[Error]", e) print("-----[new_beams]-----\n", new_beams) print("-----[new_cand]-----\n", new_cand) results += new_beams # flush last cands # post-process results res_cands = [] for prob, _, cand in sorted(results, reverse=True): cand['dec_inp'] = " ".join([ dict_lookup(inv_dec_vocab, w) for w in cand['dec_inp'] ]) print('response antilm: ', cand['dec_inp']) res_cands.append(cand) return res_cands[:args.beam_size] else: # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # add beam search parameter to run_Step path, symbol, output_logits = run_step( sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) k = output_logits[0] paths = [] for kk in range(config.BEAM_SIZE): paths.append([]) curr = range(config.BEAM_SIZE) num_steps = len(path) for i in range(num_steps - 1, -1, -1): for kk in range(config.BEAM_SIZE): paths[kk].append(symbol[i][curr[kk]]) curr[kk] = path[i][curr[kk]] responses = set() for kk in range(config.BEAM_SIZE): response = _construct_beam_response( paths[kk], inv_dec_vocab) if response not in responses: responses.add(response) print('response: ', response) output_file.write('BOT ++++ ' + response + '\n') else: while True: line = _get_user_input() if len(line) > 0 and line[-1] == '\n': line = line[:-1] if line == '': break output_file.write('HUMAN ++++ ' + line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, str(line)) if (len(token_ids) > max_length): print('Max length I can handle is:', max_length) line = _get_user_input() continue # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, target_weights = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1)
def train(): """ Train the bot """ print('train begin.' + time.ctime() + "," + config.getThreadId()) with tf.device('/gpu:0'): test_buckets, data_buckets, train_buckets_scale = _get_buckets() # in train mode, we need to create the backward path, so forwrad_only is False model = ChatBotModel(False, config.BATCH_SIZE) model.build_graph() saver = tf.train.Saver() sessionconfig = tf.ConfigProto() sessionconfig.gpu_options.allow_growth = True with tf.Session(config=sessionconfig) as sess: print('Running session' + time.ctime() + "," + config.getThreadId()) sess.run(tf.global_variables_initializer()) print("_check_restore_parameters begin " + time.ctime()) _check_restore_parameters(sess, saver) print("_check_restore_parameters end " + time.ctime()) iteration = model.global_step.eval() print("iteration:" + str(iteration)) total_loss = 0 start = time.time() bucketSizeList = [] while True: skip_step = _get_skip_step(iteration) bucket_id = _get_random_bucket(train_buckets_scale) bucketSizeList.append(bucket_id) encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( data_buckets[bucket_id], bucket_id, batch_size=config.BATCH_SIZE) _, step_loss, _ = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, False) total_loss += step_loss iteration += 1 if iteration % skip_step == 0: print('Iter {}: loss {}, time {}'.format( iteration, total_loss / skip_step, (int)(time.time() - start)) + "s, " + time.ctime() + ", globalStep:" + str(model.global_step.eval())) if (iteration > 60): _calculateSpeed(start, bucketSizeList) saver.save(sess, os.path.join(config.CPT_PATH, 'chatbot'), global_step=model.global_step) if iteration % (10 * skip_step) == 0: # Run evals on development set and print their loss _eval_test_set(sess, model, test_buckets) sys.stdout.flush() bucketSizeList = [] total_loss = 0 start = time.time()
content = content.encode('utf-8') # content = recMsg.Content replyMsg = reply.TextMsg(toUser, fromUser, content) return replyMsg.send() else: print "暂且不处理" return "success" except Exception, Argment: return Argment def GET(self): return get_predicted_sentence(u'你好', enc_vocab, inv_dec_vocab, model, sess) # return '好' model = ChatBotModel(True, batch_size=1) model.build_graph() _, enc_vocab = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab(os.path.join(config.PROCESSED_PATH, 'vocab.dec')) saver = tf.train.Saver() sess = tf.Session() sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) if __name__ == '__main__': # sess = tf.Session() # model = create_model(sess, forward_only=True) # model.batch_size = 1 # vocab_path = os.path.join(FLAGS.data_dir, "vocab%d.in" % FLAGS.vocab_size) # vocab, rev_vocab = data_utils.initialize_vocabulary(vocab_path)
def test(): """ in test mode, we don't to create the backward path """ _, enc_vocab = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.enc')) inv_dec_vocab, _ = data.load_vocab( os.path.join(config.PROCESSED_PATH, 'vocab.dec')) fh_test_truth = io.open(os.path.join(config.PROCESSED_PATH, 'test.dec'), 'r', encoding='utf-8') fh_test_enc = io.open(os.path.join(config.PROCESSED_PATH, 'test.enc'), 'r', encoding='utf-8') test_truths = fh_test_truth.readlines( ) # 1268 lines of correct translation in target language test_enc = fh_test_enc.readlines( ) # 1268 lines of initial sentence in source language fh_test_truth.close() fh_test_enc.close() bleu_scores = [] model = ChatBotModel(True, batch_size=1) model.build_graph() saver = tf.train.Saver() with tf.Session() as sess: sess.run(tf.global_variables_initializer()) _check_restore_parameters(sess, saver) output_file = io.open(os.path.join(config.PROCESSED_PATH, config.OUTPUT_FILE), 'a+', encoding='utf-8') # Decode from lines in test files. max_length = config.BUCKETS[-1][0] i = 0 # Index to be used to read ground_truth from test_truths for i in range(len(test_enc)): enc_line = test_enc[i] if len(enc_line) > 0 and enc_line[-1] == u'\n': enc_line = enc_line[:-1] if enc_line == '': break #output_file.write('HUMAN ++++ ' + enc_line + '\n') # Get token-ids for the input sentence. token_ids = data.sentence2id(enc_vocab, enc_line) if (len(token_ids) > max_length): print('Max length I can handle is:', max_length) output_file.write(u'.\n') bleu_score = 0 bleu_scores.append(bleu_score) i = i + 1 continue # Which bucket does it belong to? bucket_id = _find_right_bucket(len(token_ids)) # Get a 1-element batch to feed the sentence to the model. encoder_inputs, decoder_inputs, decoder_masks = data.get_batch( [(token_ids, [])], bucket_id, batch_size=1) # Get output logits for the sentence. _, _, output_logits = run_step(sess, model, encoder_inputs, decoder_inputs, decoder_masks, bucket_id, True) response = _construct_response( output_logits, inv_dec_vocab) # response is the translated sentence truth = test_truths[i] #print(type(truth)) #print(type(response)) truth_li = [truth.split()] response_li = response.split() bleu_score = sentence_bleu( truth_li, response_li, smoothing_function=SmoothingFunction().method1) print(response) output_file.write(response + '\n') i = i + 1 #print("BLEU: %.5f" % bleu_score) bleu_scores.append(bleu_score) output_file.write(u'=============================================\n') output_file.write(u"Average BLEU: %.5f" % np.mean(np.array(bleu_scores))) output_file.close() print("Average BLEU: %.5f" % np.mean(np.array(bleu_scores))) return bleu_scores