cc.mallet.fst
Class ThreadedOptimizable

java.lang.Object
  extended by cc.mallet.fst.ThreadedOptimizable
All Implemented Interfaces:
Optimizable, Optimizable.ByGradientValue

public class ThreadedOptimizable
extends java.lang.Object
implements Optimizable.ByGradientValue

An adaptor for optimizables based on batch values/gradients.

Computes values, gradients for each batch in multiple threads and combines them in the end.

Author:
Gaurav Chandalia
See Also:
CRFOptimizableByBatchLabelLikelihood

Nested Class Summary
 
Nested classes/interfaces inherited from interface cc.mallet.optimize.Optimizable
Optimizable.ByBatchGradient, Optimizable.ByCombiningBatchGradient, Optimizable.ByGISUpdate, Optimizable.ByGradient, Optimizable.ByGradientValue, Optimizable.ByHessian, Optimizable.ByValue, Optimizable.ByVotedPerceptron
 
Field Summary
protected  java.util.List<double[]> batchCachedGradient
          Gradient obtained from the optimizable for each batch
protected  double[] batchCachedValue
          Value obtained from the optimizable for each batch
protected  CacheStaleIndicator cacheIndicator
           
protected  Optimizable.ByCombiningBatchGradient optimizable
          optimizable to be parallelized
static int SLEEP_TIME
           
protected  InstanceList trainingSet
          Data
 
Constructor Summary
ThreadedOptimizable(Optimizable.ByCombiningBatchGradient optimizable, InstanceList trainingSet, int numFactors, CacheStaleIndicator cacheIndicator)
          Initializes the optimizable and starts new threads.
 
Method Summary
protected  void createTasks()
          Creates tasks to be executed in parallel, each task looks at a batch of data.
 int getNumParameters()
           
 Optimizable.ByCombiningBatchGradient getOptimizable()
           
 double getParameter(int index)
           
 void getParameters(double[] buffer)
           
 double getValue()
           
 void getValueGradient(double[] buffer)
          Returns the gradient, re-computes if gradient is stale.
 void setParameter(int index, double value)
           
 void setParameters(double[] buff)
           
 void shutdown()
          Shuts down the executor used to start and run threads to compute values and gradients.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

trainingSet

protected InstanceList trainingSet
Data


optimizable

protected Optimizable.ByCombiningBatchGradient optimizable
optimizable to be parallelized


batchCachedValue

protected double[] batchCachedValue
Value obtained from the optimizable for each batch


batchCachedGradient

protected java.util.List<double[]> batchCachedGradient
Gradient obtained from the optimizable for each batch


cacheIndicator

protected CacheStaleIndicator cacheIndicator

SLEEP_TIME

public static final int SLEEP_TIME
See Also:
Constant Field Values
Constructor Detail

ThreadedOptimizable

public ThreadedOptimizable(Optimizable.ByCombiningBatchGradient optimizable,
                           InstanceList trainingSet,
                           int numFactors,
                           CacheStaleIndicator cacheIndicator)
Initializes the optimizable and starts new threads.

Parameters:
optimizable - Optimizable to be parallelized
numFactors - Number of factors in model's parameters, used to initialize the gradient
cacheIndicator - Determines when value/gradient become stale
Method Detail

getOptimizable

public Optimizable.ByCombiningBatchGradient getOptimizable()

shutdown

public void shutdown()
Shuts down the executor used to start and run threads to compute values and gradients.

*Note*: For a clean exit of all the threads, it is recommended to call this method after training finishes.


getValue

public double getValue()
Specified by:
getValue in interface Optimizable.ByGradientValue

getValueGradient

public void getValueGradient(double[] buffer)
Returns the gradient, re-computes if gradient is stale.

*Note*: Assumes that buffer is already initialized.

Specified by:
getValueGradient in interface Optimizable.ByGradientValue

createTasks

protected void createTasks()
Creates tasks to be executed in parallel, each task looks at a batch of data.


getNumParameters

public int getNumParameters()
Specified by:
getNumParameters in interface Optimizable

getParameters

public void getParameters(double[] buffer)
Specified by:
getParameters in interface Optimizable

getParameter

public double getParameter(int index)
Specified by:
getParameter in interface Optimizable

setParameters

public void setParameters(double[] buff)
Specified by:
setParameters in interface Optimizable

setParameter

public void setParameter(int index,
                         double value)
Specified by:
setParameter in interface Optimizable