Class LibSVMTrainer<T extends Output<T>>

java.lang.Object
org.tribuo.common.libsvm.LibSVMTrainer<T>
All Implemented Interfaces:
com.oracle.labs.mlrg.olcut.config.Configurable, com.oracle.labs.mlrg.olcut.provenance.Provenancable<TrainerProvenance>, Trainer<T>

public abstract class LibSVMTrainer<T extends Output<T>> extends Object implements Trainer<T>
A trainer that will train using libsvm's Java implementation.

Note the train method is synchronized on LibSVMTrainer.class due to a global RNG in LibSVM. This is insufficient to ensure reproducibility if LibSVM is used directly in the same JVM as Tribuo, but avoids locking on classes Tribuo does not control.

See:

 Chang CC, Lin CJ.
 "LIBSVM: a library for Support Vector Machines"
 ACM transactions on intelligent systems and technology (TIST), 2011.
 
for the nu-svm algorithm:
 Schölkopf B, Smola A, Williamson R, Bartlett P L.
 "New support vector algorithms"
 Neural Computation, 2000, 1207-1245.
 
and for the original algorithm:
 Cortes C, Vapnik V.
 "Support-Vector Networks"
 Machine Learning, 1995.
 
  • Field Details

    • parameters

      protected libsvm.svm_parameter parameters
      The SVM parameters suitable for use by LibSVM.
    • svmType

      @Config(mandatory=true, description="Type of SVM algorithm.") protected SVMType<T extends Output<T>> svmType
      The type of SVM algorithm.
  • Constructor Details

    • LibSVMTrainer

      protected LibSVMTrainer()
      For olcut.
    • LibSVMTrainer

      protected LibSVMTrainer(SVMParameters<T> parameters, long seed)
      Constructs a LibSVMTrainer from the parameters.
      Parameters:
      parameters - The SVM parameters.
      seed - The RNG seed.
  • Method Details

    • postConfig

      public void postConfig()
      Used by the OLCUT configuration system, and should not be called by external code.
      Specified by:
      postConfig in interface com.oracle.labs.mlrg.olcut.config.Configurable
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • train

      public LibSVMModel<T> train(Dataset<T> examples)
      Specified by:
      train in interface Trainer<T extends Output<T>>
    • train

      public LibSVMModel<T> train(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance)
      Specified by:
      train in interface Trainer<T extends Output<T>>
    • train

      public LibSVMModel<T> train(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance, int invocationCount)
      Specified by:
      train in interface Trainer<T extends Output<T>>
    • createModel

      protected abstract LibSVMModel<T> createModel(ModelProvenance provenance, ImmutableFeatureMap featureIDMap, ImmutableOutputInfo<T> outputIDInfo, List<libsvm.svm_model> models)
      Construct the appropriate subtype of LibSVMModel for the prediction task.
      Parameters:
      provenance - The model provenance.
      featureIDMap - The feature id map.
      outputIDInfo - The output id info.
      models - The svm models.
      Returns:
      An implementation of LibSVMModel.
    • trainModels

      protected abstract List<libsvm.svm_model> trainModels(libsvm.svm_parameter curParams, int numFeatures, libsvm.svm_node[][] features, double[][] outputs, SplittableRandom localRNG)
      Train all the LibSVM instances necessary for this dataset.
      Parameters:
      curParams - The LibSVM parameters.
      numFeatures - The number of features in this dataset.
      features - The features themselves.
      outputs - The outputs.
      localRNG - The RNG to use for seeding LibSVM's RNG.
      Returns:
      A list of LibSVM models.
    • extractData

      protected abstract com.oracle.labs.mlrg.olcut.util.Pair<libsvm.svm_node[][], double[][]> extractData(Dataset<T> data, ImmutableOutputInfo<T> outputInfo, ImmutableFeatureMap featureMap)
      Extracts the features and Outputs in LibSVM's format.
      Parameters:
      data - The input data.
      outputInfo - The output info.
      featureMap - The feature info.
      Returns:
      The features and outputs.
    • setupParameters

      protected libsvm.svm_parameter setupParameters(ImmutableOutputInfo<T> info)
      Constructs the svm_parameter. Most of the time this is a no-op, but classification overrides it to incorporate label weights if they exist.
      Parameters:
      info - The output info.
      Returns:
      The svm_parameters to use for training.
    • getInvocationCount

      public int getInvocationCount()
      Specified by:
      getInvocationCount in interface Trainer<T extends Output<T>>
    • setInvocationCount

      public void setInvocationCount(int invocationCount)
      Specified by:
      setInvocationCount in interface Trainer<T extends Output<T>>
    • exampleToNodes

      public static <T extends Output<T>> libsvm.svm_node[] exampleToNodes(Example<T> example, ImmutableFeatureMap featureIDMap, List<libsvm.svm_node> features)
      Convert the example into an array of svm_node which represents a sparse feature vector.

      If there are collisions in the feature ids then the values are summed.

      Type Parameters:
      T - The type of the ouput.
      Parameters:
      example - The example to convert.
      featureIDMap - The feature id map which holds the indices.
      features - A buffer to use.
      Returns:
      A sparse feature vector.
    • getProvenance

      Specified by:
      getProvenance in interface com.oracle.labs.mlrg.olcut.provenance.Provenancable<T extends Output<T>>