Beispiel #1
0
 def _greedy_decode(self):
     """Performs greedy decoding from the start node. Used to obtain
     the initial hypothesis.
     """
     hypo = PartialHypothesis()
     hypos = []
     posteriors = []
     score_breakdowns = []
     scores = []
     bag = dict(self.full_bag)
     while bag:
         posterior,score_breakdown = self.apply_predictors()
         hypo.predictor_states = copy.deepcopy(self.get_predictor_states())
         hypos.append(hypo)
         posteriors.append(posterior)
         score_breakdowns.append(score_breakdown)
         best_word = utils.argmax({w: posterior[w] for w in bag})
         bag[best_word] -= 1
         if bag[best_word] < 1:
             del bag[best_word]
         self.consume(best_word)
         hypo = hypo.expand(best_word,
                            None,
                            posterior[best_word],
                            score_breakdown[best_word])
         scores.append(posterior[best_word])
     posterior,score_breakdown = self.apply_predictors()
     hypo.predictor_states = self.get_predictor_states()
     hypos.append(hypo)
     posteriors.append(posterior)
     score_breakdowns.append(score_breakdown)
     hypo = hypo.expand(utils.EOS_ID,
                        None,
                        posterior[utils.EOS_ID],
                        score_breakdown[utils.EOS_ID])
     logging.debug("Greedy hypo (%f): %s" % (
                       hypo.score,
                       ' '.join([str(w) for w in hypo.trgt_sentence])))
     scores.append(posterior[utils.EOS_ID])
     self.best_score = hypo.score
     self.add_full_hypo(hypo.generate_full_hypothesis())
     self._process_new_hypos(FlipCandidate(hypo.trgt_sentence,
                                            scores,
                                            self._create_dummy_bigrams(),
                                            hypo.score),
                              len(hypo.trgt_sentence),
                              hypos,
                              posteriors,
                              score_breakdowns)
Beispiel #2
0
 def create_initial_node(self):
     """Create the root node for the search tree. """
     init_hypo = PartialHypothesis()
     posterior,score_breakdown = self.apply_predictors()
     best_word = utils.argmax(posterior)
     init_hypo.predictor_states = self.get_predictor_states()
     init_node = BOWNode(init_hypo, posterior, score_breakdown, [])
     self._add_to_heap(init_node, best_word, 0.0) # Expected score irrelevant 
Beispiel #3
0
 def create_initial_node(self):
     """Create the root node for the search tree. """
     init_hypo = PartialHypothesis()
     posterior,score_breakdown = self.apply_predictors()
     children = sorted([RestartingChild(w, posterior[w], score_breakdown[w])
                         for w in posterior],
                       key=lambda c: c.score, reverse=True)
     init_hypo.predictor_states = self.get_predictor_states()
     heappush(self.open_nodes, (0.0, RestartingNode(init_hypo, children)))
Beispiel #4
0
 def create_initial_node(self):
     """Create the root node for the search tree. """
     init_hypo = PartialHypothesis()
     posterior, score_breakdown = self.apply_predictors()
     best_word = utils.argmax(posterior)
     init_hypo.predictor_states = self.get_predictor_states()
     init_node = BOWNode(init_hypo, posterior, score_breakdown, [])
     self._add_to_heap(init_node, best_word,
                       0.0)  # Expected score irrelevant
Beispiel #5
0
 def create_initial_node(self):
     """Create the root node for the search tree. """
     init_hypo = PartialHypothesis()
     posterior,score_breakdown = self.apply_predictors()
     children = sorted([RestartingChild(w, posterior[w], score_breakdown[w])
                         for w in posterior],
                       key=lambda c: c.score, reverse=True)
     init_hypo.predictor_states = self.get_predictor_states()
     heappush(self.open_nodes, (0.0, RestartingNode(init_hypo, children)))
Beispiel #6
0
 def _greedy_decode(self):
     """Performs greedy decoding from the start node. Used to obtain
     initial bigram statistics.
     """
     hypo = PartialHypothesis()
     hypos = []
     posteriors = []
     score_breakdowns = []
     bag = dict(self.full_bag)
     while bag:
         posterior,score_breakdown = self.apply_predictors()
         hypo.predictor_states = copy.deepcopy(self.get_predictor_states())
         bag_posterior = {w: posterior[w] for w in self.full_bag_with_eos}
         bag_breakdown = {w: score_breakdown[w] 
                                     for w in self.full_bag_with_eos}
         posteriors.append(bag_posterior)
         score_breakdowns.append(bag_breakdown)
         hypos.append(hypo)
         best_word = utils.argmax({w: bag_posterior[w] for w in bag})
         bag[best_word] -= 1
         if bag[best_word] < 1:
             del bag[best_word]
         self.consume(best_word)
         hypo = hypo.expand(best_word,
                            None,
                            bag_posterior[best_word],
                            score_breakdown[best_word])
     posterior,score_breakdown = self.apply_predictors()
     hypo.predictor_states = copy.deepcopy(self.get_predictor_states())
     bag_posterior = {w: posterior[w] for w in self.full_bag_with_eos}
     bag_breakdown = {w: score_breakdown[w] for w in self.full_bag_with_eos}
     posteriors.append(bag_posterior)
     score_breakdowns.append(bag_breakdown)
     hypos.append(hypo)
     
     hypo = hypo.cheap_expand(utils.EOS_ID,
                              bag_posterior[utils.EOS_ID],
                              score_breakdown[utils.EOS_ID])
     logging.debug("Greedy hypo (%f): %s" % (
                       hypo.score,
                       ' '.join([str(w) for w in hypo.trgt_sentence])))
     self._process_new_hypos(hypos, posteriors, score_breakdowns, hypo)
Beispiel #7
0
 def _greedy_decode(self):
     """Performs greedy decoding from the start node. Used to obtain
     initial bigram statistics.
     """
     hypo = PartialHypothesis()
     hypos = []
     posteriors = []
     score_breakdowns = []
     bag = dict(self.full_bag)
     while bag:
         posterior,score_breakdown = self.apply_predictors()
         hypo.predictor_states = copy.deepcopy(self.get_predictor_states())
         bag_posterior = {w: posterior[w] for w in self.full_bag_with_eos}
         bag_breakdown = {w: score_breakdown[w] 
                                     for w in self.full_bag_with_eos}
         posteriors.append(bag_posterior)
         score_breakdowns.append(bag_breakdown)
         hypos.append(hypo)
         best_word = utils.argmax({w: bag_posterior[w] for w in bag})
         bag[best_word] -= 1
         if bag[best_word] < 1:
             del bag[best_word]
         self.consume(best_word)
         hypo = hypo.expand(best_word,
                            None,
                            bag_posterior[best_word],
                            score_breakdown[best_word])
     posterior,score_breakdown = self.apply_predictors()
     hypo.predictor_states = copy.deepcopy(self.get_predictor_states())
     bag_posterior = {w: posterior[w] for w in self.full_bag_with_eos}
     bag_breakdown = {w: score_breakdown[w] for w in self.full_bag_with_eos}
     posteriors.append(bag_posterior)
     score_breakdowns.append(bag_breakdown)
     hypos.append(hypo)
     
     hypo = hypo.cheap_expand(utils.EOS_ID,
                              bag_posterior[utils.EOS_ID],
                              score_breakdown[utils.EOS_ID])
     logging.debug("Greedy hypo (%f): %s" % (
                       hypo.score,
                       ' '.join([str(w) for w in hypo.trgt_sentence])))
     self._process_new_hypos(hypos, posteriors, score_breakdowns, hypo)
Beispiel #8
0
 def _initialize_decoding(self, src_sentence):
     """Helper function for ``decode`` to which initializes all the
     class attributes
     """
     self.initialize_predictors(src_sentence)
     self.max_expansions = self.get_max_expansions(
         self.max_expansions_param, src_sentence)
     init_hypo = PartialHypothesis()
     init_hypo.predictor_states = self.get_predictor_states()
     init_hypo.scores = []
     init_hypo.parent_hypo_array_idx = 0  # point to guardian
     self.buckets = [[] for _ in xrange(self.max_len + 1)]
     self.expanded_hypos = [[] for _ in xrange(self.max_len + 1)]
     self.buckets[0].append((0.0, init_hypo))
     self.expand_counts = [0.0]  # with guardian
     self.expand_backpointers = [0]  # with guardian
     self.last_bucket = 0
     self.best_score = self.get_lower_score_bound()
     self.best_word_scores = [NEG_INF] * (self.max_len + 1)
     self.compressed = [True] * (self.max_len + 1)
     self.guaranteed_optimality = True
     self.cur_iter = 0
Beispiel #9
0
 def _initialize_decoding(self, src_sentence):
     """Helper function for ``decode`` to which initializes all the
     class attributes
     """
     self.initialize_predictors(src_sentence)
     self.max_expansions = self.get_max_expansions(self.max_expansions_param,
                                                   src_sentence) 
     init_hypo = PartialHypothesis()
     init_hypo.predictor_states = self.get_predictor_states()
     init_hypo.scores = []
     init_hypo.parent_hypo_array_idx = 0 # point to guardian
     self.buckets = [[] for _ in xrange(self.max_len+1)]
     self.expanded_hypos = [[] for _ in xrange(self.max_len+1)]
     self.buckets[0].append((0.0, init_hypo))
     self.expand_counts = [0.0] # with guardian
     self.expand_backpointers = [0] # with guardian
     self.last_bucket = 0
     self.best_score = self.get_lower_score_bound()
     self.best_word_scores = [NEG_INF] * (self.max_len+1)
     self.compressed = [True] * (self.max_len+1)
     self.guaranteed_optimality = True
     self.cur_iter = 0