public void calculateNCRP(TObjectDoubleHashMap<NCRPNode> nodeWeights, NCRPNode node, double weight) { for (NCRPNode child: node.children) { calculateNCRP(nodeWeights, child, weight + Math.log((double) child.customers / (node.customers + gamma))); } nodeWeights.put(node, weight + Math.log(gamma / (node.customers + gamma))); }
public void printNodes(boolean withWeight) { printNode(rootNode, 0, withWeight); }
public void estimate(int numIterations) { for (int iteration = 1; iteration <= numIterations; iteration++) { for (int doc=0; doc < numDocuments; doc++) { samplePath(doc, iteration); } for (int doc=0; doc < numDocuments; doc++) { sampleTopics(doc); } if (showProgress) { System.out.print("."); if (iteration % 50 == 0) { System.out.println(" " + iteration); } } if (iteration % displayTopicsInterval == 0) { printNodes(); } } }
HierarchicalLDA hlda = new HierarchicalLDA(); hlda.setAlpha(alpha.value()); hlda.setGamma(gamma.value()); hlda.setEta(eta.value()); hlda.setTopicDisplay(showTopicsInterval.value(), topWords.value()); hlda.setProgressDisplay(showProgress.value()); hlda.initialize(instances, testing, numLevels.value(), random); hlda.estimate(numIterations.value()); hlda.printState(new PrintWriter(stateFile.value())); double empiricalLikelihood = hlda.empiricalLikelihood(1000, testing); System.out.println("Empirical likelihood: " + empiricalLikelihood);
/** * This method is primarily for testing purposes. The {@link cc.mallet.topics.tui.HierarchicalLDATUI} * class has a more flexible interface for command-line use. */ public static void main (String[] args) { try { InstanceList instances = InstanceList.load(new File(args[0])); InstanceList testing = InstanceList.load(new File(args[1])); HierarchicalLDA sampler = new HierarchicalLDA(); sampler.initialize(instances, testing, 5, new Randoms()); sampler.estimate(250); } catch (Exception e) { e.printStackTrace(); } }
calculateNCRP(nodeWeights, rootNode, 0.0); calculateWordLikelihood(nodeWeights, rootNode, 0.0, typeCounts, newTopicWeights, 0, iteration);
/** * Writes the current sampling state to the file specified in <code>stateFile</code>. */ public void printState() throws IOException, FileNotFoundException { printState(new PrintWriter(new BufferedWriter(new FileWriter(stateFile)))); }
/** Propagate a topic weight to a node and all its children. weight is assumed to be a log. */ public void propagateTopicWeight(TObjectDoubleHashMap<NCRPNode> nodeWeights, NCRPNode node, double weight) { if (! nodeWeights.containsKey(node)) { // calculating the NCRP prior proceeds from the // root down (ie following child links), // but adding the word-topic weights comes from // the bottom up, following parent links and then // child links. It's possible that the leaf node may have // been removed just prior to this round, so the current // node may not have an NCRP weight. If so, it's not // going to be sampled anyway, so ditch it. return; } for (NCRPNode child: node.children) { propagateTopicWeight(nodeWeights, child, weight); } nodeWeights.adjustValue(node, weight); }
calculateWordLikelihood(nodeWeights, child, weight + nodeWeight, typeCounts, newTopicWeights, level + 1, iteration);
HierarchicalLDA hlda = new HierarchicalLDA(); hlda.setAlpha(alpha.value()); hlda.setGamma(gamma.value()); hlda.setEta(eta.value()); hlda.setTopicDisplay(showTopicsInterval.value(), topWords.value()); hlda.setProgressDisplay(showProgress.value()); hlda.initialize(instances, testing, numLevels.value(), random); hlda.estimate(numIterations.value()); hlda.printState(new PrintWriter(stateFile.value())); double empiricalLikelihood = hlda.empiricalLikelihood(1000, testing); System.out.println("Empirical likelihood: " + empiricalLikelihood);
/** * This method is primarily for testing purposes. The {@link cc.mallet.topics.tui.HierarchicalLDATUI} * class has a more flexible interface for command-line use. */ public static void main (String[] args) { try { InstanceList instances = InstanceList.load(new File(args[0])); InstanceList testing = InstanceList.load(new File(args[1])); HierarchicalLDA sampler = new HierarchicalLDA(); sampler.initialize(instances, testing, 5, new Randoms()); sampler.estimate(250); } catch (Exception e) { e.printStackTrace(); } }
calculateNCRP(nodeWeights, rootNode, 0.0); calculateWordLikelihood(nodeWeights, rootNode, 0.0, typeCounts, newTopicWeights, 0, iteration);
/** * Writes the current sampling state to the file specified in <code>stateFile</code>. */ public void printState() throws IOException, FileNotFoundException { printState(new PrintWriter(new BufferedWriter(new FileWriter(stateFile)))); }
/** Propagate a topic weight to a node and all its children. weight is assumed to be a log. */ public void propagateTopicWeight(TObjectDoubleHashMap<NCRPNode> nodeWeights, NCRPNode node, double weight) { if (! nodeWeights.containsKey(node)) { // calculating the NCRP prior proceeds from the // root down (ie following child links), // but adding the word-topic weights comes from // the bottom up, following parent links and then // child links. It's possible that the leaf node may have // been removed just prior to this round, so the current // node may not have an NCRP weight. If so, it's not // going to be sampled anyway, so ditch it. return; } for (NCRPNode child: node.children) { propagateTopicWeight(nodeWeights, child, weight); } nodeWeights.adjustValue(node, weight); }
calculateWordLikelihood(nodeWeights, child, weight + nodeWeight, typeCounts, newTopicWeights, level + 1, iteration);
HierarchicalLDA hlda = new HierarchicalLDA(); hlda.setAlpha(alpha.value()); hlda.setGamma(gamma.value()); hlda.setEta(eta.value()); hlda.setTopicDisplay(showTopicsInterval.value(), topWords.value()); hlda.setProgressDisplay(showProgress.value()); hlda.initialize(instances, testing, numLevels.value(), random); hlda.estimate(numIterations.value()); hlda.printState(new PrintWriter(stateFile.value())); double empiricalLikelihood = hlda.empiricalLikelihood(1000, testing); System.out.println("Empirical likelihood: " + empiricalLikelihood);
/** * This method is primarily for testing purposes. The {@link cc.mallet.topics.tui.HierarchicalLDATUI} * class has a more flexible interface for command-line use. */ public static void main (String[] args) { try { InstanceList instances = InstanceList.load(new File(args[0])); InstanceList testing = InstanceList.load(new File(args[1])); HierarchicalLDA sampler = new HierarchicalLDA(); sampler.initialize(instances, testing, 5, new Randoms()); sampler.estimate(250); } catch (Exception e) { e.printStackTrace(); } }
public void estimate(int numIterations) { for (int iteration = 1; iteration <= numIterations; iteration++) { for (int doc=0; doc < numDocuments; doc++) { samplePath(doc, iteration); } for (int doc=0; doc < numDocuments; doc++) { sampleTopics(doc); } if (showProgress) { System.out.print("."); if (iteration % 50 == 0) { System.out.println(" " + iteration); } } if (iteration % displayTopicsInterval == 0) { printNodes(); } } }
calculateNCRP(nodeWeights, rootNode, 0.0); calculateWordLikelihood(nodeWeights, rootNode, 0.0, typeCounts, newTopicWeights, 0, iteration);
public void printNodes(boolean withWeight) { printNode(rootNode, 0, withWeight); }