贝叶斯信念网络的学习与推理
pybbn的Python项目详细描述
PyBBN
pybbn是用于贝叶斯信念网络(bbns)精确推理的python库,使用 junction tree algorithm或概率 在成簇的树上繁殖。该实现直接从C. Huang and A. Darwiche, “Inference in Belief Networks: A Procedural Guide,” in International Journal of Approximate Reasoning, vol. 15, pp. 225–263, 1999获取。Pybn也近似 使用Gibbs sampling的推理算法 线性高斯bbn模型。精确的推理算法适用于具有所有变量的bbns 这是离散的,而近似推理算法是具有所有变量的BBNs。 是连续的(假设是多元高斯分布)。此外,还有 生成单连通图和多连通图的能力,取自JS Ide and FG Cozman, “Random Generation of Bayesian Network,” in Advances in Artificial Intelligence, Lecture Notes in Computer Science, vol 2507。
精确推理用法
下面是一个创建贝叶斯信念网络,将其转换为连接树的示例代码, 然后设置观察证据。最后一行打印每个节点的边际概率。
frompybbn.graph.dagimportBbnfrompybbn.graph.edgeimportEdge,EdgeTypefrompybbn.graph.jointreeimportEvidenceBuilderfrompybbn.graph.nodeimportBbnNodefrompybbn.graph.variableimportVariablefrompybbn.pptc.inferencecontrollerimportInferenceController# create the nodesa=BbnNode(Variable(0,'a',['on','off']),[0.5,0.5])b=BbnNode(Variable(1,'b',['on','off']),[0.5,0.5,0.4,0.6])c=BbnNode(Variable(2,'c',['on','off']),[0.7,0.3,0.2,0.8])d=BbnNode(Variable(3,'d',['on','off']),[0.9,0.1,0.5,0.5])e=BbnNode(Variable(4,'e',['on','off']),[0.3,0.7,0.6,0.4])f=BbnNode(Variable(5,'f',['on','off']),[0.01,0.99,0.01,0.99,0.01,0.99,0.99,0.01])g=BbnNode(Variable(6,'g',['on','off']),[0.8,0.2,0.1,0.9])h=BbnNode(Variable(7,'h',['on','off']),[0.05,0.95,0.95,0.05,0.95,0.05,0.95,0.05])# create the network structurebbn=Bbn() \ .add_node(a) \ .add_node(b) \ .add_node(c) \ .add_node(d) \ .add_node(e) \ .add_node(f) \ .add_node(g) \ .add_node(h) \ .add_edge(Edge(a,b,EdgeType.DIRECTED)) \ .add_edge(Edge(a,c,EdgeType.DIRECTED)) \ .add_edge(Edge(b,d,EdgeType.DIRECTED)) \ .add_edge(Edge(c,e,EdgeType.DIRECTED)) \ .add_edge(Edge(d,f,EdgeType.DIRECTED)) \ .add_edge(Edge(e,f,EdgeType.DIRECTED)) \ .add_edge(Edge(c,g,EdgeType.DIRECTED)) \ .add_edge(Edge(e,h,EdgeType.DIRECTED)) \ .add_edge(Edge(g,h,EdgeType.DIRECTED))# convert the BBN to a join treejoin_tree=InferenceController.apply(bbn)# insert an observation evidenceev=EvidenceBuilder() \ .with_node(join_tree.get_bbn_node_by_name('a')) \ .with_evidence('on',1.0) \ .build()join_tree.set_observation(ev)# print the marginal probabilitiesfornodeinjoin_tree.get_bbn_nodes():potential=join_tree.get_bbn_potential(node)print(node)print(potential)
下面是创建线性高斯bbn并执行推理的示例。
importnumpyasnpfrompybbn.lg.graphimportDag,Parameters,Bbn# create the directed acylic graphdag=Dag()dag.add_node(0)dag.add_node(1)dag.add_edge(0,1)# create parametersmeans=np.array([0,25])cov=np.array([[1.09,1.95],[1.95,4.52]])params=Parameters(means,cov)# create the bayesian belief networkbbn=Bbn(dag,params)# do the inferenceM,C=bbn.do_inference()print(M)# set the evidence on node 0 to a value of 1bbn.set_evidence(0,1)M,C=bbn.do_inference()print(M)bbn.clear_evidences()# set the evidence on node 1 to a value of 20bbn.set_evidence(1,20)M,C=bbn.do_inference()print(M)bbn.clear_evidences()
建筑
要构建,您需要Python2.7或3.7。通过Anaconda管理环境 强烈建议您能够构建此项目(但如果您知道 你在做什么)。假设您已经安装了anaconda,您可以创建一个环境 接下来(请确保您cd进入此项目位置的根目录)。
对于Python2.7。
conda env create -f environment-py27.yml
conda activate pybbn27
python -m ipykernel install --user --name pybbn27 --display-name "pybbn27"
对于Python3.7。
conda env create -f environment-py37.yml
conda activate pybbn37
python -m ipykernel install --user --name pybbn37 --display-name "pybbn37"
然后您可以按如下方式构建项目。(注意,在Python3.6中,您将得到一些警告)。
make build
要生成文档,请转到docs子目录并键入以下内容。
make html
引文
@misc{vang_2017, title={PyBBN}, url={https://github.com/vangj/py-bbn/}, journal={GitHub}, author={Vang, Jee}, year={2017}, month={Jan}}
版权资料
Copyright 2017 Jee Vang Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.