Beispiel #1
0
    def pass_msg_down(self, sender, tree, N, receiver):
        """
        downward message is the sum over all states of the product of
         the downward message from sender's parent,
         the product of upward messages to the sender from his other children,
         the sender-receiver edge potential,
         the sender node potential
        If the sender is the root, the calculation is somewhat simplified.

        :param sender: parent
        :param receiver: child
        """
        down_msg_temp = np.zeros(N, 'f') + logzero()
        curr_edge = tree.get_edge_by_nodes(sender, receiver)
        # can be zero if there's not children
        product_child = sum([
            tree.get_edge_by_nodes(sender, c).up_msg
            for c in sender.get_children() if c != receiver
        ])

        if sender.is_root():
            down_msg_temp = curr_edge.potentials + product_child
        else:
            prev_edge = tree.get_edge_by_nodes(sender.get_parent(), sender)
            product = project_kbest(
                prev_edge.down_msg + product_child
            ) if self.approximate else prev_edge.down_msg + product_child
            product += sender.potentials
            for curr_state in range(N):  # TODO optimize
                down_msg_temp[curr_state] = sselogsum(
                    product + curr_edge.potentials[:, curr_state])

        curr_edge.down_msg = down_msg_temp
Beispiel #2
0
    def pass_msg_down(self, sender, tree, N, receiver):
        """
        downward message is the sum over all states of the product of
         the downward message from sender's parent,
         the product of upward messages to the sender from his other children,
         the sender-receiver edge potential,
         the sender node potential
        If the sender is the root, the calculation is somewhat simplified.

        :param sender: parent
        :param receiver: child
        """
        down_msg_temp = np.zeros(N, 'f') + logzero()
        curr_edge = tree.get_edge_by_nodes(sender, receiver)
        # can be zero if there's not children
        product_child = sum([tree.get_edge_by_nodes(sender, c).up_msg
                             for c in sender.get_children()
                             if c != receiver])

        if sender.is_root():
            down_msg_temp = curr_edge.potentials + product_child
        else:
            prev_edge = tree.get_edge_by_nodes(sender.get_parent(), sender)
            product = project_kbest(
                prev_edge.down_msg + product_child) if self.approximate else prev_edge.down_msg + product_child
            product += sender.potentials
            for curr_state in range(N):  # TODO optimize
                down_msg_temp[curr_state] = sselogsum(product + curr_edge.potentials[:, curr_state])

        curr_edge.down_msg = down_msg_temp
Beispiel #3
0
 def compute_belief(self, node, tree):
     """
     belief is the product of
      node's potential (emission prob) and
      the product of messages sent to the node
     """
     if not node.has_children():  # is leaf
         product = node.initial_potentials
     else:
         product = sum([tree.get_edge_by_nodes(node, child).up_msg for child in node.get_children()])
     if node.is_root():  # no potential for root
         return product
     else:
         return project_kbest(product + node.potentials) if self.approximate else product + node.potentials
Beispiel #4
0
 def compute_belief(self, node, tree):
     """
     belief is the product of
      node's potential (emission prob) and
      the product of messages sent to the node
     """
     if not node.has_children():  # is leaf
         product = node.initial_potentials
     else:
         product = sum([
             tree.get_edge_by_nodes(node, child).up_msg
             for child in node.get_children()
         ])
     if node.is_root():  # no potential for root
         return product
     else:
         return project_kbest(
             product + node.potentials
         ) if self.approximate else product + node.potentials