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
Ejemplo n.º 2
0
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('') 
Ejemplo n.º 3
0
    # 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, 
        posts_var, posts_len, response_var, response_len, _, _= utils.get_variables(batch,
                                          vocab, args.dec_max_len, use_cuda=args.use_cuda)
        q_pos, r_pos = norm_data(posts_var, posts_len, response_var, response_len)
        # 
        rq_recon_error = rq_rbm.train_op(q_pos, r_pos)
        qr_recon_error = qr_rbm.train_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])

        step += 1
        if step == 1:
            logger.info("QR FE %.4f, RQ FE %.4f " % (qr_fe[0],
                         rq_fe[0]) + 
                       "recon error %.4f, %.4f, %.4f, %.4f %4.f, %4.f" % rq_recon_error )