def reply_generator(): train_batcher = batcher(args.batch_size, args.train_query_file, args.train_response_file) while True: try: batch = train_batcher.next() except StopIteration: break (query_src, query_len, reply_src, reply_len, _, _) = utils.get_variables(batch, vocab, args.dec_max_len, use_cuda=args.use_cuda) q_avg, r_avg = get_avg_embedding(query_src, query_len, reply_src, reply_len) yield r_avg
def eval_model(epoch): rq_fe = [] qr_fe = [] total_case = 0 cur_time = time.time() rq_recon = [] qr_recon = [] valid_batcher = batcher(args.batch_size, args.valid_query_file, args.valid_response_file) while True: try: batch = valid_batcher.next() except: break posts_var, posts_len, response_var, response_len, _, _= utils.get_variables(batch, vocab, args.dec_max_len, use_cuda=args.use_cuda) # check batcher, and padding q_pos, r_pos = norm_data(posts_var, posts_len, response_var, response_len) rq_recon_error = rq_rbm.valid_op(q_pos, r_pos) qr_recon_error = qr_rbm.valid_op(r_pos, q_pos) rq_recon.append(rq_recon_error) qr_recon.append(qr_recon_error) rq_fe.append( rq_rbm.hr_free_energy(q_pos, r_pos)) qr_fe.append( qr_rbm.hr_free_energy(r_pos, q_pos)) total_case += len(batch[0]) rq_error = zip(*rq_recon) qr_error = zip(*qr_recon) batch_num = len(rq_recon) logger.info("Valid recon error: %.4f, %.4f, %.4f %.4f" % (sum(rq_error[1])/batch_num, sum(rq_error[2])/batch_num, sum(qr_error[1])/batch_num, sum(qr_error[2])/batch_num)) logger.info('Valid batch %d: average QR FE %.4f, RQ FE %.4f, QR, FEgap: %.4f, %4.f, RQ FEGap: %.4f, %.4f; (%.1f case/sec)' % ( epoch, sum(qr_fe) / len(qr_fe), sum(rq_fe) / len(rq_fe), (sum(qr_fe)-sum(qr_error[4])) /len(qr_fe), (sum(qr_fe)-sum(qr_error[5]))/len(qr_fe), (sum(rq_fe)-sum(rq_error[4])) /len(rq_fe), (sum(rq_fe)-sum(rq_error[5]))/len(rq_fe), total_case/(time.time()-cur_time))) logger.info('')
epoch, sum(qr_fe) / len(qr_fe), sum(rq_fe) / len(rq_fe), (sum(qr_fe)-sum(qr_error[4])) /len(qr_fe), (sum(qr_fe)-sum(qr_error[5]))/len(qr_fe), (sum(rq_fe)-sum(rq_error[4])) /len(rq_fe), (sum(rq_fe)-sum(rq_error[5]))/len(rq_fe), total_case/(time.time()-cur_time))) logger.info('') eval_model(-1) for e in range(args.num_epoch): logger.info('---------------------training--------------------------') logger.info("Epoch: %d/%d" % (e, args.num_epoch)) print("Epoch: %d/%d" % (e, args.num_epoch)) if e > 5: rq_rbm.monument = 0.8 qr_rbm.monument = 0.8 train_batcher = batcher(args.batch_size, args.train_query_file, args.train_response_file) # train state rq_fe = [] qr_fe = [] rq_recon = [] qr_recon = [] total_case = 0 cur_time = time.time() step = 0 while True: try: batch = train_batcher.next() except StopIteration: break # train_opt,