Example #1
0
 def generate(self, src, idx, src_mask=None, forced_trg_ids=None):
   if not xnmt.batcher.is_batched(src):
     src = xnmt.batcher.mark_as_batch([src])
   else:
     assert src_mask is not None
   outputs = []
   for sents in src:
     self.start_sent(src)
     embeddings = self.src_embedder.embed_sent(src)
     encodings = self.encoder(embeddings)
     self.attender.init_sent(encodings)
     ss = mark_as_batch([Vocab.SS] * len(src)) if is_batched(src) else Vocab.SS
     dec_state = self.decoder.initial_state(self.encoder.get_final_states(), self.trg_embedder.embed(ss))
     output_actions, score = self.search_strategy.generate_output(self.decoder, self.attender, self.trg_embedder, dec_state, src_length=len(sents), forced_trg_ids=forced_trg_ids)
     # In case of reporting
     if self.report_path is not None:
       src_words = [self.reporting_src_vocab[w] for w in sents]
       trg_words = [self.trg_vocab[w] for w in output_actions[1:]]
       attentions = self.attender.attention_vecs
       self.set_report_input(idx, src_words, trg_words, attentions)
       self.set_report_resource("src_words", src_words)
       self.set_report_path('{}.{}'.format(self.report_path, str(idx)))
       self.generate_report(self.report_type)
     # Append output to the outputs
     outputs.append(TextOutput(actions=output_actions,
                               vocab=self.trg_vocab if hasattr(self, "trg_vocab") else None,
                               score=score))
   return outputs
Example #2
0
    def generate(self,
                 src,
                 idx,
                 src_mask=None,
                 forced_trg_ids=None,
                 search_strategy=None):
        self.start_sent(src)
        if not xnmt.batcher.is_batched(src):
            src = xnmt.batcher.mark_as_batch([src])
        else:
            assert src_mask is not None
        outputs = []

        trg = SimpleSentenceInput([0])

        if not xnmt.batcher.is_batched(trg):
            trg = xnmt.batcher.mark_as_batch([trg])

        output_actions = []
        score = 0.

        # TODO Fix this with output_one_step and use the appropriate search_strategy
        self.max_len = 100  # This is a temporary hack
        for _ in range(self.max_len):
            dy.renew_cg(immediate_compute=settings.IMMEDIATE_COMPUTE,
                        check_validity=settings.CHECK_VALIDITY)
            log_prob_tail = self.calc_loss(src,
                                           trg,
                                           loss_cal=None,
                                           infer_prediction=True)
            ys = np.argmax(log_prob_tail.npvalue(), axis=0).astype('i')
            if ys == Vocab.ES:
                output_actions.append(ys)
                break
            output_actions.append(ys)
            trg = SimpleSentenceInput(output_actions + [0])
            if not xnmt.batcher.is_batched(trg):
                trg = xnmt.batcher.mark_as_batch([trg])

        # In case of reporting
        sents = src[0]
        if self.report_path is not None:
            src_words = [self.reporting_src_vocab[w] for w in sents]
            trg_words = [self.trg_vocab[w] for w in output_actions]
            self.set_report_input(idx, src_words, trg_words)
            self.set_report_resource("src_words", src_words)
            self.set_report_path('{}.{}'.format(self.report_path, str(idx)))
            self.generate_report(self.report_type)

        # Append output to the outputs
        if hasattr(self, "trg_vocab") and self.trg_vocab is not None:
            outputs.append(TextOutput(output_actions, self.trg_vocab))
        else:
            outputs.append((output_actions, score))

        return outputs
Example #3
0
 def generate(self, src, idx, search_strategy, src_mask=None, forced_trg_ids=None):
   if not xnmt.batcher.is_batched(src):
     src = xnmt.batcher.mark_as_batch([src])
   else:
     assert src_mask is not None
   # Generating outputs
   outputs = []
   for sents in src:
     self.start_sent(src)
     embeddings = self.src_embedder.embed_sent(src)
     encodings = self.encoder(embeddings)
     self.attender.init_sent(encodings)
     ss = mark_as_batch([Vocab.SS] * len(src)) if is_batched(src) else Vocab.SS
     initial_state = self.decoder.initial_state(self.encoder.get_final_states(), self.trg_embedder.embed(ss))
     search_outputs = search_strategy.generate_output(self, initial_state,
                                                      src_length=[len(sents)],
                                                      forced_trg_ids=forced_trg_ids)
     best_output = sorted(search_outputs, key=lambda x: x.score[0], reverse=True)[0]
     output_actions = [x for x in best_output.word_ids[0]]
     attentions = [x for x in best_output.attentions[0]]
     score = best_output.score[0]
     # In case of reporting
     if self.report_path is not None:
       if self.reporting_src_vocab:
         src_words = [self.reporting_src_vocab[w] for w in sents]
       else:
         src_words = ['' for w in sents]
       trg_words = [self.trg_vocab[w] for w in output_actions]
       # Attentions
       attentions = np.concatenate([x.npvalue() for x in attentions], axis=1)
       # Segmentation
       segment = self.get_report_resource("segmentation")
       if segment is not None:
         segment = [int(x[0]) for x in segment]
         src_inp = [x[0] for x in self.encoder.apply_segmentation(src_words, segment)]
       else:
         src_inp = src_words
       # Other Resources
       self.set_report_input(idx, src_inp, trg_words, attentions)
       self.set_report_resource("src_words", src_words)
       self.set_report_path('{}.{}'.format(self.report_path, str(idx)))
       self.generate_report(self.report_type)
     # Append output to the outputs
     outputs.append(TextOutput(actions=output_actions,
                               vocab=self.trg_vocab if hasattr(self, "trg_vocab") else None,
                               score=score))
   self.outputs = outputs
   return outputs
Example #4
0
 def generate(self, src, idx, src_mask=None, forced_trg_ids=None):
   if not xnmt.batcher.is_batched(src):
     src = xnmt.batcher.mark_as_batch([src])
   else:
     assert src_mask is not None
   outputs = []
   for sents in src:
     self.start_sent(src)
     embeddings = self.src_embedder.embed_sent(src)
     encodings = self.encoder(embeddings)
     self.attender.init_sent(encodings)
     ss = mark_as_batch([Vocab.SS] * len(src)) if is_batched(src) else Vocab.SS
     dec_state = self.decoder.initial_state(self.encoder.get_final_states(), self.trg_embedder.embed(ss))
     output_actions, score = self.search_strategy.generate_output(self.decoder, self.attender, self.trg_embedder, dec_state, src_length=len(sents), forced_trg_ids=forced_trg_ids)
     # In case of reporting
     if self.report_path is not None:
       if self.reporting_src_vocab:
         src_words = [self.reporting_src_vocab[w] for w in sents]
       else:
         src_words = ['' for w in sents]
       trg_words = [self.trg_vocab[w] for w in output_actions.word_ids]
       # Attentions
       attentions = output_actions.attentions
       if type(attentions) == dy.Expression:
         attentions = attentions.npvalue()
       elif type(attentions) == list:
         attentions = np.concatenate([x.npvalue() for x in attentions], axis=1)
       elif type(attentions) != np.ndarray:
         raise RuntimeError("Illegal type for attentions in translator report: {}".format(type(attentions)))
       # Segmentation
       segment = self.get_report_resource("segmentation")
       if segment is not None:
         segment = [int(x[0]) for x in segment]
         src_inp = [x[0] for x in self.encoder.apply_segmentation(src_words, segment)]
       else:
         src_inp = src_words
       # Other Resources
       self.set_report_input(idx, src_inp, trg_words, attentions)
       self.set_report_resource("src_words", src_words)
       self.set_report_path('{}.{}'.format(self.report_path, str(idx)))
       self.generate_report(self.report_type)
     # Append output to the outputs
     outputs.append(TextOutput(actions=output_actions.word_ids,
                               vocab=self.trg_vocab if hasattr(self, "trg_vocab") else None,
                               score=score))
   self.outputs = outputs
   return outputs
Example #5
0
    def generate(self, src, idx, forced_trg_ids=None, search_strategy=None):
        self.start_sent(src)
        if not xnmt.batcher.is_batched(src):
            src = xnmt.batcher.mark_as_batch([src])
        outputs = []

        trg = SimpleSentenceInput([0])

        if not xnmt.batcher.is_batched(trg):
            trg = xnmt.batcher.mark_as_batch([trg])

        output_actions = []
        score = 0.

        # TODO Fix this with generate_one_step and use the appropriate search_strategy
        self.max_len = 100  # This is a temporary hack
        for _ in range(self.max_len):
            dy.renew_cg(immediate_compute=settings.IMMEDIATE_COMPUTE,
                        check_validity=settings.CHECK_VALIDITY)
            log_prob_tail = self.calc_loss(src,
                                           trg,
                                           loss_cal=None,
                                           infer_prediction=True)
            ys = np.argmax(log_prob_tail.npvalue(), axis=0).astype('i')
            if ys == Vocab.ES:
                output_actions.append(ys)
                break
            output_actions.append(ys)
            trg = SimpleSentenceInput(output_actions + [0])
            if not xnmt.batcher.is_batched(trg):
                trg = xnmt.batcher.mark_as_batch([trg])

        # Append output to the outputs
        if hasattr(self, "trg_vocab") and self.trg_vocab is not None:
            outputs.append(
                TextOutput(actions=output_actions, vocab=self.trg_vocab))
        else:
            outputs.append((output_actions, score))

        return outputs
Example #6
0
    def generate(self,
                 src: Batch,
                 idx: Sequence[int],
                 search_strategy: SearchStrategy,
                 forced_trg_ids: Batch = None):
        if src.batch_size() != 1:
            raise NotImplementedError(
                "batched decoding not implemented for DefaultTranslator. "
                "Specify inference batcher with batch size 1.")
        assert src.batch_size() == len(
            idx), f"src: {src.batch_size()}, idx: {len(idx)}"
        # Generating outputs
        self.start_sent(src)
        outputs = []
        cur_forced_trg = None
        sent = src[0]
        sent_mask = None
        if src.mask: sent_mask = Mask(np_arr=src.mask.np_arr[0:1])
        sent_batch = mark_as_batch([sent], mask=sent_mask)
        # TODO MBR can be implemented here. It takes only the first result from the encoder
        # To further implement MBR, we need to handle the generation considering multiple encoder output.
        initial_state = self._encode_src(sent_batch)[0]
        if forced_trg_ids is not None: cur_forced_trg = forced_trg_ids[0]
        search_outputs = search_strategy.generate_output(
            self,
            initial_state,
            src_length=[sent.sent_len()],
            forced_trg_ids=cur_forced_trg)
        sorted_outputs = sorted(search_outputs,
                                key=lambda x: x.score[0],
                                reverse=True)
        assert len(sorted_outputs) >= 1
        for curr_output in sorted_outputs:
            output_actions = [x for x in curr_output.word_ids[0]]
            attentions = [x for x in curr_output.attentions[0]]
            score = curr_output.score[0]
            if len(sorted_outputs) == 1:
                outputs.append(
                    TextOutput(actions=output_actions,
                               vocab=getattr(self.trg_reader, "vocab", None),
                               score=score))
            else:
                outputs.append(
                    NbestOutput(TextOutput(actions=output_actions,
                                           vocab=getattr(
                                               self.trg_reader, "vocab", None),
                                           score=score),
                                nbest_id=idx[0]))
        if self.compute_report:
            attentions = np.concatenate([x.npvalue() for x in attentions],
                                        axis=1)
            self.add_sent_for_report({
                "idx":
                idx[0],
                "attentions":
                attentions,
                "src":
                sent,
                "src_vocab":
                getattr(self.src_reader, "vocab", None),
                "trg_vocab":
                getattr(self.trg_reader, "vocab", None),
                "output":
                outputs[0]
            })

        return outputs