Class LibSVMTrainer<T extends Output<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
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 Summary
Modifier and TypeFieldDescriptionprotected libsvm.svm_parameter
The SVM parameters suitable for use by LibSVM.The type of SVM algorithm.Fields inherited from interface org.tribuo.Trainer
DEFAULT_SEED, INCREMENT_INVOCATION_COUNT
-
Constructor Summary
ModifierConstructorDescriptionprotected
For olcut.protected
LibSVMTrainer
(SVMParameters<T> parameters, long seed) Constructs a LibSVMTrainer from the parameters. -
Method Summary
Modifier and TypeMethodDescriptionprotected 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.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.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 andOutput
s in LibSVM's format.int
The number of times this trainer instance has had it's train method invoked.void
Used by the OLCUT configuration system, and should not be called by external code.void
setInvocationCount
(int invocationCount) Set the internal state of the trainer to the provided number of invocations of the train method.protected libsvm.svm_parameter
setupParameters
(ImmutableOutputInfo<T> info) Constructs the svm_parameter.toString()
Trains a predictive model using the examples in the given data set.train
(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance) Trains a predictive model using the examples in the given data set.train
(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance, int invocationCount) Trains a predictive model using the examples in the given data set.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.
-
Field Details
-
Constructor Details
-
LibSVMTrainer
protected LibSVMTrainer()For olcut. -
LibSVMTrainer
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 interfacecom.oracle.labs.mlrg.olcut.config.Configurable
-
toString
-
train
Description copied from interface:Trainer
Trains a predictive model using the examples in the given data set. -
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. -
train
public LibSVMModel<T> train(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance, int invocationCount) Description copied from interface:Trainer
Trains a predictive model using the examples in the given data set.- Specified by:
train
in interfaceTrainer<T extends Output<T>>
- Parameters:
examples
- the data set containing the examples.runProvenance
- Training run specific provenance (e.g., fold number).invocationCount
- The invocation counter that the trainer should be set to before training, which in most cases alters the state of the RNG inside this trainer. If the value is set toTrainer.INCREMENT_INVOCATION_COUNT
then the invocation count is not changed.- 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, 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 andOutput
s in LibSVM's format.- Parameters:
data
- The input data.outputInfo
- The output info.featureMap
- The feature info.- Returns:
- The features and outputs.
-
setupParameters
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 interfaceTrainer<T extends Output<T>>
- Returns:
- The number of train invocations.
-
setInvocationCount
public void setInvocationCount(int invocationCount) Description copied from interface:Trainer
Set the internal state of the trainer to the provided number of invocations of the train method.This is used when reproducing a Tribuo-trained model by setting the state of the RNG to what it was at when Tribuo trained the original model by simulating invocations of the train method. This method should ALWAYS be overridden, and the default method is purely for compatibility.
In a future major release this default implementation will be removed.
- Specified by:
setInvocationCount
in interfaceTrainer<T extends Output<T>>
- Parameters:
invocationCount
- the number of invocations of the train method to simulate
-
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
-