<commit_before>/* Copyright (C) 2008-2010 University of Massachusetts Amherst,
   Department of Computer Science.
   This file is part of "FACTORIE" (Factor graphs, Imperative, Extensible)
   http://factorie.cs.umass.edu, http://code.google.com/p/factorie/
   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 cc.factorie.app.classify
import cc.factorie._
import cc.factorie.generative.Proportions
import cc.factorie.er._
import scala.collection.mutable.{HashMap,ArrayBuffer}

class MaxEntSampleRankTrainer extends ClassifierTrainer {
  var iterations = 10
  var learningRateDecay = 0.9
  def train[L<:LabelVariable[_]](il:LabelList[L])(implicit lm:Manifest[L]): Classifier[L] = {
    val cmodel = new LogLinearModel(il.labelToFeatures)
    val learner = new VariableSettingsSampler[L](cmodel, HammingLossObjective) with SampleRank with GradientAscentUpdates
    learner.learningRate = 1.0
    for (i <- 0 until iterations) {
      learner.processAll(il)
      learner.learningRate *= learningRateDecay
    }
    new Classifier[L] { val model = cmodel; val labelDomain = il.head.domain }
  }
}

class MaxEntLikelihoodTrainer extends ClassifierTrainer {
  def train[L<:LabelVariable[_]](il:LabelList[L])(implicit lm:Manifest[L]): Classifier[L] = {
    val cmodel = new LogLinearModel(il.labelToFeatures)
    val trainer = new LogLinearMaximumLikelihood(cmodel)
    // Do the training by BFGS
    trainer.processAll(il.map(List(_)))
    new Classifier[L] { val model = cmodel; val labelDomain = il.head.domain }
  }
}<commit_msg>Don't waste computation time normalizing and sampling when doing MaxEnt training<commit_after>/* Copyright (C) 2008-2010 University of Massachusetts Amherst,
   Department of Computer Science.
   This file is part of "FACTORIE" (Factor graphs, Imperative, Extensible)
   http://factorie.cs.umass.edu, http://code.google.com/p/factorie/
   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 cc.factorie.app.classify
import cc.factorie._
import cc.factorie.generative.Proportions
import cc.factorie.er._
import scala.collection.mutable.{HashMap,ArrayBuffer}

class MaxEntSampleRankTrainer extends ClassifierTrainer {
  var iterations = 10
  var learningRateDecay = 0.9
  def train[L<:LabelVariable[_]](il:LabelList[L])(implicit lm:Manifest[L]): Classifier[L] = {
    val cmodel = new LogLinearModel(il.labelToFeatures)
    val learner = new VariableSettingsSampler[L](cmodel, HammingLossObjective) with SampleRank with GradientAscentUpdates {
      override def pickProposal(proposals:Seq[Proposal]): Proposal = proposals.head
    }
    learner.learningRate = 1.0
    for (i <- 0 until iterations) {
      learner.processAll(il)
      learner.learningRate *= learningRateDecay
    }
    new Classifier[L] { val model = cmodel; val labelDomain = il.head.domain }
  }
}

class MaxEntLikelihoodTrainer extends ClassifierTrainer {
  def train[L<:LabelVariable[_]](il:LabelList[L])(implicit lm:Manifest[L]): Classifier[L] = {
    val cmodel = new LogLinearModel(il.labelToFeatures)
    val trainer = new LogLinearMaximumLikelihood(cmodel)
    // Do the training by BFGS
    trainer.processAll(il.map(List(_)))
    new Classifier[L] { val model = cmodel; val labelDomain = il.head.domain }
  }
}
{'fraction_non_alphanumeric': 0.09022022259057542, 'fraction_numerical': 0.008998342410608573, 'mean_word_length': 4.306532663316583, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4887485', 'n_tokens_mistral': 1281, 'n_tokens_neox': 1245, 'n_words': 389}