(model, dev_data, batch_size=32)
return ppl
Evaluate perplexity on dev sentences
    @param model (NMT): NMT Model
    @param dev_data (list of (src_sent, tgt_sent)): list of tuples containing source and target sentence
    @param batch_size (batch size)
    @returns ppl (perplixty on dev sentences)
Evaluate perplexity on dev sentences
Evaluate perplexity on dev sentences
def evaluate_ppl(model, dev_data, batch_size=32):
    """ Evaluate perplexity on dev sentences
    @param model (NMT): NMT Model
    @param dev_data (list of (src_sent, tgt_sent)): list of tuples containing source and target sentence
    @param batch_size (batch size)
    @returns ppl (perplixty on dev sentences)
    """
    was_training = model.training
    model.eval()

    cum_loss = 0.
    cum_tgt_words = 0.

    # no_grad() signals backend to throw away all gradients
    with torch.no_grad():
        for src_sents, tgt_sents in batch_iter(dev_data, batch_size):
            loss = -model(src_sents, tgt_sents).sum()

            cum_loss += loss.item()
            tgt_word_num_to_predict = sum(len(s[1:]) for s in tgt_sents)  # omitting leading `<s>`
            cum_tgt_words += tgt_word_num_to_predict

        ppl = np.exp(cum_loss / cum_tgt_words)

    if was_training:
        model.train()

    return ppl
def evaluate_ppl ( model , dev_data , batch_size = 32 ) : was_training = model . training model . eval ( ) cum_loss = 0. cum_tgt_words = 0. # no_grad() signals backend to throw away all gradients with torch . no_grad ( ) : for src_sents , tgt_sents in batch_iter ( dev_data , batch_size ) : loss = - model ( src_sents , tgt_sents ) . sum ( ) cum_loss += loss . item ( ) tgt_word_num_to_predict = sum ( len ( s [ 1 : ] ) for s in tgt_sents ) # omitting leading `<s>` cum_tgt_words += tgt_word_num_to_predict ppl = np . exp ( cum_loss / cum_tgt_words ) if was_training : model . train ( ) return ppl