blob: 0d47fa241e350ef0a1a5701e1fb4996a1a088bee [file] [log] [blame]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>MADlib: Latent Dirichlet Allocation</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/search.js"></script>
<script type="text/javascript">
$(document).ready(function() { searchBox.OnSelectItem(0); });
</script>
<script src="../mathjax/MathJax.js">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
</head>
<body>
<div id="top"><!-- do not remove this div! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td style="padding-left: 0.5em;">
<div id="projectname">MADlib
&#160;<span id="projectnumber">0.7</span> <span style="font-size:10pt; font-style:italic"><a href="../latest/./group__grp__lda.html"> A newer version is available</a></span>
</div>
<div id="projectbrief">User Documentation</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- Generated by Doxygen 1.7.5.1 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="dynsections.js"></script>
<div id="navrow1" class="tabs">
<ul class="tablist">
<li><a href="index.html"><span>Main&#160;Page</span></a></li>
<li><a href="modules.html"><span>Modules</span></a></li>
<li><a href="files.html"><span>Files</span></a></li>
<li>
<div id="MSearchBox" class="MSearchBoxInactive">
<span class="left">
<img id="MSearchSelect" src="search/mag_sel.png"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
alt=""/>
<input type="text" id="MSearchField" value="Search" accesskey="S"
onfocus="searchBox.OnSearchFieldFocus(true)"
onblur="searchBox.OnSearchFieldFocus(false)"
onkeyup="searchBox.OnSearchFieldChange(event)"/>
</span><span class="right">
<a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
</span>
</div>
</li>
</ul>
</div>
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
initNavTree('group__grp__lda.html','');
</script>
<div id="doc-content">
<div class="header">
<div class="headertitle">
<div class="title">Latent Dirichlet Allocation</div> </div>
<div class="ingroups"><a class="el" href="group__grp__unsuplearn.html">Unsupervised Learning</a></div></div>
<div class="contents">
<div id="dynsection-0" onclick="return toggleVisibility(this)" class="dynheader closed" style="cursor:pointer;">
<img id="dynsection-0-trigger" src="closed.png" alt="+"/> Collaboration diagram for Latent Dirichlet Allocation:</div>
<div id="dynsection-0-summary" class="dynsummary" style="display:block;">
</div>
<div id="dynsection-0-content" class="dyncontent" style="display:none;">
<center><table><tr><td><div class="center"><iframe scrolling="no" frameborder="0" src="group__grp__lda.svg" width="411" height="40"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe></div>
</td></tr></table></center>
</div>
<dl class="user"><dt><b>About:</b></dt><dd></dd></dl>
<p>Latent Dirichlet Allocation (LDA) is an interesting generative probabilistic model for natural texts and has received a lot of attention in recent years. The model is quite versatile, having found uses in problems like automated topic discovery, collaborative filtering, and document classification.</p>
<p>The LDA model posits that each document is associated with a mixture of various topics (e.g. a document is related to Topic 1 with probability 0.7, and Topic 2 with probability 0.3), and that each word in the document is attributable to one of the document's topics. There is a (symmetric) Dirichlet prior with parameter \( \alpha \) on each document's topic mixture. In addition, there is another (symmetric) Dirichlet prior with parameter \( \beta \) on the distribution of words for each topic.</p>
<p>The following generative process then defines a distribution over a corpus of documents.</p>
<ul>
<li>Sample for each topic \( i \), a per-topic word distribution \( \phi_i \) from the Dirichlet( \(\beta\)) prior.</li>
</ul>
<ul>
<li>For each document:<ul>
<li>Sample a document length N from a suitable distribution, say, Poisson.</li>
<li>Sample a topic mixture \( \theta \) for the document from the Dirichlet( \(\alpha\)) distribution.</li>
<li>For each of the N words:<ul>
<li>Sample a topic \( z_n \) from the multinomial topic distribution \( \theta \).</li>
<li>Sample a word \( w_n \) from the multinomial word distribution \( \phi_{z_n} \) associated with topic \( z_n \).</li>
</ul>
</li>
</ul>
</li>
</ul>
<p>In practice, only the words in each document are observable. The topic mixture of each document and the topic for each word in each document are latent unobservable variables that need to be inferred from the observables, and this is the problem people refer to when they talk about the inference problem for LDA. Exact inference is intractable, but several approximate inference algorithms for LDA have been developed. The simple and effective Gibbs sampling algorithm described in Griffiths and Steyvers [2] appears to be the current algorithm of choice.</p>
<p>This implementation provides a parallel and scalable in-database solution for LDA based on Gibbs sampling. Different with the implementations based on MPI or Hadoop Map/Reduce, this implementation builds upon the shared-nothing MPP databases and enables high-performance in-database analytics.</p>
<dl class="user"><dt><b>Input:</b></dt><dd>The <b>corpus/dataset</b> to be analyzed is expected to be of the following form: <pre>{TABLE|VIEW} <em>data_table</em> (
<em>docid</em> INTEGER,
<em>wordid</em> INTEGER,
<em>count</em> INTEGER
)</pre> where <code>docid</code> refers to the document ID, <code>wordid</code> is the word ID (the index of a word in the vocabulary), and <code>count</code> is the number of occurence of the word in the document.</dd></dl>
<p>The <b>vocabulary/dictionary</b> that indexes all the words found in the corpus is of the following form: </p>
<pre>{TABLE|VIEW} <em>vocab_table</em> (
<em>wordid</em> INTEGER,
<em>word</em> TEXT,
)</pre><p> where <code>wordid</code> refers the word ID (the index of a word in the vocabulary) and <code>word</code> is the actual word.</p>
<dl class="user"><dt><b>Usage:</b></dt><dd><ul>
<li>The training (i.e. topic inference) can be done with the following function: <pre>
SELECT lda_train(
<em>'data_table'</em>,
<em>'model_table'</em>,
<em>'output_data_table'</em>,
<em>voc_size</em>,
<em>topic_num</em>,
<em>iter_num</em>,
<em>alpha</em>,
<em>beta</em>)
</pre></li>
</ul>
</dd></dl>
<p>This function stores the resulting model in <code><em>model_table</em></code>. The table has only 1 row and is in the following form: </p>
<pre>{TABLE} <em>model_table</em> (
<em>voc_size</em> INTEGER,
<em>topic_num</em> INTEGER,
<em>alpha</em> FLOAT,
<em>beta</em> FLOAT,
<em>model</em> INTEGER[][])
</pre><p>This function also stores the topic counts and the topic assignments in each document in <code><em>output_data_table</em></code>. The table is in the following form: </p>
<pre>{TABLE} <em>output_data_table</em> (
<em>docid</em> INTEGER,
<em>wordcount</em> INTEGER,
<em>words</em> INTEGER[],
<em>counts</em> INTEGER[],
<em>topic_count</em> INTEGER[],
<em>topic_assignment</em> INTEGER[])
</pre><ul>
<li>The prediction (i.e. labelling of test documents using a learned LDA model) can be done with the following function: <pre>
SELECT lda_predict(
<em>'data_table'</em>,
<em>'model_table'</em>,
<em>'output_table'</em>);
</pre></li>
</ul>
<p>This function stores the prediction results in <code><em>output_table</em></code>. Each row in the table stores the topic distribution and the topic assignments for a docuemnt in the dataset. And the table is in the following form: </p>
<pre>{TABLE} <em>output_table</em> (
<em>docid</em> INTEGER,
<em>wordcount</em> INTEGER,
<em>words</em> INTEGER,
<em>counts</em> INTEGER,
<em>topic_count</em> INTEGER[],
<em>topic_assignment</em> INTEGER[])
</pre><ul>
<li>This module also provides a function for computing the perplexity: <pre>
SELECT lda_get_perplexity(
<em>'model_table'</em>,
<em>'output_data_table'</em>);
</pre></li>
</ul>
<dl class="user"><dt><b>Implementation Notes:</b></dt><dd>The input format for this module is very common in many machine learning packages written in various lanugages, which allows users to generate datasets using any existing document preprocessing tools or import existing dataset very conveniently. Internally, the input data will be validated and then converted to the following format for efficiency: <pre>{TABLE} <em>__internal_data_table__</em> (
<em>docid</em> INTEGER,
<em>wordcount</em> INTEGER,
<em>words</em> INTEGER[],
<em>counts</em> INTEGER[])
</pre> where <code>docid</code> is the document ID, <code>wordcount</code> is the count of words in the document, <code>words</code> is the list of unique words in the document, and <code>counts</code> is the list of number of occurence of each unique word in the document. The convertion can be done with the help of aggregation functions very easily.</dd></dl>
<dl class="user"><dt><b>Examples:</b></dt><dd></dd></dl>
<p>We now give a usage example.</p>
<ul>
<li>As a first step, we need to prepare a dataset and vocabulary in the appropriate structure. <div class="fragment"><pre class="fragment"> CREATE TABLE my_vocab(wordid INT4, word TEXT)
;
INSERT INTO my_vocab VALUES
(0, <span class="stringliteral">&#39;code&#39;</span>), (1, <span class="stringliteral">&#39;data&#39;</span>), (2, <span class="stringliteral">&#39;graph&#39;</span>), (3, <span class="stringliteral">&#39;image&#39;</span>), (4, <span class="stringliteral">&#39;input&#39;</span>), (5,
<span class="stringliteral">&#39;layer&#39;</span>), (6, <span class="stringliteral">&#39;learner&#39;</span>), (7, <span class="stringliteral">&#39;loss&#39;</span>), (8, <span class="stringliteral">&#39;model&#39;</span>), (9, <span class="stringliteral">&#39;network&#39;</span>), (10,
<span class="stringliteral">&#39;neuron&#39;</span>), (11, <span class="stringliteral">&#39;object&#39;</span>), (12, <span class="stringliteral">&#39;output&#39;</span>), (13, <span class="stringliteral">&#39;rate&#39;</span>), (14, <span class="stringliteral">&#39;set&#39;</span>), (15,
<span class="stringliteral">&#39;signal&#39;</span>), (16, <span class="stringliteral">&#39;sparse&#39;</span>), (17, <span class="stringliteral">&#39;spatial&#39;</span>), (18, <span class="stringliteral">&#39;system&#39;</span>), (19, <span class="stringliteral">&#39;training&#39;</span>);
CREATE TABLE my_training
(
docid INT4,
wordid INT4,
count INT4
)
;
INSERT INTO my_training VALUES
(0, 0, 2),(0, 3, 2),(0, 5, 1),(0, 7, 1),(0, 8, 1),(0, 9, 1),(0, 11, 1),(0, 13,
1), (1, 0, 1),(1, 3, 1),(1, 4, 1),(1, 5, 1),(1, 6, 1),(1, 7, 1),(1, 10, 1),(1,
14, 1),(1, 17, 1),(1, 18, 1), (2, 4, 2),(2, 5, 1),(2, 6, 2),(2, 12, 1),(2, 13,
1),(2, 15, 1),(2, 18, 2), (3, 0, 1),(3, 1, 2),(3, 12, 3),(3, 16, 1),(3, 17,
2),(3, 19, 1), (4, 1, 1),(4, 2, 1),(4, 3, 1),(4, 5, 1),(4, 6, 1),(4, 10, 1),(4,
11, 1),(4, 14, 1),(4, 18, 1),(4, 19, 1), (5, 0, 1),(5, 2, 1),(5, 5, 1),(5, 7,
1),(5, 10, 1),(5, 12, 1),(5, 16, 1),(5, 18, 1),(5, 19, 2), (6, 1, 1),(6, 3,
1),(6, 12, 2),(6, 13, 1),(6, 14, 2),(6, 15, 1),(6, 16, 1),(6, 17, 1), (7, 0,
1),(7, 2, 1),(7, 4, 1),(7, 5, 1),(7, 7, 2),(7, 8, 1),(7, 11, 1),(7, 14, 1),(7,
16, 1), (8, 2, 1),(8, 4, 4),(8, 6, 2),(8, 11, 1),(8, 15, 1),(8, 18, 1),
(9, 0, 1),(9, 1, 1),(9, 4, 1),(9, 9, 2),(9, 12, 2),(9, 15, 1),(9, 18, 1),(9,
19, 1);
CREATE TABLE my_testing
(
docid INT4,
wordid INT4,
count INT4
)
;
INSERT INTO my_testing VALUES
(0, 0, 2),(0, 8, 1),(0, 9, 1),(0, 10, 1),(0, 12, 1),(0, 15, 2),(0, 18, 1),(0,
19, 1), (1, 0, 1),(1, 2, 1),(1, 5, 1),(1, 7, 1),(1, 12, 2),(1, 13, 1),(1, 16,
1),(1, 17, 1),(1, 18, 1), (2, 0, 1),(2, 1, 1),(2, 2, 1),(2, 3, 1),(2, 4, 1),(2,
5, 1),(2, 6, 1),(2, 12, 1),(2, 14, 1),(2, 18, 1), (3, 2, 2),(3, 6, 2),(3, 7,
1),(3, 9, 1),(3, 11, 2),(3, 14, 1),(3, 15, 1), (4, 1, 1),(4, 2, 2),(4, 3,
1),(4, 5, 2),(4, 6, 1),(4, 11, 1),(4, 18, 2);
</pre></div></li>
</ul>
<ul>
<li>To perform training, we call the lda_train() function with the appropriate parameters. Here is an example. <div class="fragment"><pre class="fragment"> SELECT MADLib.lda_train(
<span class="stringliteral">&#39;my_training&#39;</span>, <span class="stringliteral">&#39;my_model&#39;</span>, <span class="stringliteral">&#39;my_outdata&#39;</span>, 20, 5, 10, 5, 0.01);
</pre></div></li>
</ul>
<p>After a successful run of the lda_train() function, two tables will be generated, one for storing the learned models, and another for storing the output data table.</p>
<p>To get the detailed information about the learned model, we can run the following commands:</p>
<ul>
<li>The topic description by top-k words <div class="fragment"><pre class="fragment"> SELECT * FROM MADLib.lda_get_topic_desc(
<span class="stringliteral">&#39;my_model&#39;</span>, <span class="stringliteral">&#39;my_vocab&#39;</span>, <span class="stringliteral">&#39;my_topic_desc&#39;</span>, 15);
</pre></div></li>
</ul>
<ul>
<li>The per-topic word counts <div class="fragment"><pre class="fragment"> SELECT MADLib.lda_get_topic_word_count(
<span class="stringliteral">&#39;my_model&#39;</span>, <span class="stringliteral">&#39;my_topic_word_count&#39;</span>);
</pre></div></li>
</ul>
<ul>
<li>The per-word topic counts <div class="fragment"><pre class="fragment"> SELECT MADLib.lda_get_word_topic_count(
<span class="stringliteral">&#39;my_model&#39;</span>, <span class="stringliteral">&#39;my_word_topic_count&#39;</span>);
</pre></div></li>
</ul>
<p>To get the topic counts and the topic assignments for each doucment, we can run the following commands:</p>
<ul>
<li>The per-document topic counts: <div class="fragment"><pre class="fragment"> SELECT
docid, topic_count
FROM my_outdata;
</pre></div></li>
</ul>
<ul>
<li>The per-document topic assignments: <div class="fragment"><pre class="fragment"> SELECT
docid, words, counts, topic_assignment
FROM my_outdata;
</pre></div> By scanning <code>words</code>, <code>counts</code>, and <code>topic_assignment</code> together, we can get the topic assignment for each word in a document.</li>
</ul>
<ul>
<li>To use a learned LDA model for prediction (i.e. to label new documents), we can use the following command: <div class="fragment"><pre class="fragment"> SELECT MADLib.lda_predict(
<span class="stringliteral">&#39;my_testing&#39;</span>, <span class="stringliteral">&#39;my_model&#39;</span>, <span class="stringliteral">&#39;my_pred&#39;</span>);
</pre></div></li>
</ul>
<p>After a successful run of the lda_predict() function, the prediction results will be generated and stored in <em>my_pred</em>. This table has the same schema as the <em>my_outdata</em> generated by the lda_train() function.</p>
<p>To get te the topic counts and the topic assignments for each doucment, we can run the following commands:</p>
<ul>
<li>The per-document topic counts: <div class="fragment"><pre class="fragment"> SELECT
docid, topic_count
FROM my_pred;
</pre></div></li>
</ul>
<ul>
<li>The per-document topic assignments: <div class="fragment"><pre class="fragment"> SELECT
docid, words, counts, topic_assignment
FROM my_pred;
</pre></div> By scanning <code>words</code>, <code>counts</code>, and <code>topic_assignment</code> together, we can get the topic assignment for each word in a document.</li>
</ul>
<ul>
<li>To compute the perplexity, we can use the following command: <div class="fragment"><pre class="fragment"> SELECT MADLib.lda_get_perplexity(
<span class="stringliteral">&#39;my_model&#39;</span>, <span class="stringliteral">&#39;my_pred&#39;</span>);
</pre></div></li>
</ul>
<dl class="user"><dt><b>Literature:</b></dt><dd></dd></dl>
<p>[1] D.M. Blei, A.Y. Ng, M.I. Jordan, <em>Latent Dirichlet Allocation</em>, Journal of Machine Learning Research, vol. 3, pp. 993-1022, 2003.</p>
<p>[2] T. Griffiths and M. Steyvers, <em>Finding scientific topics</em>, PNAS, vol. 101, pp. 5228-5235, 2004.</p>
<p>[3] Y. Wang, H. Bai, M. Stanton, W-Y. Chen, and E.Y. Chang, <em>lda: Parallel Dirichlet Allocation for Large-scale Applications</em>, AAIM, 2009.</p>
<p>[4] <a href="http://en.wikipedia.org/wiki/Latent_Dirichlet_allocation">http://en.wikipedia.org/wiki/Latent_Dirichlet_allocation</a></p>
<p>[5] J. Chang, Collapsed Gibbs sampling methods for topic models, R manual, 2010.</p>
<dl class="see"><dt><b>See also:</b></dt><dd>File <a class="el" href="lda_8sql__in.html" title="SQL functions for Latent Dirichlet Allocation.">lda.sql_in</a> documenting the SQL functions. </dd></dl>
</div>
</div>
<div id="nav-path" class="navpath">
<ul>
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Functions</a></div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<li class="footer">Generated on Fri May 10 2013 01:37:13 for MADlib by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
</ul>
</div>
</body>
</html>