jsat.classifiers.svm
Class Pegasos
- java.lang.Object
-
- jsat.classifiers.svm.Pegasos
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, BinaryScoreClassifier, Classifier, Parameterized, SimpleWeightVectorModel, SingleWeightVectorModel
public class Pegasos extends java.lang.Object implements BinaryScoreClassifier, Parameterized, SingleWeightVectorModel
Implements the linear kernel mini-batch version of the Pegasos SVM classifier. It performs updates stochastically and is very fast.
Because Pegasos updates the primal directly, there are no support vectors saved from the training set.
See: Shalev-Shwartz, S., Singer, Y.,&Srebro, N. (2007). Pegasos : Primal Estimated sub-GrAdient SOlver for SVM. 24th international conference on Machine learning (pp. 807–814). New York, NY: ACM. doi:10.1145/1273496.1273598- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field and Description static intDEFAULT_BATCH_SIZEThe default batch size is 1static intDEFAULT_EPOCHSThe default number of epochs is 5static doubleDEFAULT_REGThe default regularization value is 1.0E-4
-
Constructor Summary
Constructors Constructor and Description Pegasos()Creates a new Pegasos SVM classifier using default values.Pegasos(int epochs, double reg, int batchSize)Creates a new Pegasos SVM classifierPegasos(Pegasos toCopy)Copy constructor
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method and Description CategoricalResultsclassify(DataPoint data)Performs classification on the given data point.Pegasosclone()intgetBatchSize()Returns the number of points used in each iterationdoublegetBias()Returns the bias term used for the model, or 0 of the model does not support or was not trained with a bias term.doublegetBias(int index)Returns the bias term used with the weight vector for the given class index.doublegetEpochs()Returns the number of iterations of updating that will be doneVecgetRawWeight()Returns the only weight vector used for the modelVecgetRawWeight(int index)Returns the raw weight vector associated with the given class index.doublegetRegularization()Returns the amount of regularization to used in trainingdoublegetScore(DataPoint dp)Returns the numeric score for predicting a class of a given data point, where the sign of the value indicates which class the data point is predicted to belong to.static DistributionguessRegularization(DataSet d)Guess the distribution to use for the regularization termsetRegularization(double)in Pegasos.booleanisProjectionStep()Returns whether or not the projection step is in use after each iterationintnumWeightsVecs()Returns the number of weight vectors that can be returned.voidsetBatchSize(int batchSize)Sets the batch size used during training.voidsetEpochs(int epochs)Sets the number of iterations through the training set that will be performed.voidsetProjectionStep(boolean projectionStep)Sets whether or not to use the projection step after each update per iterationvoidsetRegularization(double reg)Sets the regularization constant used for learning.booleansupportsWeightedData()Indicates whether the model knows how to train using weighted data points.voidtrain(ClassificationDataSet dataSet)Trains the classifier and constructs a model for classification using the given data set.voidtrain(ClassificationDataSet dataSet, boolean parallel)Trains the classifier and constructs a model for classification using the given data set.-
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jsat.parameters.Parameterized
getParameter, getParameters
-
-
-
-
Field Detail
-
DEFAULT_EPOCHS
public static final int DEFAULT_EPOCHS
The default number of epochs is 5- See Also:
- Constant Field Values
-
DEFAULT_REG
public static final double DEFAULT_REG
The default regularization value is 1.0E-4- See Also:
- Constant Field Values
-
DEFAULT_BATCH_SIZE
public static final int DEFAULT_BATCH_SIZE
The default batch size is 1- See Also:
- Constant Field Values
-
-
Constructor Detail
-
Pegasos
public Pegasos()
Creates a new Pegasos SVM classifier using default values.
-
Pegasos
public Pegasos(int epochs, double reg, int batchSize)Creates a new Pegasos SVM classifier- Parameters:
epochs- the number of training iterationsreg- the regularization termbatchSize- the batch size
-
Pegasos
public Pegasos(Pegasos toCopy)
Copy constructor- Parameters:
toCopy- the object to copy
-
-
Method Detail
-
setBatchSize
public void setBatchSize(int batchSize)
Sets the batch size used during training. At each epoch, a batch of randomly selected data points will be used to update.- Parameters:
batchSize- the number of data points to use when updating
-
getBatchSize
public int getBatchSize()
Returns the number of points used in each iteration- Returns:
- the number of points used in each iteration
-
setEpochs
public void setEpochs(int epochs)
Sets the number of iterations through the training set that will be performed.- Parameters:
epochs- the number of iterations
-
getEpochs
public double getEpochs()
Returns the number of iterations of updating that will be done- Returns:
- the number of iterations
-
setProjectionStep
public void setProjectionStep(boolean projectionStep)
Sets whether or not to use the projection step after each update per iteration- Parameters:
projectionStep- whether or not to use the projection step
-
isProjectionStep
public boolean isProjectionStep()
Returns whether or not the projection step is in use after each iteration- Returns:
- true if the projection step will be performed
-
setRegularization
public void setRegularization(double reg)
Sets the regularization constant used for learning. The regularization must be positive, and the learning rate is proportional to the regularization value. This means regularizations very near zero will take a long time to converge.- Parameters:
reg- the regularization to apply
-
getRegularization
public double getRegularization()
Returns the amount of regularization to used in training- Returns:
- the regularization parameter.
-
getRawWeight
public Vec getRawWeight()
Description copied from interface:SingleWeightVectorModelReturns the only weight vector used for the model- Specified by:
getRawWeightin interfaceSingleWeightVectorModel- Returns:
- the only weight vector used for the model
-
getBias
public double getBias()
Description copied from interface:SingleWeightVectorModelReturns the bias term used for the model, or 0 of the model does not support or was not trained with a bias term.- Specified by:
getBiasin interfaceSingleWeightVectorModel- Returns:
- the bias term for the model
-
getRawWeight
public Vec getRawWeight(int index)
Description copied from interface:SimpleWeightVectorModelReturns the raw weight vector associated with the given class index. If the given class is an implicit zero vector, aConstantVectorobject may be returned.
Do not alter the returned weight vector, as it will change the model's values.
If a regression problem, onlyindex = 0should be used- Specified by:
getRawWeightin interfaceSimpleWeightVectorModel- Parameters:
index- the class index to get the weight vector for- Returns:
- the weight vector used for the specified class
-
getBias
public double getBias(int index)
Description copied from interface:SimpleWeightVectorModelReturns the bias term used with the weight vector for the given class index. If the model does not support or was not trained with bias weights,0will be returned.
If a regression problem, onlyindex = 0should be used- Specified by:
getBiasin interfaceSimpleWeightVectorModel- Parameters:
index- the class index to get the weight vector for- Returns:
- the bias term for the specified class
-
numWeightsVecs
public int numWeightsVecs()
Description copied from interface:SimpleWeightVectorModelReturns the number of weight vectors that can be returned. For binary classification problems the value may be 1 if only a single weight vector's sign is used to determine the class. For multi-class problems, the weight vector count includes the implicit zero vector (if one is being used).- Specified by:
numWeightsVecsin interfaceSimpleWeightVectorModel- Returns:
- the number of weight vectors for which
SimpleWeightVectorModel.getRawWeight(int)can be called.
-
clone
public Pegasos clone()
- Specified by:
clonein interfaceBinaryScoreClassifier- Specified by:
clonein interfaceClassifier- Overrides:
clonein classjava.lang.Object
-
classify
public CategoricalResults classify(DataPoint data)
Description copied from interface:ClassifierPerforms classification on the given data point.- Specified by:
classifyin interfaceClassifier- Parameters:
data- the data point to classify- Returns:
- the results of the classification.
-
getScore
public double getScore(DataPoint dp)
Description copied from interface:BinaryScoreClassifierReturns the numeric score for predicting a class of a given data point, where the sign of the value indicates which class the data point is predicted to belong to.- Specified by:
getScorein interfaceBinaryScoreClassifier- Parameters:
dp- the data point to predict the class label of- Returns:
- the score for the given data point
-
train
public void train(ClassificationDataSet dataSet, boolean parallel)
Description copied from interface:ClassifierTrains the classifier and constructs a model for classification using the given data set. If the training method knows how, it will used the threadPool to conduct training in parallel. This method will block until the training has completed.- Specified by:
trainin interfaceClassifier- Parameters:
dataSet- the data set to train onparallel-trueif multiple threads should be used to train the model.falseif it should be done in a single threaded manner.
-
train
public void train(ClassificationDataSet dataSet)
Description copied from interface:ClassifierTrains the classifier and constructs a model for classification using the given data set.- Specified by:
trainin interfaceClassifier- Parameters:
dataSet- the data set to train on
-
supportsWeightedData
public boolean supportsWeightedData()
Description copied from interface:ClassifierIndicates whether the model knows how to train using weighted data points. If it does, the model will train assuming the weights. The values returned by this method may change depending on the parameters set for the model.- Specified by:
supportsWeightedDatain interfaceClassifier- Returns:
- true if the model supports weighted data, false otherwise
-
guessRegularization
public static Distribution guessRegularization(DataSet d)
Guess the distribution to use for the regularization termsetRegularization(double)in Pegasos.- Parameters:
d- the data set to get the guess for- Returns:
- the guess for the λ parameter
-
-
DataMelt 3.0 © DataMelt by jWork.ORG