blob: 8d8f951f7c899232d1114269835ed903d1bbfa72 [file] [log] [blame]
<!-- HTML header for doxygen 1.8.4-->
<!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"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.4"/>
<meta name="keywords" content="madlib,postgres,greenplum,machine learning,data mining,deep learning,ensemble methods,data science,market basket analysis,affinity analysis,pca,lda,regression,elastic net,huber white,proportional hazards,k-means,latent dirichlet allocation,bayes,support vector machines,svm"/>
<title>MADlib: Latent Dirichlet Allocation</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
$(document).ready(initResizable);
$(window).load(resizeHeight);
</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 type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script><script src="../mathjax/MathJax.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="madlib_extra.css" rel="stylesheet" type="text/css"/>
<!-- google analytics -->
<script>
(function(i,s,o,g,r,a,m){i['GoogleAnalyticsObject']=r;i[r]=i[r]||function(){
(i[r].q=i[r].q||[]).push(arguments)},i[r].l=1*new Date();a=s.createElement(o),
m=s.getElementsByTagName(o)[0];a.async=1;a.src=g;m.parentNode.insertBefore(a,m)
})(window,document,'script','//www.google-analytics.com/analytics.js','ga');
ga('create', 'UA-45382226-1', 'auto');
ga('send', 'pageview');
</script>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<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">1.4.1</span>
</div>
<div id="projectbrief">User Documentation</div>
</td>
<td> <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>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.4 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('group__grp__lda.html','');});
</script>
<div id="doc-content">
<!-- 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><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Groups</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>
<div class="header">
<div class="headertitle">
<div class="title">Latent Dirichlet Allocation<div class="ingroups"><a class="el" href="group__grp__topic__modelling.html">Topic Modelling</a></div></div> </div>
</div><!--header-->
<div class="contents">
<div class="toc"><b>Contents</b> </p>
<ul>
<li>
<a href="#vocabulary">Vocabulary Format</a> </li>
<li>
<a href="#train">Training Function</a> </li>
<li>
<a href="#predict">Prediction Function</a> </li>
<li>
<a href="#examples">Examples</a> </li>
<li>
<a href="#notes">Notes</a> </li>
<li>
<a href="#literature">Literature</a> </li>
<li>
<a href="#related">Related Topics</a></li>
<li>
</li>
</ul>
</div><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>
<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>
<p><a class="anchor" id="vocabulary"></a></p>
<dl class="section user"><dt>Vocabulary Format</dt><dd></dd></dl>
<p>The vocabulary, or dictionary, indexes all the words found in the corpus and has the following format: </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="section user"><dt>Usage</dt><dd><ul>
<li><p class="startli">The training (i.e. topic inference) can be done with the following function: </p>
<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><p class="startli">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> BIGINT[][])
</pre><p class="startli">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></li>
<li><p class="startli">The prediction (i.e. labelling of test documents using a learned LDA model) can be done with the following function: </p>
<pre>
SELECT lda_predict(
<em>'data_table'</em>,
<em>'model_table'</em>,
<em>'output_table'</em>);
</pre><p class="startli">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></li>
<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>
</dd></dl>
<dl class="section user"><dt>Implementation Notes</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 a list of the number of occurrences of each unique word in the document. The conversion is done easily with the help of aggregation functions.</dd></dl>
<p><a class="anchor" id="train"></a></p>
<dl class="section user"><dt>Training Function</dt><dd>The LDA training function has the following syntax. <pre class="syntax">
lda_train( data_table,
model_table,
output_data_table,
voc_size,
topic_num,
iter_num,
alpha,
beta
)
</pre> <b>Arguments</b> <dl class="arglist">
<dt>data_table </dt>
<dd><p class="startdd">TEXT. The name of the table storing the training dataset. Each row is in the form <code>&lt;docid, wordid, count&gt;</code> where <code>docid</code>, <code>wordid</code>, and <code>count</code> are non-negative integers.</p>
<p class="enddd">The <code>docid</code> column refers to the document ID, the <code>wordid</code> column is the word ID (the index of a word in the vocabulary), and <code>count</code> is the number of occurrences of the word in the document. </p>
</dd>
<dt>model_table </dt>
<dd>TEXT. The name of the table storing the learned models. This table has one row and the following columns. <table class="output">
<tr>
<th>voc_size </th><td>INTEGER. Size of the vocabulary. Note that the <code>wordid</code> should be continous integers starting from 0 to <code>voc_size</code> &minus; <code>1</code>. A data validation routine is called to validate the dataset. </td></tr>
<tr>
<th>topic_num </th><td>INTEGER. Number of topics. </td></tr>
<tr>
<th>alpha </th><td>DOUBLE PRECISION. Dirichlet parameter for the per-doc topic multinomial (e.g. 50/topic_num). </td></tr>
<tr>
<th>beta </th><td>DOUBLE PRECISION. Dirichlet parameter for the per-topic word multinomial (e.g. 0.01). </td></tr>
<tr>
<th>model </th><td>INTEGER[][]. </td></tr>
</table>
</dd>
<dt>output_data_table </dt>
<dd>TEXT. The name of the table to store the output data. It has the following columns: <table class="output">
<tr>
<th>docid </th><td>INTEGER. </td></tr>
<tr>
<th>wordcount </th><td>INTEGER. </td></tr>
<tr>
<th>words </th><td>INTEGER[]. </td></tr>
<tr>
<th>counts </th><td>INTEGER[]. </td></tr>
<tr>
<th>topic_count </th><td>INTEGER[]. </td></tr>
<tr>
<th>topic_assignment </th><td>INTEGER[]. </td></tr>
</table>
</dd>
<dt>iter_num </dt>
<dd>INTEGER. Number of iterations (e.g. 60). </dd>
<dt>alpha </dt>
<dd>DOUBLE PRECISION. Dirichlet parameter for the per-doc topic multinomial (e.g. 50/topic_num). </dd>
<dt>beta </dt>
<dd>DOUBLE PRECISION. Dirichlet parameter for the per-topic word multinomial (e.g. 0.01). </dd>
</dl>
</dd></dl>
<p><a class="anchor" id="predict"></a></p>
<dl class="section user"><dt>Prediction Function</dt><dd></dd></dl>
<p>Prediction&mdash;labelling test documents using a learned LDA model&mdash;is accomplished with the following function: </p>
<pre class="syntax">
lda_predict( data_table,
model_table,
output_table
);
</pre><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 document in the dataset. The table has the following columns: </p>
<table class="output">
<tr>
<th>docid </th><td>INTEGER. </td></tr>
<tr>
<th>wordcount </th><td>INTEGER. </td></tr>
<tr>
<th>words </th><td>INTEGER. </td></tr>
<tr>
<th>counts </th><td>INTEGER. </td></tr>
<tr>
<th>topic_count </th><td>INTEGER[]. </td></tr>
<tr>
<th>topic_assignment </th><td>INTEGER[]. </td></tr>
</table>
<p><a class="anchor" id="perplexity"></a></p>
<dl class="section user"><dt>Perplexity Function</dt><dd>This module provides a function for computing the perplexity. <pre class="syntax">
lda_get_perplexity( model_table,
output_data_table
);
</pre></dd></dl>
<p><a class="anchor" id="examples"></a></p>
<dl class="section user"><dt>Examples</dt><dd><ol type="1">
<li>Prepare a vocabulary and training dataset in the appropriate structures. <pre class="example">
-- vocabulary table
CREATE TABLE my_vocab(wordid INT4, word TEXT)
INSERT INTO my_vocab VALUES
(0, 'code'), (1, 'data'), (2, 'graph'), (3, 'image'),
(4, 'input'), (5, 'layer'), (6, 'learner'), (7, 'loss'),
(8, 'model'), (9, 'network'), (10, 'neuron'), (11, 'object'),
(12, 'output'), (13, 'rate'), (14, 'set'), (15, 'signal'),
(16, 'sparse'), (17, 'spatial'), (18, 'system'), (19, 'training');
-- training data table
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);
-- testing data
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></li>
<li>Run the lda_train() function. <pre class="example">
SELECT madlib.lda_train( 'my_training',
'my_model',
'my_outdata',
20,
5,
10,
5,
0.01
);
</pre> A successful run of the lda_train() function generates two tables, one for storing the learned models, the other for storing the output data table.</li>
<li>To get the detailed information about the learned model, run these commands: <pre class="example">
-- The topic description by top-k words
SELECT * FROM madlib.lda_get_topic_desc( 'my_model',
'my_vocab',
'my_topic_desc',
15
);
-- The per-topic word counts
SELECT madlib.lda_get_topic_word_count( 'my_model',
'my_topic_word_count'
);
-- The per-word topic counts
SELECT madlib.lda_get_word_topic_count( 'my_model',
'my_word_topic_count'
);
</pre></li>
<li>To get the topic counts and the topic assignments for each doucment, run the following commands: <pre class="example">
-- The per-document topic counts:
SELECT docid, topic_count
FROM my_outdata;
-- The per-document topic assignments:
SELECT docid, words, counts, topic_assignment
FROM my_outdata;
</pre> Get the topic assignment for each word in a document by scanning <code>words</code>, <code>counts</code>, and <code>topic_assignment</code> together.</li>
<li>To use a learned LDA model for prediction (that is, to label new documents), use the following command: <pre class="example">
SELECT madlib.lda_predict( 'my_testing',
'my_model',
'my_pred'
);
</pre> After a successful run of the lda_predict() function, the prediction results are generated and stored in <em>my_pred</em>. This table has the same schema as the <em>my_outdata</em> table generated by the lda_train() function.</li>
<li>To get the topic counts and topic assignments for each document, run the following commands: <pre class="example">
-- The per-document topic counts:
SELECT docid, topic_count
FROM my_pred;
-- The per-document topic assignments:
SELECT docid, words, counts, topic_assignment
FROM my_pred;
</pre> Get the topic assignment for each word in a document by scanning <code>words</code>, <code>counts</code>, and <code>topic_assignment</code> together.</li>
<li>Use the following command to compute the perplexity. <pre class="example">
SELECT madlib.lda_get_perplexity( 'my_model',
'my_pred'
);
</pre></li>
</ol>
</dd></dl>
<p><a class="anchor" id="literature"></a></p>
<dl class="section user"><dt>Literature</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>
<p><a class="anchor" id="related"></a></p>
<dl class="section user"><dt>Related Topics</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><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="footer">Generated on Thu Jan 9 2014 20:25:07 for MADlib by
<a href="http://www.doxygen.org/index.html">
<img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.8.4 </li>
</ul>
</div>
</body>
</html>