Exemplo n.º 1
0
 def test_topp_sampling_search_low_prob(self):
     # Given a prob low enough to top-P sampling, we expect only the top
     # 1 token to be sampled, which always results in the same output.
     low_sampling_topp = self.min_top1_prob / 2.0
     search_strategy = search.Sampling(self.tgt_dict,
                                       sampling_topp=low_sampling_topp)
     generator = SequenceGenerator([self.model],
                                   self.tgt_dict,
                                   beam_size=2,
                                   search_strategy=search_strategy)
     sample = {
         'net_input': {
             'src_tokens': self.src_tokens,
             'src_lengths': self.src_lengths
         }
     }
     hypos = generator.forward(sample)
     eos, w1 = self.eos, self.w1
     # sentence 1, beam 1
     self.assertHypoTokens(hypos[0][0], [w1, w1, eos])
     self.assertHypoScore(hypos[0][0], [1.0, 0.4, 1.0])
     # sentence 1, beam 2
     self.assertHypoTokens(hypos[0][1], [w1, w1, eos])
     self.assertHypoScore(hypos[0][1], [1.0, 0.4, 1.0])
     # sentence 2, beam 1
     self.assertHypoTokens(hypos[1][0], [w1, w1, eos])
     self.assertHypoScore(hypos[1][0], [1.0, 0.4, 1.0])
     # sentence 2, beam 2
     self.assertHypoTokens(hypos[1][1], [w1, w1, eos])
     self.assertHypoScore(hypos[1][1], [1.0, 0.4, 1.0])
Exemplo n.º 2
0
 def test_diverse_beam_search(self):
     search_strategy = search.DiverseBeamSearch(self.tgt_dict,
                                                num_groups=2,
                                                diversity_strength=0.)
     generator = SequenceGenerator(
         [self.model],
         self.tgt_dict,
         beam_size=2,
         search_strategy=search_strategy,
     )
     sample = {
         'net_input': {
             'src_tokens': self.src_tokens,
             'src_lengths': self.src_lengths
         }
     }
     hypos = generator.forward(sample)
     eos, w1, w2 = self.eos, self.w1, self.w2
     # sentence 1, beam 1
     self.assertHypoTokens(hypos[0][0], [w1, w1, eos])
     self.assertHypoScore(hypos[0][0], [0.9, 0.6, 1.0])
     # sentence 1, beam 2
     self.assertHypoTokens(hypos[0][1], [w1, w1, eos])
     self.assertHypoScore(hypos[0][1], [0.9, 0.6, 1.0])
     # sentence 2, beam 1
     self.assertHypoTokens(hypos[1][0], [w1, w2, eos])
     self.assertHypoScore(hypos[1][0], [0.7, 0.4, 0.9])
     # sentence 2, beam 2
     self.assertHypoTokens(hypos[1][1], [w1, w2, eos])
     self.assertHypoScore(hypos[1][1], [0.7, 0.4, 0.9])
Exemplo n.º 3
0
 def test_diverse_beam_search(self):
     search_strategy = search.DiverseSiblingsSearch(self.tgt_dict,
                                                    diversity_rate=0.5)
     generator = SequenceGenerator([self.model],
                                   self.tgt_dict,
                                   beam_size=2,
                                   search_strategy=search_strategy)
     sample = {
         "net_input": {
             "src_tokens": self.src_tokens,
             "src_lengths": self.src_lengths,
         }
     }
     hypos = generator.forward(sample)
     eos, w1, w2 = self.eos, self.w1, self.w2
     # sentence 1, beam 1
     self.assertHypoTokens(hypos[0][0], [w1, w1, eos])
     self.assertHypoScore(hypos[0][0], [0.9, 0.6, 1.0], [0, 1, 1], 0.5)
     # sentence 1, beam 2
     self.assertHypoTokens(hypos[0][1], [w1, w2, eos])
     self.assertHypoScore(hypos[0][1], [0.9, 0.4, 1.0], [0, 2, 1], 0.5)
     # sentence 2, beam 1
     self.assertHypoTokens(hypos[1][0], [w1, w2, eos])
     self.assertHypoScore(hypos[1][0], [0.7, 0.4, 0.9], [0, 1, 1], 0.5)
     # sentence 2, beam 2
     self.assertHypoTokens(hypos[1][1], [w1, w1, eos])
     self.assertHypoScore(hypos[1][1], [0.7, 0.35, 0.9], [0, 2, 1], 0.5)
Exemplo n.º 4
0
 def test_prefix_beam_search(self):
     search_strategy = search.BeamSearch(self.tgt_dict)
     generator = SequenceGenerator(
         [self.model],
         self.tgt_dict,
         beam_size=self.beam_size,
         search_strategy=search_strategy,
     )
     sample = {
         "net_input": {
             "src_tokens": self.tokens,
             "src_lengths": self.token_lengths,
         }
     }
     # make sure test sample doesn't break any assertion
     generator.forward(sample, prefix_tokens=self.tokens[:, :-1])
Exemplo n.º 5
0
    def test_topp_sampling_search_high_prob(self):
        # Given a prob high enough to top-P sampling, any of the top 2
        # tokens could be sampled. This can cause different outputs.
        high_sampling_topp = (self.min_top1_prob + self.min_top2_prob) / 2.0
        search_strategy = search.Sampling(
            self.tgt_dict, sampling_topp=high_sampling_topp
        )
        generator = SequenceGenerator(
            [self.model], self.tgt_dict, beam_size=2, search_strategy=search_strategy
        )
        sample = {
            "net_input": {
                "src_tokens": self.src_tokens,
                "src_lengths": self.src_lengths,
            }
        }
        hypos = generator.forward(sample)
        eos, w1, w2 = self.eos, self.w1, self.w2
        # sentence 1, beam 1
        self.assertTrue(
            self.hypoTokens(hypos[0][0], [w1, w1, eos])
            or self.hypoTokens(hypos[0][0], [w1, w2, eos])
        )
        self.assertTrue(
            self.hypoScore(hypos[0][0], [1.0, 0.4, 1.0])
            or self.hypoScore(hypos[0][0], [1.0, 0.35, 1.0])
        )

        # sentence 1, beam 2
        self.assertTrue(
            self.hypoTokens(hypos[0][1], [w1, w1, eos])
            or self.hypoTokens(hypos[0][1], [w1, w2, eos])
        )
        self.assertTrue(
            self.hypoScore(hypos[0][1], [1.0, 0.4, 1.0])
            or self.hypoScore(hypos[0][1], [1.0, 0.35, 1.0])
        )

        # sentence 2, beam 1
        self.assertTrue(
            self.hypoTokens(hypos[1][0], [w1, w1, eos])
            or self.hypoTokens(hypos[1][0], [w1, w2, eos])
        )
        self.assertTrue(
            self.hypoScore(hypos[1][0], [1.0, 0.4, 1.0])
            or self.hypoScore(hypos[1][0], [1.0, 0.35, 1.0])
        )

        # sentence 2, beam 2
        self.assertTrue(
            self.hypoTokens(hypos[1][1], [w1, w1, eos])
            or self.hypoTokens(hypos[1][1], [w1, w2, eos])
        )
        self.assertTrue(
            self.hypoScore(hypos[1][1], [1.0, 0.4, 1.0])
            or self.hypoScore(hypos[1][1], [1.0, 0.35, 1.0])
        )
Exemplo n.º 6
0
 def test_encoder_with_different_output_len(self):
     args = self.model.encoder.args
     task = test_utils.TestTranslationTask.setup_task(args, self.tgt_dict, self.tgt_dict)
     reshaping_model = test_utils.TestReshapingModel.build_model(args, task)
     generator = SequenceGenerator([reshaping_model], self.tgt_dict, beam_size=2, max_len_b=2)
     hypos = generator.forward(self.sample)
     for sent in [0, 1]:
         for beam in [0, 1]:
             assert hypos[sent][beam]['attention'] is not None
Exemplo n.º 7
0
 def test_generation_with_additional_input(self):
     args = self.model.encoder.args
     task = test_utils.TestTranslationTask.setup_task(args, self.tgt_dict, self.tgt_dict)
     add_input_model = test_utils.TestAdditionalInputModel.build_model(args, task)
     generator = SequenceGenerator([add_input_model], self.tgt_dict, beam_size=2)
     sample = self.sample.copy()
     sample['net_input']['fancy_other_input'] = sample['net_input']['src_tokens']
     hypos = generator.forward(self.sample)
     eos, w1, w2 = self.tgt_dict.eos(), self.w1, self.w2
     # sentence 1, beam 1
     self.assertHypoTokens(hypos[0][0], [w1, eos])
     self.assertHypoScore(hypos[0][0], [0.9, 1.0])
Exemplo n.º 8
0
 def test_with_normalization(self):
     generator = SequenceGenerator([self.model], self.tgt_dict, beam_size=2)
     hypos = generator.forward(self.sample)
     eos, w1, w2 = self.tgt_dict.eos(), self.w1, self.w2
     # sentence 1, beam 1
     self.assertHypoTokens(hypos[0][0], [w1, eos])
     self.assertHypoScore(hypos[0][0], [0.9, 1.0])
     # sentence 1, beam 2
     self.assertHypoTokens(hypos[0][1], [w2, w1, w2, eos])
     self.assertHypoScore(hypos[0][1], [0.1, 0.9, 0.9, 1.0])
     # sentence 2, beam 1
     self.assertHypoTokens(hypos[1][0], [w1, w2, w1, eos])
     self.assertHypoScore(hypos[1][0], [0.7, 0.4, 0.4, 1.0])
     # sentence 2, beam 2
     self.assertHypoTokens(hypos[1][1], [w1, w2, eos])
     self.assertHypoScore(hypos[1][1], [0.7, 0.4, 0.6])
Exemplo n.º 9
0
 def test_with_lenpen_favoring_long_hypos(self):
     lenpen = 5.0
     generator = SequenceGenerator(
         [self.model], self.tgt_dict, beam_size=2, len_penalty=lenpen
     )
     hypos = generator.forward(self.sample)
     eos, w1, w2 = self.tgt_dict.eos(), self.w1, self.w2
     # sentence 1, beam 1
     self.assertHypoTokens(hypos[0][0], [w2, w1, w2, eos])
     self.assertHypoScore(hypos[0][0], [0.1, 0.9, 0.9, 1.0], lenpen=lenpen)
     # sentence 1, beam 2
     self.assertHypoTokens(hypos[0][1], [w1, eos])
     self.assertHypoScore(hypos[0][1], [0.9, 1.0], lenpen=lenpen)
     # sentence 2, beam 1
     self.assertHypoTokens(hypos[1][0], [w1, w2, w1, eos])
     self.assertHypoScore(hypos[1][0], [0.7, 0.4, 0.4, 1.0], lenpen=lenpen)
     # sentence 2, beam 2
     self.assertHypoTokens(hypos[1][1], [w1, w2, eos])
     self.assertHypoScore(hypos[1][1], [0.7, 0.4, 0.6], lenpen=lenpen)
Exemplo n.º 10
0
 def test_without_normalization(self):
     # Sentence 1: unchanged from the normalized case
     # Sentence 2: beams swap order
     generator = SequenceGenerator(
         [self.model], self.tgt_dict, beam_size=2, normalize_scores=False
     )
     hypos = generator.forward(self.sample)
     eos, w1, w2 = self.tgt_dict.eos(), self.w1, self.w2
     # sentence 1, beam 1
     self.assertHypoTokens(hypos[0][0], [w1, eos])
     self.assertHypoScore(hypos[0][0], [0.9, 1.0], normalized=False)
     # sentence 1, beam 2
     self.assertHypoTokens(hypos[0][1], [w2, w1, w2, eos])
     self.assertHypoScore(hypos[0][1], [0.1, 0.9, 0.9, 1.0], normalized=False)
     # sentence 2, beam 1
     self.assertHypoTokens(hypos[1][0], [w1, w2, eos])
     self.assertHypoScore(hypos[1][0], [0.7, 0.4, 0.6], normalized=False)
     # sentence 2, beam 2
     self.assertHypoTokens(hypos[1][1], [w1, w2, w1, eos])
     self.assertHypoScore(hypos[1][1], [0.7, 0.4, 0.4, 1.0], normalized=False)