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>
Direct Known Subclasses:
LibSVMAnomalyTrainer, LibSVMClassificationTrainer, LibSVMRegressionTrainer

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

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)
      Constructs a LibSVMTrainer from the parameters.
      Parameters:
      parameters - The SVM parameters.
  • 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)
      Description copied from interface: Trainer
      Trains a predictive model using the examples in the given data set.
      Specified by:
      train in interface Trainer<T extends Output<T>>
      Parameters:
      examples - the data set containing the examples.
      Returns:
      a predictive model that can be used to generate predictions for new examples.
    • train

      public LibSVMModel<T> train(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance)
      Description copied from interface: Trainer
      Trains a predictive model using the examples in the given data set.
      Specified by:
      train in interface Trainer<T extends Output<T>>
      Parameters:
      examples - the data set containing the examples.
      runProvenance - Training run specific provenance (e.g., fold number).
      Returns:
      a predictive model that can be used to generate predictions for new examples.
    • 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)
      Train all the liblinear instances necessary for this dataset.
      Parameters:
      curParams - The LibLinear parameters.
      numFeatures - The number of features in this dataset.
      features - The features themselves.
      outputs - The outputs.
      Returns:
      A list of liblinear 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 LibLinear'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()
      Description copied from interface: Trainer
      The number of times this trainer instance has had it's train method invoked.

      This is used to determine how many times the trainer's RNG has been accessed to ensure replicability in the random number stream.

      Specified by:
      getInvocationCount in interface Trainer<T extends Output<T>>
      Returns:
      The number of train invocations.
    • 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>>