Implementation of Stochastic Coordinate Descent for L1 regularized classification and regression. Which one is supported is controlled by the LossFunc used. To be used the loss function must be twice differentiable with a finite maximal second derivative value. LogisticLoss for class
| 35 | * @author Edward Raff |
| 36 | */ |
| 37 | public class SCD implements Classifier, Regressor, Parameterized, SingleWeightVectorModel |
| 38 | { |
| 39 | |
| 40 | private static final long serialVersionUID = 3576901723216525618L; |
| 41 | private Vec w; |
| 42 | private LossFunc loss; |
| 43 | private double reg; |
| 44 | private int iterations; |
| 45 | |
| 46 | /** |
| 47 | * Creates anew SCD learner |
| 48 | * |
| 49 | * @param loss the loss function to use |
| 50 | * @param regularization the regularization term to used |
| 51 | * @param iterations the number of iterations to perform |
| 52 | */ |
| 53 | public SCD(LossFunc loss, double regularization, int iterations) |
| 54 | { |
| 55 | double beta = loss.getDeriv2Max(); |
| 56 | if (Double.isNaN(beta) || Double.isInfinite(beta) || beta <= 0) |
| 57 | throw new IllegalArgumentException("SCD needs a loss function with a finite positive maximal second derivative"); |
| 58 | this.loss = loss; |
| 59 | setRegularization(regularization); |
| 60 | setIterations(iterations); |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Copy constructor |
| 65 | * |
| 66 | * @param toCopy the object to copy |
| 67 | */ |
| 68 | public SCD(SCD toCopy) |
| 69 | { |
| 70 | this(toCopy.loss.clone(), toCopy.reg, toCopy.iterations); |
| 71 | if (toCopy.w != null) |
| 72 | this.w = toCopy.w.clone(); |
| 73 | } |
| 74 | |
| 75 | /** |
| 76 | * Sets the number of iterations that will be used. |
| 77 | * |
| 78 | * @param iterations the number of training iterations |
| 79 | */ |
| 80 | public void setIterations(int iterations) |
| 81 | { |
| 82 | if(iterations < 1) |
| 83 | throw new IllegalArgumentException("The iterations must be a positive value, not " + iterations); |
| 84 | this.iterations = iterations; |
| 85 | } |
| 86 | |
| 87 | /** |
| 88 | * Returns the number of iterations used |
| 89 | * |
| 90 | * @return the number of iterations used |
| 91 | */ |
| 92 | public int getIterations() |
| 93 | { |
| 94 | return iterations; |
nothing calls this directly
no outgoing calls
no test coverage detected