title: Labeled LDA inference in Stanford Topic Modeling Toolbox
body: <p>I am using Stanford Topic Modeling Toolbox v.0.3 for doing LabeledLDA.
  I was able to train a LabeledLDA model using the <a href="http://nlp.stanford.edu/software/tmt/tmt-0.3/" rel="nofollow">documentation</a> (<a href="http://nlp.stanford.edu/software/tmt/tmt-0.3/examples/example-6-llda-learn.scala" rel="nofollow">example-6-llda-learn.scala</a>) provided.
  How can I predict labels for a new dataset?</p>
  
  <p>I tried using code similar to <a href="http://nlp.stanford.edu/software/tmt/tmt-0.3/examples/example-3-lda-infer.scala" rel="nofollow">example-3-lda-infer.scala</a> for the inference on new dataset but it was not successful. Can anyone please help me with this issue?</p>
  
  <p><em>Edit</em>
  This is the code I use for inference but it is not working:</p>
  
  <pre><code>// tells Scala where to find the TMT classes
  import scalanlp.io._;
  import scalanlp.stage._;
  import scalanlp.stage.text._;
  import scalanlp.text.tokenize._;
  import scalanlp.pipes.Pipes.global._;
  
  import edu.stanford.nlp.tmt.stage._;
  import edu.stanford.nlp.tmt.model.lda._;
  import edu.stanford.nlp.tmt.model.llda._;
  
  // the path of the model to load
  val modelPath = file("llda-cvb0-2053ec3f-13-a69e079a-5cd58962");
  
  //Labeled LDA model
  
  println("Loading "+modelPath);
  val model = LoadCVB0LabeledLDA(modelPath);
  // Or, for a Gibbs model, use:
  // val model = LoadGibbsLDA(modelPath);
  
  // A new dataset for inference.  (Here we use the same dataset
  // that we trained against, but this file could be something new.)
  val source = CSVFile("test_lab_lda.csv") ~&gt; IDColumn(1);
  // Test File
  
  val text = {
    source ~&gt;                              // read from the source file
    Column(2) ~&gt;                           // select column containing text
    TokenizeWith(model.tokenizer.get)      // tokenize with existing model's tokenizer
  }
  
  // Base name of output files to generate
  val output = file(modelPath, source.meta[java.io.File].getName.replaceAll(".csv",""));
  
  // turn the text into a dataset ready to be used with LDA
  val dataset = LabeledLDADataset(text);
  
  val out_val=InferCVB0LabeledLDADocumentTopicDistributions(model, dataset)
  CSVFile(output+"-document-topic-distributuions.csv").write(out_val);
  </code></pre>
  
  <p>This code upon execution as <code>java -Xmx3g -jar tmt-0.3.3.jar infer_llda.scala</code> produces following error: </p>
  
  <pre><code>infer_llda.scala:40: error: overloaded method value apply with alternatives:
    (name: String,terms: Iterable[Iterable[String]],labels: Iterable[Iterable[String]],termIndex: Option[scalanlp.util.Index[String]],labelIndex: Option[scalanlp.util.Index[String]],tokenizer: Option[scalanlp.text.tokenize.Tokenizer])edu.stanford.nlp.tmt.model.llda.LabeledLDADataset[((Iterable[String], Iterable[String]), Int)] &lt;and&gt;
    [ID(in method apply)](text: scalanlp.stage.Parcel[scalanlp.collection.LazyIterable[scalanlp.stage.Item[ID(in method apply),Iterable[String]]]],labels: scalanlp.stage.Parcel[scalanlp.collection.LazyIterable[scalanlp.stage.Item[ID(in method apply),Iterable[String]]]],termIndex: Option[scalanlp.util.Index[String]],labelIndex: Option[scalanlp.util.Index[String]])edu.stanford.nlp.tmt.model.llda.LabeledLDADataset[(ID(in method apply), Iterable[String], Iterable[String])] &lt;and&gt;
    [ID(in method apply)](text: scalanlp.stage.Parcel[scalanlp.collection.LazyIterable[scalanlp.stage.Item[ID(in method apply),Iterable[String]]]],labels: scalanlp.stage.Parcel[scalanlp.collection.LazyIterable[scalanlp.stage.Item[ID(in method apply),Iterable[String]]]])edu.stanford.nlp.tmt.model.llda.LabeledLDADataset[(ID(in method apply), Iterable[String], Iterable[String])]
   cannot be applied to (scalanlp.stage.Parcel[scalanlp.collection.LazyIterable[scalanlp.stage.Item[String,Iterable[String]]]])
  val dataset = LabeledLDADataset(text);
                ^
  infer_llda.scala:43: error: could not find implicit value for evidence parameter of type scalanlp.serialization.TableWritable[scalanlp.collection.LazyIterable[(String, scalala.collection.sparse.SparseArray[Double])]]
  CSVFile(output+"-document-topic-distributuions.csv").write(out_val);
  </code></pre>
  
  <p>With help from @Skarab here is the solution to Labeled LDA learning and inference:</p>
  
  <ul>
  <li><a href="https://gist.github.com/1122784" rel="nofollow">Learning</a></li>
  <li><a href="https://gist.github.com/1122788" rel="nofollow">Inference</a></li>
  </ul>