Ejemplo n.º 1
0
    def __call__(self, history):
        status = history.status()

        if status.x_diff is None:
            pydecode.pairwise_dot(self.potentials, status.x, self.dual_weights)
        else:
            pydecode.pairwise_dot(self.potentials, status.x_diff, self.dual_weights)
        path = self.dual_weights.kind.viterbi(self.current_graph,
                                              self.dual_weights,
                                              self.chart).path
        # path = pydecode.best_path(self.current_graph,
        #                     self.dual_weights)

        score = self.dual_weights.dot(path)
        vec = self.potentials.dot(path)
        subgrad = np.zeros(len(status.x))

        prune = None

        # Call prune function
        #if self.use_prune and history.gap() < history.last_prune - 5:
        if self.pruning_function is not None:
            #print "pruning "
            pruning = \
                self.pruning_function(self.current_graph,
                                      self.dual_weights,
                                      self.potentials,
                                      history)

            # pruning = pydecode.prune_hypergraph(self.current_graph,
            #                               self.dual_weights,
            #                               history.best_primal)

            if pruning is not None:
                graph, dual_weights, potentials = pruning
                self.dual_weights = dual_weights
                self.potentials = potentials
                print "OLD SIZE", len(self.current_grapydecode.nodes)
                self.current_graph = graph
                self.chart = pydecode.LogViterbiChart(self.current_graph)
                print "NEW SIZE", len(self.current_graph.nodes)
                prune = history.gap()

        for i, j in vec:
            subgrad[i] = j

        return SubgradientRoundResult(
            dual=score, subgrad=subgrad,
            extra=path, prune=prune,
            constraints=vec)
Ejemplo n.º 2
0
    def __call__(self, x, x_diff):
        """
        Parameters
        ----------

        Returns
        --------
        path, subgradient, dual_score
        """

        if x_diff is None:
            pydecode.pairwise_dot(self.constraint_potentials, x, self.dual_weights)
        else:
            pydecode.pairwise_dot(self.constraint_potentials, x_diff,
                            self.dual_weights)

        path = pydecode.best_path(self.current_graph, self.dual_weights)
        dual_score = self.dual_weights.dot(path)
        constraint_vector = self.constraint_potentials.dot(path)
        subgradient = np.zeros(len(x))
        for i, j in constraint_vector:
            subgradient[i] = j
        return path, subgradient, dual_score