⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 index.html.svn-base

📁 Probabilistic graphical models in matlab.
💻 SVN-BASE
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"            "http://www.w3.org/TR/html4/strict.dtd"><html><head>	<meta name="resource-type" content="document">	<meta http-equiv="pragma" content="no-cache">	<meta name="revisit-after" content="10">	<meta name="classification" content="Jean-No毛l Rivasseau">	<meta name="description" content="This is the personal homepage of Jean-No毛l Rivasseau, listing professional and personal projects and interests.">	<meta name="keywords" content="jean-no毛l, jean-noel, rivasseau, jean, noel, no毛l, machine-learning, machine, learning, music, UBC, vancouver, computer, science, graduate, student, polytechnique, linux, powerpc, ppc, composition, research, russia, tchastouchki, mac, games, hiking, canada, france, role-playing, board, mac os x, university, british, columbia, open source, statistical inference, markov chains, mcmc, monte carlo, artificial intelligence, inference algorithms, belief propagation, bayesian networks, gibbs, gibbs sampler, markov random fields, mcmc, smc, particle filters, bayesian filtering">	<meta name="distribution" content="Global">	<meta name="rating" content="General">	<meta name="copyright" content="Copyright 2003-2007, Jean-No毛l Rivasseau. All rights reserved.">	<meta name="author" content="Jean-No毛l Rivasseau">	<meta name="language" content="English">	<meta name="doc-type" content="Web Page">	<meta name="doc-class" content="Living Document">	<meta name="doc-rights" content="Copywritten Work">	<title>PGM Tutorial</title><meta http-equiv="Content-Type" content="text/html; charset=utf-8"></head><body><h1>How to use the Probabilistic Graphical Models (PGM) program with Matlab</h1><hr><h1><a name="contents">Table of Contents</a></h1><a href="#installation">Installation</a> <br><ul><li> Download the package</li><li> Installing the Matlab M-files</li><li> Installing the loader</li><li> Installing the C++ executable</li></ul><a href="#basic">Basic usage information</a> <br> <br><a href="#pairwise">Creating Pairwise Graphs</a> <br><ul><li> Initial setup</li><li> Providing variable sizes</li><li> Providing potential tables</li><li> Additional operations</li></ul><a href="#factor">Creating Factor Graphs</a> <br><ul><li> Initial setup</li><li> Providing variable sizes</li><li> Providing potential tables</li><li> Additional operations</li></ul><a href="#common">Operations identical for Pairwise and Factor Graphs</a> <br><ul><li> Specifying observed variables</li><li> Specifying internal potentials (priors)</li><li> Using a timer</li></ul><a href="#inference">Computing Inference</a> <br><a href="#examples">Examples</a> <br><a href="#bugs">Bugs and Problems</a> <br><a href="#source">Source Code</a> <br><a href="#development">Developer Documentation</a> <br><hr><h1><a name="installation">Installation</a></h1><p>There are 3 things that need to be installed in order for the toolbox to work correctly:<ul><li> the Matlab M-files, </li><li> the loader, which is a C MEX file (so it has to be compiled in Matlab),</li><li> and, finally, the core program, which is written in C++ and that actually does the inference computations.</li></ul></p><h2>Downloading a PGM package</h2><p><a href="#installation">See below</a> to download a full PGM package.</p><h2>Installing the Matlab M-files</h2><p>The M-files are under the directory matlab_interface in the package.</a> Put all of these files in a directory that is in your Matlab path. These files have to be accessible to Matlab.</p><h2>Installing the loader</h2><p>In Matlab, go to the directory where you unpacked the previous files. Edit the file "loader.c" so that the line containing #include "/Applications/MATLAB704/extern/include/mex.h" points to the right file, which should be the location of the Matlab header file for MEX files. It is located in your root Matlab installation directory, then in extern/include/mex.h. So, if you have installed Matlab in the directory /usr/local/software/matlab-7.0.4/, for instance, the line should contain:</p><p style="font-weight: bold;">#include "/usr/local/software/matlab-7.0.4/extern/include/mex.h"</p><p style="color: red;">WARNING: The previous line is only an example!</p><p>Then type in Matlab "mex loader.c". If you included the correct header, it should compile without any error message. If it fails, you won't be able to use pgm directly with Matlab because it needs this loader file in order to launch the actual program.</p><p>Note: "/Applications/MATLAB704/extern/include/mex.h" should be the default location for Mac OS X with Matlab version 7.04.</p><p>Note (2): <span style="color: red;">I have now included some precompiled mex files of this loader,</span> so you can try to use them if you can't manage to compile it yourself. However, it may not work due to a mismatch in Matlab versions and/or platforms, so it is not recommended. The precompiled files for various platforms are in the directory "precompiled_loaders". You should pick the one corresponding to your platform and move it to a directory in your Matlab path.</p><h2>Installing the C++ executable</h2><p>This is probably the hardest step. You need to install the compiled version of pgm. Currently, it compiles on Mac OS X and Linux, with gcc 3.4 or later and the Boost libraries, version 1.33 or later. It should compile under Windows with MinGW (not tried yet), but it will almost certainly fail to build with Microsoft compilers as they are not compliant to the C++ standards. To build the package, follow the instructions of the README file.</p><p>I provided some binaries that I compiled here: they may be out of date, and are not available for all platforms. So the recommended way is still to compile pgm from source.</p><ul><li><a href="os_x/pgm.zip">Mac OS X (Tiger, PowerPC)</a></li><li><a href="linux/pgm-linux-x86_64-20070124.bz2">Linux (GCC 4.1, 64-bits)</a></li></ul><p>Once you have the pgm binary, you then need to install it into /usr/local/bin. It can be installed elsewhere, but you then need to manually edit the file compute_inference.m (It is one of the M-files) and change the line default_executable_name = '/usr/local/bin/pgm'; replace it with the location of the pgm binary, without forgetting the actual program name (the default is pgm, if you didn't rename it). If, for instance, you installed it into '~/bin/' the line should be:</p><p style="font-weight: bold;">default_executable_name = '~/bin/pgm';</p><hr><h1><a name="basic">Basic usage information</a></h1><p>This interface will allow you to create undirected networks with Matlab and compute inference on them. The results (random variables marginals) will be viewable in Matlab. However, keep in mind that this program was developped as a stand alone application. I did not have in mind at the earliest stage a Matlab interface, so it may not be very well suited to that purpose.</p><p>Currently pgm only works for discrete networks. To create a network and perform inference on it, you need to perform the following steps:<ul><li> Specify your network in term of a graphical model. You need to specify a graph: type of graph, number of vertices, and edges.</li><li> Enter the sizes of the variables, the potential tables (including variables priors), and specify which variables are observed.</li><li> Optionally, set some options (such as using a timer instead of computational steps).</li><li> Choose your <a href="#inference">inference method</a> (Exact BP, if it is a Tree; Loopy BP, Gibbs Sampling, RB MCMC tree Sampling, etc...).</li></ul></p><h2>Specifying the network</h2><p>The first and most important step is to specify the type of your graphical model. Currently there are two types available:</p><ul><li> <a href="#pairwise">Pairwise Markov Random Fields</a> (pairwise undirected graphs, with a potential on each edge): type <span style="font-weight: bold;">'pairwise'</span> as the graph type;</li><li> <a href="#factor">Factor Graphs:</a> type <span style="font-weight: bold;">'factor'</span> as the graph type.</li></ul><h2>Entering information about the graphical model</h2>This is accomplished differently for <a href="#pairwise">pairwise graphs</a> and <a href="#factor">factor graphs</a>, so refer to the appropriate section.<p style="color: red;">Note: it is extremely important that, for every command you will run on the created network, that you put the results of the command into the network variable. That is, for instance, do "my_network = set_variable_sizes(...) not just set_variable_sizes(...). This is because Matlab seems unable to take arguments passed by reference. Everything is passed by value.</p><hr><h1><a name="pairwise">Pairwise Graphs</a></h1><h2>Initial setup</h2><p>For a pairwise graph, the network should be specified via a Matlab adjacency matrix. An adjacency matrix is a 2D n*n matrix M that contains only 0 and 1. n will be the number of vertices in the graph; then, if M(i,j) = 1, there is an edge (link) between i and j, else there isn't.</p><p>Note that it is important for pgm that if M(i,j) = 1, M(j,i) = 0, so we can easily see that such a matrix will be triangular. This is an example of a simple matrix coding a 3 nodes network when 1 is linked to 2 and 2 to 3:</p><p>[ 0 1 0 ]<br>   [ 0 0 1 ]<br>   [ 0 0 0 ]</p><p>Once, we got our adjacency matrix, we can create the network by using the new_model command. The first argument to new_model is the type, and then the second argument is the adjacency matrix, so we type 'pairwise' as the first argument:</p><p style="font-weight: bold;">my_network = new_model('pairwise', M);</p>where M is the adjacency matrix in Matlab.</p><p>Note: there are several built-in options to create common graphs. Specify a model name instead of an adjacency matrix, and then a required number of arguments.</p><ul><li> Fully connected graph: type <span style="font-weight: bold;">'fully_connected'</span> followed by the number of vertices in the graph. This will create a graph with every possible edge between the vertices;</li><p style="font-weight: bold;">my_network = new_model('pairwise', 'fully_connected', 10 );</p><li> Square lattice Markov Random Field: <span style="font-weight: bold;">'mrf'</span> followed by the number of rows and columns;</li><p style="font-weight: bold;">my_network = new_model('pairwise', 'mrf', 5, 8 );</p><li> Random Graph: <span style="font-weight: bold;">'random'</span> followed by the number of vertices, and the graph density between 0 and 1 (the probabily that a given vertex is linked to another one);</li><p style="font-weight: bold;">my_network = new_model('pairwise', 'random', 80, 0.15 );</p></ul><h2>Providing variable sizes</h2><p>The next step is to specify the size of every variable in the network (since it is discrete). You can do that by using the command set_variables_sizes(network, M), which takes two arguments: the first is your newly created network, the second is an array of variable sizes. For example, if you have a 5 node network:</p><p style="font-weight: bold;">my_network = set_variables_sizes(my_network, [2 5 7 4 2] );</p><p>will specify that the first Random Variable can have 2 possible values, the second 5, etc.</p><p>If all your RVs have the same size, you can just use the set_all_variables_sizes() command. For example, the following line will specify that all your variables are binary:</p><p style="font-weight: bold;">my_network = set_all_variables_sizes(my_network, 2 );</p><p>You could achieve exactly the same result by entering "my_network = set_variables_sizes(my_network, ones [1, number_variables] * 2 )" (number_variables being the number of variables in the graph).</p><h2>Providing potential tables</h2><p>After that, you should specify the potential tables. In the pairwise representation, a potential is associated with an edge (the edge can be seem as the clique of size 2 containing the two random variables it links). Such a potential table is just a matrix of size n*m where n and m are the sizes of the two linked RVs. Note that every value in a potential table must be positive.</p><p>The command to enter the potential tables is set_potential(network, a, b, M). a and b are the indexes of the two variables of that particular edge, and M is the potential table proper. For example:</p><p style="font-weight: bold;">my_network = set_potential(my_network, 1, 2, [0.2 0.1; 0.7 0.9] );</p><p>would set the potential table of the edge linking variables 1 and 2 (which here, are binary) to the following table:</p><p>[ 0.2 0.1 ]<br>   [ 0.7 0.9 ]</p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -