[Home] DMelt member access. Java source code: com.datumbox.framework.core.machinelearning.topicmodeling.LatentDirichletAllocation
/**
* Copyright (C) 2013-2017 Vasilis Vryniotis
*
* 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.
*/
package com.datumbox.framework.core.machinelearning.topicmodeling;
import com.datumbox.framework.common.Configuration;
import com.datumbox.framework.common.dataobjects.*;
import com.datumbox.framework.common.storage.interfaces.BigMap;
import com.datumbox.framework.common.storage.interfaces.StorageEngine;
import com.datumbox.framework.common.storage.interfaces.StorageEngine.MapType;
import com.datumbox.framework.common.storage.interfaces.StorageEngine.StorageHint;
import com.datumbox.framework.core.common.utilities.MapMethods;
import com.datumbox.framework.core.common.utilities.PHPMethods;
import com.datumbox.framework.core.common.dataobjects.Dataframe;
import com.datumbox.framework.core.common.dataobjects.Record;
import com.datumbox.framework.core.machinelearning.common.abstracts.AbstractTrainer;
import com.datumbox.framework.core.machinelearning.common.abstracts.modelers.AbstractTopicModeler;
import com.datumbox.framework.core.statistics.descriptivestatistics.Descriptives;
import com.datumbox.framework.core.statistics.sampling.SimpleRandomSampling;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
/**
* Implementation of the Latent Dirichlet Allocation algorithm.
*
* References:
* http://videolectures.net/mlss09uk_blei_tm/
* http://www.ncbi.nlm.nih.gov/pmc/articles/PMC387300/
* https://github.com/angeloskath/php-nlp-tools/blob/master/src/NlpTools/Models/Lda.php
* https://gist.github.com/mblondel/542786
* http://stats.stackexchange.com/questions/9315/topic-prediction-using-latent-dirichlet-allocation
* http://home.uchicago.edu/~lkorsos/GibbsNGramLDA.pdf
* http://machinelearning.wustl.edu/mlpapers/paper_files/BleiNJ03.pdf
* http://www.cl.cam.ac.uk/teaching/1213/L101/clark_lectures/lect7.pdf
* http://www.ics.uci.edu/~newman/pubs/fastlda.pdf
* http://www.tnkcs.inf.elte.hu/vedes/Biro_Istvan_Tezisek_en.pdf (Limit Gibbs Sampler & unseen inference)
* http://airweb.cse.lehigh.edu/2008/submissions/biro_2008_latent_dirichlet_allocation_spam.pdf (unseen inference)
* http://www.cs.cmu.edu/~akyrola/10702project/kyrola10702FINAL.pdf
* http://stats.stackexchange.com/questions/18167/how-to-calculate-perplexity-of-a-holdout-with-latent-dirichlet-allocation
* http://www.slideserve.com/adamdaniel/an-introduction-to-latent-dirichlet-allocation-lda
*
* @author Vasilis Vryniotis
*/
public class LatentDirichletAllocation extends AbstractTopicModeler {
/** {@inheritDoc} */
public static class ModelParameters extends AbstractTopicModeler.AbstractModelParameters {
private static final long serialVersionUID = 1L;
//number of features in data points used for training
private Integer d = 0;
private int totalIterations;
@BigMap(keyClass=List.class, valueClass=Integer.class, mapType=MapType.HASHMAP, storageHint=StorageHint.IN_CACHE, concurrent=false)
private Map, Integer> topicAssignmentOfDocumentWord; //the Z in the graphical model
@BigMap(keyClass=List.class, valueClass=Integer.class, mapType=MapType.HASHMAP, storageHint=StorageHint.IN_MEMORY, concurrent=false)
private Map, Integer> documentTopicCounts; //the nj(d) in the papers
@BigMap(keyClass=List.class, valueClass=Integer.class, mapType=MapType.HASHMAP, storageHint=StorageHint.IN_CACHE, concurrent=false)
private Map, Integer> topicWordCounts; //the nj(w) in the papers
@BigMap(keyClass=Integer.class, valueClass=Integer.class, mapType=MapType.HASHMAP, storageHint=StorageHint.IN_MEMORY, concurrent=false)
private Map documentWordCounts; //the n.(d) in the papers
@BigMap(keyClass=Integer.class, valueClass=Integer.class, mapType=MapType.HASHMAP, storageHint=StorageHint.IN_MEMORY, concurrent=false)
private Map topicCounts; //the nj(.) in the papers
/**
* @param storageEngine
* @see AbstractTrainer.AbstractModelParameters#AbstractModelParameters(StorageEngine)
*/
protected ModelParameters(StorageEngine storageEngine) {
super(storageEngine);
}
/**
* Getter for the dimension of the dataset used in training.
*
* @return
*/
public Integer getD() {
return d;
}
/**
* Setter for the dimension of the dataset used in training.
*
* @param d
*/
protected void setD(Integer d) {
this.d = d;
}
/**
* Getter for the total number of iterations performed during training.
*
* @return
*/
public int getTotalIterations() {
return totalIterations;
}
/**
* Setter for the total number of iterations performed during training.
*
* @param totalIterations
*/
protected void setTotalIterations(int totalIterations) {
this.totalIterations = totalIterations;
}
/**
* Getter for the Topic Assignments of the words of the document.
* It returns the topic assignments of a particular word in a particular document.
* It is a key-value of {@literal [,