Ejemplo n.º 1
0
 def get_BLEU(self, batch_y_hat, batch_y):
     """Get the average smoothed BLEU of the predictions."""
     hyps = batch_y_hat.tolist()
     refs = batch_y.tolist()
     bleus = []
     for hyp, ref in zip(hyps, refs):
         if 2 in hyp:
             hyp = hyp[:hyp.index(2)]
         if 2 in ref:
             ref = ref[:ref.index(2)]
         hyp = hyp[1:]
         ref = ref[1:]
         bleus.append(smoothed_bleu(hyp, ref))
     return torch.tensor(np.mean(bleus) * 100.)
Ejemplo n.º 2
0
 def _compute_bleu(sampled_tokens, tgt_seq):
     """Compute smoothed BLEU of sampled tokens
     """
     bleus = []
     tgt_seq = tgt_seq.cpu().numpy()
     sampled_tokens = sampled_tokens.cpu().numpy()
     tgt_mask = np.greater(tgt_seq, 0)
     for i in xrange(tgt_seq.shape[0]):
         target_len = int(tgt_mask[i].sum())
         ref_tokens = tgt_seq[i, 1:target_len - 1]
         out_tokens = list(sampled_tokens[i, 1:target_len - 1])
         if not out_tokens:
             bleus.append(0.)
         else:
             bleus.append(smoothed_bleu(out_tokens, ref_tokens))
     return np.mean(bleus)