Example #1
0
    def decode(self):
        start = time.time()
        counter = 0
        batch = self.batcher.next_batch()
        while batch is not None:  #  and counter <= 100 # 11490
            # Run beam search to get best Hypothesis
            best_summary = self.beam_search(batch)

            # Extract the output ids from the hypothesis and convert back to words
            output_ids = [int(t) for t in best_summary.tokens[1:]]
            decoded_words = data.outputids2words(
                output_ids, self.vocab,
                (batch.art_oovs[0] if config.pointer_gen else None))

            # Remove the [STOP] token from decoded_words, if necessary
            try:
                fst_stop_idx = decoded_words.index(data.STOP_DECODING)
                decoded_words = decoded_words[:fst_stop_idx]
            except ValueError:
                decoded_words = decoded_words

            original_abstract_sents = batch.original_abstracts_sents[0]

            write_for_rouge(original_abstract_sents, decoded_words, counter,
                            self._rouge_ref_dir, self._rouge_dec_dir)
            counter += 1
            if counter % 10 == 0:
                print('%d example in %d sec' % (counter, time.time() - start))
                start = time.time()
            batch = self.batcher.next_batch()

        print("Decoder has finished reading dataset for single_pass.")
        print("Now starting ROUGE eval...")
        results_dict = rouge_eval(self._rouge_ref_dir, self._rouge_dec_dir)
        rouge_log(results_dict, self._decode_dir)
Example #2
0
    def decode(self):
        start = time.time()
        counter = 0
        bleu_scores = []
        batch = self.batcher.next_batch()
        while batch is not None:
            # Run beam search to get best Hypothesis
            best_summary = self.beam_search(batch)
            import pdb
            pdb.set_trace()
            # Extract the output ids from the hypothesis and convert back to words
            output_ids = [int(t) for t in best_summary.tokens[1:]]
            decoded_words = data.outputids2words(
                output_ids, self.vocab,
                (batch.art_oovs[0] if config.pointer_gen else None))

            # Remove the [STOP] token from decoded_words, if necessary
            try:
                fst_stop_idx = decoded_words.index(data.STOP_DECODING)
                decoded_words = decoded_words[:fst_stop_idx]
            except ValueError:
                decoded_words = decoded_words

            original_abstracts = batch.original_abstracts_sents[0]
            reference = original_abstracts[0].strip().split()
            bleu = nltk.translate.bleu_score.sentence_bleu([reference],
                                                           decoded_words,
                                                           weights=(0.5, 0.5))
            bleu_scores.append(bleu)

            write_for_rouge(original_abstracts, decoded_words, counter,
                            self._rouge_ref_dir, self._rouge_dec_dir)
            counter += 1
            if counter % 1000 == 0:
                print('%d example in %d sec' % (counter, time.time() - start))
                start = time.time()

            batch = self.batcher.next_batch()

        print('Average BLEU score:', np.mean(bleu_scores))

        # uncomment this if you successfully install `pyrouge`
        print("Decoder has finished reading dataset for single_pass.")
        print("Now starting ROUGE eval...")
        results_dict = rouge_eval(self._rouge_ref_dir, self._rouge_dec_dir)
        rouge_log(results_dict, self._decode_dir)
Example #3
0
import sys

from utils import write_for_rouge, rouge_eval, rouge_log

decode_dir = sys.argv[1]

print("Decoder has finished reading dataset for single_pass.")
print("Now starting ROUGE eval...")
results_dict = rouge_eval(decode_dir + 'rouge_ref',
                          decode_dir + 'rouge_dec_dir')
rouge_log(results_dict, decode_dir + 'rouge_dec_dir')
Example #4
0
    def decode(self, data_iter):
        start = time.time()
        counter = 0
        for batch in data_iter:
            summ, summ_tag, article, article_extend, summ_lens, article_lens, oov_nums = batch
            summ, summ_tag, article, article_extend, summ_lens, article_lens, oov_nums = [
                data.repeat(config.beam_size, 1) for data in batch
            ]
            summ_lens, article_lens, oov_nums = summ_lens.squeeze(
            ), article_lens.squeeze(), oov_nums.squeeze()
            if use_cuda and torch.cuda.is_available():
                summ, article, article_extend = to_cuda(data=(summ, article,
                                                              article_extend))

            airticle_words = ids2words(article[0].squeeze()[1:], self.id2word)
            # Run beam search to get best Hypothesis
            best_summary = self.beam_search(enc_inputs=article,
                                            enc_lens=article_lens,
                                            enc_inputs_extend=article_extend,
                                            oov_nums=oov_nums,
                                            dec_lens=summ_lens)

            # Extract the output ids from the hypothesis and convert back to words
            output_ids = [int(t) for t in best_summary.tokens[1:]]
            # decoded_words = data.outputids2words(output_ids, self.vocab,
            #                                      (batch.art_oovs[0] if config.use_pgen else None))

            decoded_words = ids2words(output_ids, self.id2word)

            # Remove the [STOP] token from decoded_words, if necessary
            try:
                fst_stop_idx = decoded_words.index('<end>')
                decoded_words = decoded_words[:fst_stop_idx]
            except ValueError:
                decoded_words = decoded_words

            original_abstract_sents = ids2words(
                summ[0].squeeze()[1:],
                self.id2word)  # summary repeat beam_size time

            decoded_words = "".join(decoded_words)
            original_abstract_sents = "".join(original_abstract_sents)

            rouge = Rouge()
            rouge_score = rouge.get_scores(
                decoded_words[:len(original_abstract_sents)],
                original_abstract_sents)
            rouge_score = rouge.get_scores(str(output_ids),
                                           str(summ[0].squeeze()[1:]))

            rouge_1 = rouge_score[0]["rouge-1"]
            rouge_2 = rouge_score[0]["rouge-2"]
            rouge_l = rouge_score[0]["rouge-l"]

            # pyrouge don't support chinese, need to use token
            # decoded_words = [str(ids) for ids in output_ids]
            # original_abstract_sents = [str(ids) for ids in summ[0].squeeze()[1:].tolist()]

            # write_for_rouge(original_abstract_sents, decoded_words, counter,
            #                 self._rouge_ref_dir, self._rouge_dec_dir)
            counter += 1
            if counter % 1000 == 0:
                print('%d example in %d sec' % (counter, time.time() - start))
                start = time.time()

        print("Decoder has finished reading dataset for single_pass.")
        print("Now starting ROUGE eval...")
        results_dict = rouge_eval(self._rouge_ref_dir, self._rouge_dec_dir)
        rouge_log(results_dict, self._decode_dir)
Example #5
0
from utils import rouge_eval, rouge_log

rouge_ref_dir = 'logs/pku_all_vocab/decode_test_50maxenc_4beam_6mindec_50maxdec_ckpt-123230/reference_tmp'
rouge_dec_dir = 'logs/pku_all_vocab/decode_test_50maxenc_4beam_6mindec_50maxdec_ckpt-123230/decoded_tmp'
print("Decoder has finished reading dataset for single_pass.")
print("Now starting ROUGE eval...")
results_dict = rouge_eval(rouge_ref_dir, rouge_dec_dir)
rouge_log(
    results_dict,
    'logs/pku_all_vocab/decode_test_50maxenc_4beam_6mindec_50maxdec_ckpt-123230/'
)