jsat.classifiers.linear
Class SDCA
- java.lang.Object
-
- jsat.classifiers.linear.SDCA
-
- All Implemented Interfaces:
- java.io.Serializable, java.lang.Cloneable, Classifier, WarmClassifier, Parameterized, Regressor, WarmRegressor, SimpleWeightVectorModel
public class SDCA extends java.lang.Object implements Classifier, Regressor, Parameterized, SimpleWeightVectorModel, WarmClassifier, WarmRegressor
This class implements the Proximal Stochastic Dual Coordinate Ascent (SDCA) algorithm for learning general linear models with Elastic-Net regularization. It is a fast learning algorithm, and can be used for generic Logistic or least-squares regression with elastic-net regularization.
It can work with anyLossFuncto determine if it solves a classification or regression problem, so long as theconjugatemethod of the loss is implemented properly. This is especially useful for more exotic cases, like using the robustHuber lossin a L1 regularized scenario.
NOTE: The current implementation dose not support any multi-class loss function, though that isn't a limitation of the algorithm.- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor and Description SDCA()Creates a new SDCA learner forlogistic-regression.SDCA(double lambda)
The implementation will use Elastic-Net regularization by default.SDCA(double lambda, LossFunc loss)Creates a new SDCA learner for either a classification or regression problem, the type of which is determined by the LossFunction given.SDCA(SDCA 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.SDCAclone()doublegetAlpha()doublegetBias(int index)Returns the bias term used with the weight vector for the given class index.doublegetLambda()LossFuncgetLoss()Returns the loss function in useintgetMaxIters()VecgetRawWeight(int index)Returns the raw weight vector associated with the given class index.doublegetTolerance()static DistributionguessAlpha(DataSet d)Guess the distribution to use for the regularization balancealpha.static DistributionguessLambda(DataSet d)Guess the distribution to use for the regularization termlambda.booleanisUseBias()Returns whether or not an implicit bias term is in useintnumWeightsVecs()Returns the number of weight vectors that can be returned.doubleregress(DataPoint data)voidsetAlpha(double alpha)Using α = 1 corresponds to pure L1 regularization, and α = 0 corresponds to pure L2 regularization.voidsetLambda(double lambda)Sets the regularization term, where larger values indicate a larger regularization penalty.voidsetLoss(LossFunc loss)Sets the loss function used for the model.voidsetMaxIters(int maxOuterIters)Sets the maximum number of training iterations (epochs) for the algorithm.voidsetTolerance(double e_out)Sets the tolerance parameter for convergence.voidsetUseBias(boolean useBias)Sets whether or not an implicit bias term will be added to the data setbooleansupportsWeightedData()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.voidtrain(ClassificationDataSet dataSet, Classifier warmSolution)Trains the classifier and constructs a model for classification using the given data set.voidtrain(ClassificationDataSet dataSet, Classifier warmSolution, boolean parallel)Trains the classifier and constructs a model for classification using the given data set.voidtrain(RegressionDataSet dataSet)voidtrain(RegressionDataSet dataSet, boolean parallel)voidtrain(RegressionDataSet dataSet, Regressor warmSolution)Trains the regressor and constructs a model for regression using the given data set.voidtrain(RegressionDataSet dataSet, Regressor warmSolution, boolean parallel)Trains the regressor and constructs a model for regression using the given data set.booleanwarmFromSameDataOnly()Some models can only be warm started from a solution trained on the exact same data set as the model it is warm starting from.-
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
-
-
-
-
Constructor Detail
-
SDCA
public SDCA()
Creates a new SDCA learner forlogistic-regression. Pure L2 or L1 regularization can be obtained using thealphaparameter.
-
SDCA
public SDCA(double lambda)
The implementation will use Elastic-Net regularization by default. Pure L2 or L1 regularization can be obtained using thealphaparameter.- Parameters:
lambda- the regularization penalty to use.
-
SDCA
public SDCA(double lambda, LossFunc loss)Creates a new SDCA learner for either a classification or regression problem, the type of which is determined by the LossFunction given.
The implementation will use Elastic-Net regularization by default. Pure L2 or L1 regularization can be obtained using thealphaparameter.- Parameters:
lambda- the regularization penalty to use.loss- the loss function to use for training, which determines if it implements classification or regression
-
SDCA
public SDCA(SDCA toCopy)
Copy Constructor- Parameters:
toCopy- the object to copy
-
-
Method Detail
-
setUseBias
public void setUseBias(boolean useBias)
Sets whether or not an implicit bias term will be added to the data set- Parameters:
useBias-trueto add an implicit bias term
-
isUseBias
public boolean isUseBias()
Returns whether or not an implicit bias term is in use- Returns:
trueif a bias term is in use
-
setLambda
public void setLambda(double lambda)
Sets the regularization term, where larger values indicate a larger regularization penalty.- Parameters:
lambda- the positive regularization term
-
getLambda
public double getLambda()
- Returns:
- the regularization term
-
setAlpha
public void setAlpha(double alpha)
Using α = 1 corresponds to pure L1 regularization, and α = 0 corresponds to pure L2 regularization. Any value in-between is then an Elastic Net regularization.- Parameters:
alpha- the value in [0, 1] for determining the regularization penalty's interpolation between pure L2 and L1 regularization.
-
getAlpha
public double getAlpha()
- Returns:
- the fraction of weight (in [0, 1]) to apply to L1 regularization instead of L2 regularization.
-
setMaxIters
public void setMaxIters(int maxOuterIters)
Sets the maximum number of training iterations (epochs) for the algorithm.- Parameters:
maxOuterIters- the maximum number of outer iterations
-
getMaxIters
public int getMaxIters()
- Returns:
- the maximum number of training iterations
-
setTolerance
public void setTolerance(double e_out)
Sets the tolerance parameter for convergence. Smaller values will be more exact, but larger values will converge faster. The default value is fairly exact at , increasing it by an order of magnitude can often be done without hurting accuracy.- Parameters:
e_out- the tolerance parameter.
-
getTolerance
public double getTolerance()
- Returns:
- the convergence tolerance parameter
-
setLoss
public void setLoss(LossFunc loss)
Sets the loss function used for the model. The loss function controls whether or not regression, binary classification, or multi-class classification is supported.
NOTE: SDCA requires that the given loss function implement theconjugatemethod, otherwise it will not work with this algorithm.- Parameters:
loss- the loss function to use
-
getLoss
public LossFunc getLoss()
Returns the loss function in use- Returns:
- the loss function in use
-
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.
-
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
-
train
public void train(ClassificationDataSet dataSet, Classifier warmSolution, boolean parallel)
Description copied from interface:WarmClassifierTrains 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 interfaceWarmClassifier- Parameters:
dataSet- the data set to train onwarmSolution- the solution to use to warm start this modelparallel-trueif the training should be done using multiple-cores,falsefor single threaded.
-
train
public void train(ClassificationDataSet dataSet, Classifier warmSolution)
Description copied from interface:WarmClassifierTrains the classifier and constructs a model for classification using the given data set.- Specified by:
trainin interfaceWarmClassifier- Parameters:
dataSet- the data set to train onwarmSolution- the solution to use to warm start this model
-
train
public void train(RegressionDataSet dataSet, boolean parallel)
-
train
public void train(RegressionDataSet dataSet)
-
train
public void train(RegressionDataSet dataSet, Regressor warmSolution, boolean parallel)
Description copied from interface:WarmRegressorTrains the regressor and constructs a model for regression 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 interfaceWarmRegressor- Parameters:
dataSet- the data set to train onwarmSolution- the solution to use to warm start this modelparallel-trueif the training should be done using multiple-cores,falsefor single threaded.
-
train
public void train(RegressionDataSet dataSet, Regressor warmSolution)
Description copied from interface:WarmRegressorTrains the regressor and constructs a model for regression using the given data set.- Specified by:
trainin interfaceWarmRegressor- Parameters:
dataSet- the data set to train onwarmSolution- the solution to use to warm start this model
-
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- Specified by:
supportsWeightedDatain interfaceRegressor- Returns:
- true if the model supports weighted data, false otherwise
-
clone
public SDCA clone()
- Specified by:
clonein interfaceClassifier- Specified by:
clonein interfaceRegressor- Overrides:
clonein classjava.lang.Object
-
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.
-
warmFromSameDataOnly
public boolean warmFromSameDataOnly()
Description copied from interface:WarmClassifierSome models can only be warm started from a solution trained on the exact same data set as the model it is warm starting from. If this is the casetruewill be returned. The behavior for training on a different data set when this is defined is undefined. It may cause an error, or it may cause the algorithm to take longer or reach a worse solution.
Whentrue, it is important that the data set be unaltered - this includes mutating the values stored or re-arranging the data points within the data set.- Specified by:
warmFromSameDataOnlyin interfaceWarmClassifier- Specified by:
warmFromSameDataOnlyin interfaceWarmRegressor- Returns:
trueif the algorithm can only be warm started from the model trained on the exact same data set.
-
guessLambda
public static Distribution guessLambda(DataSet d)
Guess the distribution to use for the regularization termlambda.- Parameters:
d- the data set to get the guess for- Returns:
- the guess for the lambda parameter
-
guessAlpha
public static Distribution guessAlpha(DataSet d)
Guess the distribution to use for the regularization balancealpha.- Parameters:
d- the data set to get the guess for- Returns:
- the guess for the lambda parameter
-
-
DataMelt 3.0 © DataMelt by jWork.ORG