def compute_score(predictions, references, vocab=None):
    'Computes the bleu score.\n\n  Args:\n    predictions: a numpy arrary in the shape of [batch_size, max_phrase_length]\n    references: a numpy array in the shape of [batch_size, 7, 10]\n    vocab: the vocabulary file.\n\n  Returns:\n    a scalar value for the corpus level bleu score.\n  '
    assert (np.rank(predictions) == 2)
    assert (predictions.shape[0] == references.shape[0])
    batch_size = predictions.shape[0]
    predictions = tf.make_ndarray(tf.make_tensor_proto(predictions)).tolist()
    references = tf.make_ndarray(tf.make_tensor_proto(references)).tolist()
    hypotheses_list = []
    references_list = []
    for index in range(batch_size):
        h = predictions[index]
        try:
            eos_index = h.index(input_utils.EOS)
        except ValueError:
            eos_index = len(h)
        hypotheses_list.append(h[:eos_index])
        ref = references[index].decode().split('|')
        ref_list = [r.strip().split(' ') for r in ref if r.strip()]
        references_list.append(ref_list)
    all_scores = collections.defaultdict(list)
    for (hypothesis, references) in zip(hypotheses_list, references_list):
        if ((vocab is not None) and len(vocab)):
            hypothesis = [vocab[word_id].numpy().decode() for word_id in hypothesis if (word_id > 3)]
            logging.info('hypothesis: %s', str(hypothesis))
            logging.info('references: %s', str(references))
        h_str = ' '.join((str(e) for e in hypothesis))
        r_str = [' '.join((str(e) for e in ref)) for ref in references]
        scores = widget_caption_eval.coco_evaluate(r_str, h_str)
        for (key, score) in scores.items():
            all_scores[key].append(score)
    score_names = ['BLEU-1', 'BLEU-2', 'BLEU-3', 'BLEU-4', 'ROUGE-1-f1-mean', 'ROUGE-1-f1-min', 'ROUGE-1-f1-max', 'ROUGE-2-f1-mean', 'ROUGE-2-f1-min', 'ROUGE-2-f1-max', 'ROUGE-L-f1-mean', 'ROUGE-L-f1-min', 'ROUGE-L-f1-max']
    return [np.array(all_scores[name], dtype=np.float32) for name in score_names]
Computes the bleu score.

Args:
  predictions: a numpy arrary in the shape of [batch_size, max_phrase_length]
  references: a numpy array in the shape of [batch_size, 7, 10]
  vocab: the vocabulary file.

Returns:
  a scalar value for the corpus level bleu score.
def compute_score(predictions, references, vocab=None):
    'Computes the bleu score.\n\n  Args:\n    predictions: a numpy arrary in the shape of [batch_size, max_phrase_length]\n    references: a numpy array in the shape of [batch_size, 7, 10]\n    vocab: the vocabulary file.\n\n  Returns:\n    a scalar value for the corpus level bleu score.\n  '
    assert (np.rank(predictions) == 2)
    assert (predictions.shape[0] == references.shape[0])
    batch_size = predictions.shape[0]
    predictions = tf.make_ndarray(tf.make_tensor_proto(predictions)).tolist()
    references = tf.make_ndarray(tf.make_tensor_proto(references)).tolist()
    hypotheses_list = []
    references_list = []
    for index in range(batch_size):
        h = predictions[index]
        try:
            eos_index = h.index(input_utils.EOS)
        except ValueError:
            eos_index = len(h)
        hypotheses_list.append(h[:eos_index])
        ref = references[index].decode().split('|')
        ref_list = [r.strip().split(' ') for r in ref if r.strip()]
        references_list.append(ref_list)
    all_scores = collections.defaultdict(list)
    for (hypothesis, references) in zip(hypotheses_list, references_list):
        if ((vocab is not None) and len(vocab)):
            hypothesis = [vocab[word_id].numpy().decode() for word_id in hypothesis if (word_id > 3)]
            logging.info('hypothesis: %s', str(hypothesis))
            logging.info('references: %s', str(references))
        h_str = ' '.join((str(e) for e in hypothesis))
        r_str = [' '.join((str(e) for e in ref)) for ref in references]
        scores = widget_caption_eval.coco_evaluate(r_str, h_str)
        for (key, score) in scores.items():
            all_scores[key].append(score)
    score_names = ['BLEU-1', 'BLEU-2', 'BLEU-3', 'BLEU-4', 'ROUGE-1-f1-mean', 'ROUGE-1-f1-min', 'ROUGE-1-f1-max', 'ROUGE-2-f1-mean', 'ROUGE-2-f1-min', 'ROUGE-2-f1-max', 'ROUGE-L-f1-mean', 'ROUGE-L-f1-min', 'ROUGE-L-f1-max']
    return [np.array(all_scores[name], dtype=np.float32) for name in score_names]