Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add method to allow RMallet to restore a previous saved model #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/cc/mallet/topics/ParallelTopicModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ public void buildInitialTypeTopicCounts () {
* and create histograms for use in Dirichlet hyperparameter
* optimization.
*/
private void initializeHistograms() {
protected void initializeHistograms() {

int maxTokens = 0;
totalTokens = 0;
Expand Down
53 changes: 53 additions & 0 deletions src/cc/mallet/topics/RTopicModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import com.carrotsearch.hppc.IntIntHashMap;
import com.carrotsearch.hppc.cursors.IntCursor;
import java.io.*;
import java.util.ArrayList;

/** A wrapper for a topic model to be used from the R statistical package through rJava.
R does not distinguish between integers and floating point numbers, so many of these
Expand All @@ -17,6 +18,53 @@ public class RTopicModel extends ParallelTopicModel {
public RTopicModel(double numTopics, double alpha, double beta) {
super((int) Math.floor(numTopics), alpha, beta);
}

private RTopicModel(ParallelTopicModel model) {
super(model.getTopicAlphabet(), model.alphaSum, model.beta);
this.data = (ArrayList) model.data.clone();
this.alphabet = model.alphabet;
this.topicAlphabet = model.topicAlphabet;
this.numTopics = model.numTopics;
this.topicMask = model.topicMask;
this.topicBits = model.topicBits;
this.numTypes = model.numTypes;
this.totalTokens = model.totalTokens;
this.alpha = model.alpha;
this.alphaSum = model.alphaSum;
this.beta = model.beta;
this.betaSum = model.betaSum;
this.usingSymmetricAlpha = model.usingSymmetricAlpha;
this.typeTopicCounts = model.typeTopicCounts;
this.tokensPerTopic = model.tokensPerTopic;
this.docLengthCounts = model.docLengthCounts;
this.topicDocCounts = model.topicDocCounts;
this.numIterations = model.numIterations;
this.burninPeriod = model.burninPeriod;
this.saveSampleInterval = model.saveSampleInterval;
this.optimizeInterval = model.optimizeInterval;
this.temperingInterval = model.temperingInterval;
this.showTopicsInterval = model.showTopicsInterval;
this.wordsPerTopic = model.wordsPerTopic;
this.saveStateInterval = model.saveStateInterval;
this.stateFilename = model.stateFilename;
this.saveModelInterval = model.saveModelInterval;
this.modelFilename = model.modelFilename;
this.randomSeed = model.randomSeed;
this.formatter = model.formatter;
this.printLogLikelihood = model.printLogLikelihood;
this.typeTotals = model.typeTotals;
this.maxTypeCount = model.maxTypeCount;
this.numThreads = model.numThreads;

InstanceList instanceList = new InstanceList();
for (TopicAssignment topic : model.data) {
instanceList.add(topic.instance);
}

this.instances = instanceList;

this.initializeHistograms();
}

public void loadDocuments(String filename) {
instances = InstanceList.load(new File(filename));
Expand Down Expand Up @@ -117,4 +165,9 @@ public void writeState(String filename) {
System.err.println(e);
}
}

public static RTopicModel read (File f) throws Exception {
return new RTopicModel(ParallelTopicModel.read(f));
}

}