def six_node_graph(): Path('data').mkdir(exist_ok=True) rng = RandomState(1) graph = GraphModel() rv_dim = 2 # init varnode for _ in range(6): potential = np.ones([rv_dim]) varnode = VarNode(rv_dim, potential) graph.add_varnode(varnode) # init factornode edges = [ [0, 1], [1, 2], [1, 3], [3, 4], [3, 5] ] for item in edges: potential = rng.normal(size=[rv_dim, rv_dim]) factorname = [f"VarNode_{data:03d}" for data in item] factornode = FactorNode(factorname, np.exp(potential)) graph.add_factornode(factornode) graph.plot(f"data/six_node_graph.png") return graph
def add_factor_different(self, name_list, is_conv=False): if is_conv: factor_potential = diagonal_potential_conv(self.node_dim, self.node_dim, self.rng) else: factor_potential = diagonal_potential_different( self.node_dim, self.node_dim, self.rng) factornode = FactorNode(name_list, factor_potential) self.graph.add_factornode(factornode) return factornode
def setUp(self): self.graph = GraphModel(coef_policy=bp_policy) # init varnode potential = np.ones(2) self.varnode_1 = VarNode(2, potential) self.graph.add_varnode(self.varnode_1) varnode_name = [node.name for node in [self.varnode_1]] factor_potential = np.array( [3, 2] ) self.factornode_1 = FactorNode(varnode_name, factor_potential) self.graph.add_factornode(self.factornode_1) factor_potential_2 = np.array( [1, 4] ) self.factornode_2 = FactorNode(varnode_name, factor_potential_2) self.graph.add_factornode(self.factornode_2)
def three_node_tree(): graph = two_node_tree() varnode = VarNode(2) graph.add_varnode(varnode) connect_var = ['VarNode_001', 'VarNode_002'] factor_potential = np.array([ [6, 14], [1, 4] ]) factornode = FactorNode(connect_var, factor_potential) graph.add_factornode(factornode) return graph
def two_node_tree(): graph = GraphModel() # init varnode for _ in range(2): varnode = VarNode(2) graph.add_varnode(varnode) connect_var = ['VarNode_000', 'VarNode_001'] factor_potential = np.array([ [3, 2], [3, 17] ]) factornode = FactorNode(connect_var, factor_potential) graph.add_factornode(factornode) return graph
def add_factor(self, name_list, is_conv=False): """add factor to hmm graph :param name_list: connected node :type name_list: list :param is_conv: is emit or not , defaults to False-- transition :type is_conv: bool, optional :return: FactorNode :rtype: cbp.FactorNode """ if is_conv: # emit potential = self.simulator.get_emission_potential() else: potential = self.simulator.get_transition_potential() factornode = FactorNode(name_list, potential) self.graph.add_factornode(factornode) return factornode
def add_factor(self, name_list, is_conv=False): factor_potential = identity_potential(self.node_dim, self.node_dim, self.rng) factornode = FactorNode(name_list, factor_potential) self.graph.add_factornode(factornode) return factornode