def bleu_validation(uidx, valid_iterator, model, bleu_scorer, vocab_tgt, batch_size, valid_dir='./valid', max_steps=10, beam_size=5, alpha=(- 1.0)):
    model.eval()
    numbers = []
    trans = []
    infer_progress_bar = tqdm(total=len(valid_iterator), desc=' - (Infer)  ', unit='sents')
    valid_iter = valid_iterator.build_generator(batch_size=batch_size)
    for batch in valid_iter:
        seq_nums = batch[0]
        numbers += seq_nums
        seqs_x = batch[1]
        infer_progress_bar.update(len(seqs_x))
        x = prepare_data(seqs_x, cuda=GlobalNames.USE_GPU)
        with torch.no_grad():
            word_ids = beam_search(nmt_model=model, beam_size=beam_size, max_steps=max_steps, src_seqs=x, alpha=alpha)
        word_ids = word_ids.cpu().numpy().tolist()
        for sent_t in word_ids:
            sent_t = [[wid for wid in line if (wid != PAD)] for line in sent_t]
            x_tokens = []
            for wid in sent_t[0]:
                if (wid == EOS):
                    break
                x_tokens.append(vocab_tgt.id2token(wid))
            if (len(x_tokens) > 0):
                trans.append(vocab_tgt.tokenizer.detokenize(x_tokens))
            else:
                trans.append(('%s' % vocab_tgt.id2token(EOS)))
    origin_order = np.argsort(numbers).tolist()
    trans = [trans[ii] for ii in origin_order]
    infer_progress_bar.close()
    if (not os.path.exists(valid_dir)):
        os.mkdir(valid_dir)
    hyp_path = os.path.join(valid_dir, 'trans.iter{0}.txt'.format(uidx))
    with open(hyp_path, 'w') as f:
        for line in trans:
            f.write(('%s\n' % line))
    with open(hyp_path) as f:
        bleu_v = bleu_scorer.corpus_bleu(f)
    return bleu_v
def bleu_validation(uidx, valid_iterator, model, bleu_scorer, vocab_tgt, batch_size, valid_dir='./valid', max_steps=10, beam_size=5, alpha=(- 1.0)):
    model.eval()
    numbers = []
    trans = []
    infer_progress_bar = tqdm(total=len(valid_iterator), desc=' - (Infer)  ', unit='sents')
    valid_iter = valid_iterator.build_generator(batch_size=batch_size)
    for batch in valid_iter:
        seq_nums = batch[0]
        numbers += seq_nums
        seqs_x = batch[1]
        infer_progress_bar.update(len(seqs_x))
        x = prepare_data(seqs_x, cuda=GlobalNames.USE_GPU)
        with torch.no_grad():
            word_ids = beam_search(nmt_model=model, beam_size=beam_size, max_steps=max_steps, src_seqs=x, alpha=alpha)
        word_ids = word_ids.cpu().numpy().tolist()
        for sent_t in word_ids:
            sent_t = [[wid for wid in line if (wid != PAD)] for line in sent_t]
            x_tokens = []
            for wid in sent_t[0]:
                if (wid == EOS):
                    break
                x_tokens.append(vocab_tgt.id2token(wid))
            if (len(x_tokens) > 0):
                trans.append(vocab_tgt.tokenizer.detokenize(x_tokens))
            else:
                trans.append(('%s' % vocab_tgt.id2token(EOS)))
    origin_order = np.argsort(numbers).tolist()
    trans = [trans[ii] for ii in origin_order]
    infer_progress_bar.close()
    if (not os.path.exists(valid_dir)):
        os.mkdir(valid_dir)
    hyp_path = os.path.join(valid_dir, 'trans.iter{0}.txt'.format(uidx))
    with open(hyp_path, 'w') as f:
        for line in trans:
            f.write(('%s\n' % line))
    with open(hyp_path) as f:
        bleu_v = bleu_scorer.corpus_bleu(f)
    return bleu_v