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>
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 Summary
FieldsModifier and TypeFieldDescriptionprotected libsvm.svm_parameterThe 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
ConstructorsModifierConstructorDescriptionprotectedFor olcut.protectedLibSVMTrainer(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 andOutputs in LibSVM's format.intvoidUsed by the OLCUT configuration system, and should not be called by external code.voidsetInvocationCount(int invocationCount) protected libsvm.svm_parametersetupParameters(ImmutableOutputInfo<T> info) Constructs the svm_parameter.toString()train(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance) train(Dataset<T> examples, Map<String, com.oracle.labs.mlrg.olcut.provenance.Provenance> runProvenance, int invocationCount) 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
-
parameters
The SVM parameters suitable for use by LibSVM. -
svmType
-
-
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
Used by the OLCUT configuration system, and should not be called by external code.- Specified by:
postConfigin interfacecom.oracle.labs.mlrg.olcut.config.Configurable
-
toString
-
train
-
train
-
train
-
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 andOutputs 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
- Specified by:
getInvocationCountin interfaceTrainer<T extends Output<T>>
-
setInvocationCount
- Specified by:
setInvocationCountin interfaceTrainer<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
-