cbp.graph

cbp.graph.base_graph

class cbp.graph.base_graph.BaseGraph(silent=True, epsilon=1, coef_policy=<function bp_policy>, config=BaseConfig())[source]

Bases: object

add_factornode(factornode)[source]

add one factor node to the graph Do the following tasks

  • add node to the recorders

  • set connections

  • set parent relation

Parameters

factornode (FactorNode) – one factor node

Returns

name of factor node

Return type

str

add_varnode(node)[source]

add one ~cbp.node.VarNode to this graph, idx follow the increasing order

Parameters

node (VarNode) – one VarNode

Returns

name of varnode

Return type

str

bake()[source]
cal_bethe(margin)[source]

calculate bethe energy

Parameters

margin (dict) – node_name : margin

Returns

KL divergence between expoert joint dist and p_graph

Return type

float

copy_bp_initialization(another_graph)[source]

copy message setup from the another graph has same structure

Parameters

another_graph (BaseGraph) – another graph which close to the optimal point

delete_node(name_str)[source]

delete node from graph, needs to check following

  • judge type, Factor or Variable

  • delete from the various recorders

  • clear connections

Parameters

name_str ([type]) – [description]

Raises

RuntimeError – [description]

exact_marginal()[source]
export_convergence_marginals()[source]

export the marginal for variable nodes and factor nodes

Returns

{node.key:node.marginal}

Return type

dict

export_marginals()[source]

export the marginal for variable nodes

Returns

{node.key:node.marginal}

Return type

dict

export_sinkhorn()[source]
first_belief_propagation()[source]
get_node(name_str)[source]
init_node_recorder()[source]
plot(png_name='file.png')[source]

plot the graph through graphviz

  • red: constrained variable node

  • blue: free variable node

  • green: factor

Parameters

png_name (str, optional) – name of figure, defaults to ‘file.png’

Raises

ValueError – [description]

pmf()[source]

output the probability mass matrix through brutal-force methods

Returns

joint probability mass matrix

Return type

ndarray

set_node(node_name, potential=None, isconstrained=None)[source]

change node property 1. check whether or not in recorder 2. change potential easily[this may a duplicate function] 3. change isconstrained if possible, delete from recorder

Parameters
  • node_name ([type]) – [description]

  • potential ([type], optional) – [description], defaults to None

  • isconstrained ([type], optional) – [description], defaults to None

Raises

RuntimeError – [description]

sinkhorn(max_iter=5000000, tolerance=1e-05)[source]
sinkhorn_update(tilde_c)[source]
tree_bp()[source]

run classical belief propagation on a tree graph, only need forward and backward

  • add attr: is_send_forward: begin send forward false, after forward

before backward true, after backward false

Raises

RuntimeError – Only works for the tree graph, loopy graph does

not work, root node not decided

cbp.graph.coef_policy

cbp.graph.coef_policy.avg_policy(node, node_map)[source]
cbp.graph.coef_policy.bp_policy(node, node_map)[source]
cbp.graph.coef_policy.crazy_policy(node, node_map)[source]
cbp.graph.coef_policy.factor_policy(node, node_map)[source]

cbp.graph.graph_model

class cbp.graph.graph_model.GraphModel(silent=True, epsilon=1, coef_policy=<function bp_policy>, config=BaseConfig())[source]

Bases: cbp.graph.base_graph.BaseGraph

bake()[source]
engine_loop(engine_fun, max_iter=5000000, tolerance=0.01, error_fun=None, isoutput=False)[source]
itsbp()[source]

run sinkhorn or iterative scaling inference

Returns

[description]

Return type

[type]

itsbp_outer_loop()[source]
norm_product_bp(max_iter=5000000, tolerance=1e-05, error_fun=None)[source]
parallel_message(run_constrained=True)[source]
run_bp()[source]
run_cnp()[source]

cbp.graph.graph_utils

cbp.graph.graph_utils.cal_marginal_from_tensor(prob_tensor, varnode_list)[source]
cbp.graph.graph_utils.get_node2root(node)[source]
cbp.graph.graph_utils.itsbp_inner_loop(loop_link, is_silent)[source]