From befd7c4209222ebfe4adc9c3b0911474b16dc825 Mon Sep 17 00:00:00 2001 From: yimingjiang Date: Sun, 12 Jun 2016 17:56:44 -0700 Subject: [PATCH 1/3] sgd regression & sgd classification --- .../learn/StochasticGradientDescent.java | 241 ++++++---- .../learn/StochasticGradientDescentCL.java | 434 ++++++++++++++++++ 2 files changed, 581 insertions(+), 94 deletions(-) create mode 100644 lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java diff --git a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java index fc29e2ba..dab7835c 100644 --- a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java +++ b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java @@ -1,14 +1,17 @@ /** - * This software is released under the University of Illinois/Research and Academic Use License. See - * the LICENSE file in the root folder for details. Copyright (c) 2016 - * - * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign + * This software is released under the University of Illinois/Research and + * Academic Use License. See the LICENSE file in the root folder for details. + * Copyright (c) 2016 + *

+ * Developed by: + * The Cognitive Computations Group + * University of Illinois at Urbana-Champaign * http://cogcomp.cs.illinois.edu/ */ package edu.illinois.cs.cogcomp.lbjava.learn; import java.io.PrintStream; - +import java.util.Objects; import edu.illinois.cs.cogcomp.lbjava.classify.Feature; import edu.illinois.cs.cogcomp.lbjava.classify.FeatureVector; import edu.illinois.cs.cogcomp.lbjava.classify.RealPrimitiveStringFeature; @@ -18,20 +21,22 @@ /** - * Gradient descent is a batch learning algorithm for function approximation in which the learner - * tries to follow the gradient of the error function to the solution of minimal error. This - * implementation is a stochastic approximation to gradient descent in which the approximated - * function is assumed to have linear form. + * Gradient descent is a batch learning algorithm for function approximation + * in which the learner tries to follow the gradient of the error function to + * the solution of minimal error. This implementation is a stochastic + * approximation to gradient descent in which the approximated function is + * assumed to have linear form. * - *

- * This algorithm's user-configurable parameters are stored in member fields of this class. They may - * be set via either a constructor that names each parameter explicitly or a constructor that takes - * an instance of {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters - * Parameters} as input. The documentation in each member field in this class indicates the default - * value of the associated parameter when using the former type of constructor. The documentation of - * the associated member field in the - * {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters Parameters} - * class indicates the default value of the parameter when using the latter type of constructor. + *

This algorithm's user-configurable parameters are stored in member + * fields of this class. They may be set via either a constructor that names + * each parameter explicitly or a constructor that takes an instance of + * {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters Parameters} as + * input. The documentation in each member field in this class indicates the + * default value of the associated parameter when using the former type of + * constructor. The documentation of the associated member field in the + * {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters Parameters} class + * indicates the default value of the parameter when using the latter type of + * constructor. * * @author Nick Rizzolo **/ @@ -39,7 +44,10 @@ public class StochasticGradientDescent extends Learner { /** Default value for {@link #learningRate}. */ public static final double defaultLearningRate = 0.1; /** Default for {@link #weightVector}. */ - public static final SparseWeightVector defaultWeightVector = new SparseWeightVector(); + public static final SparseWeightVector defaultWeightVector = + new SparseWeightVector(); + /** Default loss function */ + public static final String defaultLossFunction = "lms"; /** The hypothesis vector; default {@link #defaultWeightVector}. */ @@ -49,34 +57,42 @@ public class StochasticGradientDescent extends Learner { **/ protected double bias; /** - * The rate at which weights are updated; default {@link #defaultLearningRate}. + * The rate at which weights are updated; default + * {@link #defaultLearningRate}. **/ protected double learningRate; - + /** + * The name of the loss function + */ + protected String lossFunction; + /** + * Boolean flag for loss function + */ + private boolean isLMS; /** - * The learning rate takes the default value, while the name of the classifier gets the empty - * string. + * The learning rate takes the default value, while the name of the + * classifier gets the empty string. **/ public StochasticGradientDescent() { this(""); } /** - * Sets the learning rate to the specified value, while the name of the classifier gets the - * empty string. + * Sets the learning rate to the specified value, while the name of the + * classifier gets the empty string. * - * @param r The desired learning rate value. + * @param r The desired learning rate value. **/ public StochasticGradientDescent(double r) { this("", r); } /** - * Initializing constructor. Sets all member variables to their associated settings in the - * {@link StochasticGradientDescent.Parameters} object. + * Initializing constructor. Sets all member variables to their associated + * settings in the {@link StochasticGradientDescent.Parameters} object. * - * @param p The settings of all parameters. + * @param p The settings of all parameters. **/ public StochasticGradientDescent(Parameters p) { this("", p); @@ -85,17 +101,18 @@ public StochasticGradientDescent(Parameters p) { /** * The learning rate takes the default value. * - * @param n The name of the classifier. + * @param n The name of the classifier. **/ public StochasticGradientDescent(String n) { this(n, defaultLearningRate); } /** - * Use this constructor to specify an alternative subclass of {@link SparseWeightVector}. + * Use this constructor to specify an alternative subclass of + * {@link SparseWeightVector}. * - * @param n The name of the classifier. - * @param r The desired learning rate value. + * @param n The name of the classifier. + * @param r The desired learning rate value. **/ public StochasticGradientDescent(String n, double r) { super(n); @@ -105,11 +122,11 @@ public StochasticGradientDescent(String n, double r) { } /** - * Initializing constructor. Sets all member variables to their associated settings in the - * {@link StochasticGradientDescent.Parameters} object. + * Initializing constructor. Sets all member variables to their associated + * settings in the {@link StochasticGradientDescent.Parameters} object. * - * @param n The name of the classifier. - * @param p The settings of all parameters. + * @param n The name of the classifier. + * @param p The settings of all parameters. **/ public StochasticGradientDescent(String n, Parameters p) { super(n); @@ -118,21 +135,33 @@ public StochasticGradientDescent(String n, Parameters p) { /** - * Sets the values of parameters that control the behavior of this learning algorithm. + * Sets the values of parameters that control the behavior of this learning + * algorithm. * - * @param p The parameters. + * @param p The parameters. **/ public void setParameters(Parameters p) { weightVector = p.weightVector; learningRate = p.learningRate; + lossFunction = p.lossFunction; + if (Objects.equals(p.lossFunction, "lms")) { + isLMS = true; + } + else if (Objects.equals(p.lossFunction, "hinge")) { + isLMS = false; + } + else { + System.out.println("Undefined loss function! lms or hinge"); + System.exit(-1); + } } /** * Retrieves the parameters that are set in this learner. * - * @return An object containing all the values of the parameters that control the behavior of - * this learning algorithm. + * @return An object containing all the values of the parameters that + * control the behavior of this learning algorithm. **/ public Learner.Parameters getParameters() { Parameters p = new Parameters(super.getParameters()); @@ -153,14 +182,19 @@ public double getLearningRate() { /** - * Sets the {@link #learningRate} member variable to the specified value. + * Sets the {@link #learningRate} member variable to the specified + * value. * - * @param t The new value for {@link #learningRate}. + * @param t The new value for {@link #learningRate}. **/ public void setLearningRate(double t) { learningRate = t; } + public String getLossFunction() { + return lossFunction; + } + /** Resets the weight vector to all zeros. */ public void forget() { @@ -183,29 +217,39 @@ public String getOutputType() { /** * Trains the learning algorithm given an object as an example. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. - * @param exampleLabels The example's label(s). - * @param labelValues The labels' values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. + * @param exampleLabels The example's label(s). + * @param labelValues The labels' values. **/ - public void learn(int[] exampleFeatures, double[] exampleValues, int[] exampleLabels, - double[] labelValues) { - assert exampleLabels.length == 1 : "Example must have a single label."; + public void learn(int[] exampleFeatures, double[] exampleValues, + int[] exampleLabels, double[] labelValues) { + assert exampleLabels.length == 1 + : "Example must have a single label."; double labelValue = labelValues[0]; - double multiplier = - learningRate - * (labelValue - weightVector.dot(exampleFeatures, exampleValues) - bias); - weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); - bias += multiplier; + double wtx = weightVector.dot(exampleFeatures, exampleValues) + bias; + + if (isLMS) { + double multiplier = learningRate * (labelValue - wtx); + weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); + bias += multiplier; + } + else { + if (labelValue * wtx <= 1) { + double multiplier = learningRate * labelValue; + weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); + bias += multiplier; + } + } } /** * Since this algorithm returns a real feature, it does not return scores. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return null **/ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { @@ -214,23 +258,25 @@ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { /** - * Returns the classification of the given example as a single feature instead of a - * {@link FeatureVector}. + * Returns the classification of the given example as a single feature + * instead of a {@link FeatureVector}. * - * @param f The features array. - * @param v The values array. + * @param f The features array. + * @param v The values array. * @return The classification of the example as a feature. **/ public Feature featureValue(int[] f, double[] v) { - return new RealPrimitiveStringFeature(containingPackage, name, "", realValue(f, v)); + return + new RealPrimitiveStringFeature(containingPackage, name, "", + realValue(f, v)); } /** * Simply computes the dot product of the weight vector and the example * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return The computed real value. **/ public double realValue(int[] exampleFeatures, double[] exampleValues) { @@ -239,11 +285,11 @@ public double realValue(int[] exampleFeatures, double[] exampleValues) { /** - * Simply computes the dot product of the weight vector and the feature vector extracted from - * the example object. + * Simply computes the dot product of the weight vector and the feature + * vector extracted from the example object. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return The computed feature (in a vector). **/ public FeatureVector classify(int[] exampleFeatures, double[] exampleValues) { @@ -252,24 +298,23 @@ public FeatureVector classify(int[] exampleFeatures, double[] exampleValues) { /** - * Writes the algorithm's internal representation as text. In the first line of output, the name - * of the classifier is printed, followed by {@link #learningRate} and {@link #bias}. + * Writes the algorithm's internal representation as text. In the first + * line of output, the name of the classifier is printed, followed by + * {@link #learningRate} and {@link #bias}. * - * @param out The output stream. + * @param out The output stream. **/ public void write(PrintStream out) { out.println(name + ": " + learningRate + ", " + bias); - if (lexicon.size() == 0) - weightVector.write(out); - else - weightVector.write(out, lexicon); + if (lexicon.size() == 0) weightVector.write(out); + else weightVector.write(out, lexicon); } /** * Writes the learned function's internal representation in binary form. * - * @param out The output stream. + * @param out The output stream. **/ public void write(ExceptionlessOutputStream out) { super.write(out); @@ -280,9 +325,9 @@ public void write(ExceptionlessOutputStream out) { /** - * Reads the binary representation of a learner with this object's run-time type, overwriting - * any and all learned or manually specified parameters as well as the label lexicon but without - * modifying the feature lexicon. + * Reads the binary representation of a learner with this object's run-time + * type, overwriting any and all learned or manually specified parameters + * as well as the label lexicon but without modifying the feature lexicon. * * @param in The input stream. **/ @@ -311,38 +356,46 @@ public Object clone() { /** - * Simply a container for all of {@link StochasticGradientDescent}'s configurable parameters. - * Using instances of this class should make code more readable and constructors less - * complicated. + * Simply a container for all of {@link StochasticGradientDescent}'s + * configurable parameters. Using instances of this class should make code + * more readable and constructors less complicated. * * @author Nick Rizzolo **/ public static class Parameters extends Learner.Parameters { /** - * The hypothesis vector; default {@link StochasticGradientDescent#defaultWeightVector}. + * The hypothesis vector; default + * {@link StochasticGradientDescent#defaultWeightVector}. **/ public SparseWeightVector weightVector; /** - * The rate at which weights are updated; default {@link #defaultLearningRate}. + * The rate at which weights are updated; default + * {@link #defaultLearningRate}. **/ public double learningRate; + /** + * This name of the loss function + */ + public String lossFunction; /** Sets all the default values. */ public Parameters() { weightVector = (SparseWeightVector) defaultWeightVector.clone(); learningRate = defaultLearningRate; + lossFunction = defaultLossFunction; } /** - * Sets the parameters from the parent's parameters object, giving defaults to all - * parameters declared in this object. + * Sets the parameters from the parent's parameters object, giving + * defaults to all parameters declared in this object. **/ public Parameters(Learner.Parameters p) { super(p); weightVector = (SparseWeightVector) defaultWeightVector.clone(); learningRate = defaultLearningRate; + lossFunction = defaultLossFunction; } @@ -351,14 +404,15 @@ public Parameters(Parameters p) { super(p); weightVector = p.weightVector; learningRate = p.learningRate; + lossFunction = p.lossFunction; } /** - * Calls the appropriate Learner.setParameters(Parameters) method for this - * Parameters object. + * Calls the appropriate Learner.setParameters(Parameters) + * method for this Parameters object. * - * @param l The learner whose parameters will be set. + * @param l The learner whose parameters will be set. **/ public void setParameters(Learner l) { ((StochasticGradientDescent) l).setParameters(this); @@ -366,8 +420,8 @@ public void setParameters(Learner l) { /** - * Creates a string representation of these parameters in which only those parameters that - * differ from their default values are mentioned. + * Creates a string representation of these parameters in which only + * those parameters that differ from their default values are mentioned. **/ public String nonDefaultString() { String result = super.nonDefaultString(); @@ -375,9 +429,8 @@ public String nonDefaultString() { if (learningRate != StochasticGradientDescent.defaultLearningRate) result += ", learningRate = " + learningRate; - if (result.startsWith(", ")) - result = result.substring(2); + if (result.startsWith(", ")) result = result.substring(2); return result; } } -} +} \ No newline at end of file diff --git a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java new file mode 100644 index 00000000..7b5f4913 --- /dev/null +++ b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java @@ -0,0 +1,434 @@ +/** + * This software is released under the University of Illinois/Research and + * Academic Use License. See the LICENSE file in the root folder for details. + * Copyright (c) 2016 + *

+ * Developed by: + * The Cognitive Computations Group + * University of Illinois at Urbana-Champaign + * http://cogcomp.cs.illinois.edu/ + */ +package edu.illinois.cs.cogcomp.lbjava.learn; + +import edu.illinois.cs.cogcomp.lbjava.classify.Feature; +import edu.illinois.cs.cogcomp.lbjava.classify.FeatureVector; +import edu.illinois.cs.cogcomp.lbjava.classify.ScoreSet; +import edu.illinois.cs.cogcomp.lbjava.util.ExceptionlessInputStream; +import edu.illinois.cs.cogcomp.lbjava.util.ExceptionlessOutputStream; +import java.io.PrintStream; +import java.util.Objects; + +/** + * Stochastic Gradient Descent learning algorithm for classification + * + * There are two user-configurable loss functions: hinge, least mean square. + * Default is least mean square, "lms". + * + * @author Yiming Jiang + */ +public class StochasticGradientDescentCL extends LinearThresholdUnit { + /** Default value for {@link #learningRate}. */ + public static final double defaultLearningRate = 0.1; + /** Default for {@link #weightVector}. */ + public static final SparseWeightVector defaultWeightVector = + new SparseWeightVector(); + /** Default loss function */ + public static final String defaultLossFunction = "lms"; + + + /** The hypothesis vector; default {@link #defaultWeightVector}. */ + protected SparseWeightVector weightVector; + /** + * The bias is stored here rather than as an element of the weight vector. + **/ + protected double bias; + /** + * The rate at which weights are updated; default + * {@link #defaultLearningRate}. + **/ + protected double learningRate; + /** + * The name of the loss function + */ + protected String lossFunction; + /** + * Boolean flag for loss function + */ + protected boolean isLMS; + + /** + * The learning rate takes the default value, while the name of the + * classifier gets the empty string. + **/ + public StochasticGradientDescentCL() { + this(""); + } + + /** + * Sets the learning rate to the specified value, while the name of the + * classifier gets the empty string. + * + * @param r The desired learning rate value. + **/ + public StochasticGradientDescentCL(double r) { + this("", r); + } + + /** + * Initializing constructor. Sets all member variables to their associated + * settings in the {@link StochasticGradientDescent.Parameters} object. + * + * @param p The settings of all parameters. + **/ + public StochasticGradientDescentCL(Parameters p) { + this("", p); + } + + /** + * The learning rate takes the default value. + * + * @param n The name of the classifier. + **/ + public StochasticGradientDescentCL(String n) { + this(n, defaultLearningRate); + } + + /** + * Use this constructor to specify an alternative subclass of + * {@link SparseWeightVector}. + * + * @param n The name of the classifier. + * @param r The desired learning rate value. + **/ + public StochasticGradientDescentCL(String n, double r) { + super(n); + Parameters p = new Parameters(); + p.learningRate = r; + setParameters(p); + } + + /** + * Initializing constructor. Sets all member variables to their associated + * settings in the {@link StochasticGradientDescent.Parameters} object. + * + * @param n The name of the classifier. + * @param p The settings of all parameters. + **/ + public StochasticGradientDescentCL(String n, Parameters p) { + super(n); + setParameters(p); + } + + + /** + * Sets the values of parameters that control the behavior of this learning + * algorithm. + * + * @param p The parameters. + **/ + public void setParameters(Parameters p) { + weightVector = p.weightVector; + learningRate = p.learningRate; + lossFunction = p.lossFunction; + if (Objects.equals(p.lossFunction, "lms")) { + isLMS = true; + } + else if (Objects.equals(p.lossFunction, "hinge")) { + isLMS = false; + } + else { + System.out.println("Undefined loss function! lms or hinge"); + System.exit(-1); + } + } + + + /** + * Retrieves the parameters that are set in this learner. + * + * @return An object containing all the values of the parameters that + * control the behavior of this learning algorithm. + **/ + public Learner.Parameters getParameters() { + Parameters p = new Parameters(super.getParameters()); + p.weightVector = weightVector; + p.learningRate = learningRate; + return p; + } + + + /** + * Returns the current value of the {@link #learningRate} variable. + * + * @return The value of the {@link #learningRate} variable. + **/ + public double getLearningRate() { + return learningRate; + } + + + /** + * Sets the {@link #learningRate} member variable to the specified + * value. + * + * @param t The new value for {@link #learningRate}. + **/ + public void setLearningRate(double t) { + learningRate = t; + } + + public String getLossFunction() { + return lossFunction; + } + + + /** Resets the weight vector to all zeros. */ + public void forget() { + super.forget(); + weightVector = weightVector.emptyClone(); + bias = 0; + } + + /** Inherited unused method from LTU class */ + @Override + public void promote(int[] exampleFeatures, double[] exampleValues, double rate) { + + } + + /** Inherited unused method from LTU class */ + @Override + public void demote(int[] exampleFeatures, double[] exampleValues, double rate) { + + } + + + /** + * Trains the learning algorithm given an object as an example. + * + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. + * @param exampleLabels The example's label(s). + * @param labelValues The labels' values. + **/ + public void learn(int[] exampleFeatures, double[] exampleValues, + int[] exampleLabels, double[] labelValues) { + assert exampleLabels.length == 1 + : "Example must have a single label."; + + double labelValue = 1; + if (exampleLabels[0] == 1) { + labelValue = 1; + } + else if (exampleLabels[0] == 0) { + labelValue = -1; + } + + double wtx = weightVector.dot(exampleFeatures, exampleValues) + bias; + + learnUpdate(exampleFeatures, exampleValues, labelValue, wtx); + } + + void learnUpdate(int[] exampleFeatures, double[] exampleValues, double labelValue, double wtx) { + if (isLMS) { + double multiplier = learningRate * (labelValue - wtx); + weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); + bias += multiplier; + } + else { + if (labelValue * wtx <= 1) { + double multiplier = learningRate * labelValue; + weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); + bias += multiplier; + } + } + } + + + /** + * Since this algorithm returns a real feature, it does not return scores. + * + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. + * @return null + **/ + public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { + return null; + } + + + /** + * Returns the classification of the given example as a single feature + * instead of a {@link FeatureVector}. + * + * @param f The features array. + * @param v The values array. + * @return The classification of the example as a feature. + **/ + public Feature featureValue(int[] f, double[] v) { + int index = score(f, v) >= 0 ? 1 : 0; + return predictions.get(index); + } + + + /** + * Simply computes the dot product of the weight vector and the example + * + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. + * @return The computed real value. + **/ + public double score(int[] exampleFeatures, double[] exampleValues) { + return weightVector.dot(exampleFeatures, exampleValues) + bias; + } + + + /** + * Simply computes the dot product of the weight vector and the feature + * vector extracted from the example object. + * + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. + * @return The computed feature (in a vector). + **/ + public FeatureVector classify(int[] exampleFeatures, double[] exampleValues) { + return new FeatureVector(featureValue(exampleFeatures, exampleValues)); + } + + + /** + * Writes the algorithm's internal representation as text. In the first + * line of output, the name of the classifier is printed, followed by + * {@link #learningRate} and {@link #bias}. + * + * @param out The output stream. + **/ + public void write(PrintStream out) { + out.println(name + ": " + learningRate + ", " + bias); + if (lexicon.size() == 0) weightVector.write(out); + else weightVector.write(out, lexicon); + } + + + /** + * Writes the learned function's internal representation in binary form. + * + * @param out The output stream. + **/ + public void write(ExceptionlessOutputStream out) { + super.write(out); + out.writeDouble(learningRate); + out.writeDouble(bias); + weightVector.write(out); + } + + + /** + * Reads the binary representation of a learner with this object's run-time + * type, overwriting any and all learned or manually specified parameters + * as well as the label lexicon but without modifying the feature lexicon. + * + * @param in The input stream. + **/ + public void read(ExceptionlessInputStream in) { + super.read(in); + learningRate = in.readDouble(); + bias = in.readDouble(); + weightVector = SparseWeightVector.readWeightVector(in); + } + + + /** Returns a deep clone of this learning algorithm. */ + public Object clone() { + StochasticGradientDescentCL clone = null; + + try { + clone = (StochasticGradientDescentCL) super.clone(); + } catch (Exception e) { + System.err.println("Error cloning StochasticGradientDescentCL: " + e); + System.exit(1); + } + + clone.weightVector = (SparseWeightVector) weightVector.clone(); + return clone; + } + + + /** + * Simply a container for all of {@link StochasticGradientDescent}'s + * configurable parameters. Using instances of this class should make code + * more readable and constructors less complicated. + * + * @author Nick Rizzolo + **/ + public static class Parameters extends Learner.Parameters { + /** + * The hypothesis vector; default + * {@link StochasticGradientDescent#defaultWeightVector}. + **/ + public SparseWeightVector weightVector; + /** + * The rate at which weights are updated; default + * {@link #defaultLearningRate}. + **/ + public double learningRate; + /** + * This name of the loss function + */ + public String lossFunction; + + + /** Sets all the default values. */ + public Parameters() { + weightVector = (SparseWeightVector) defaultWeightVector.clone(); + learningRate = defaultLearningRate; + lossFunction = defaultLossFunction; + } + + + /** + * Sets the parameters from the parent's parameters object, giving + * defaults to all parameters declared in this object. + **/ + public Parameters(Learner.Parameters p) { + super(p); + weightVector = (SparseWeightVector) defaultWeightVector.clone(); + learningRate = defaultLearningRate; + lossFunction = defaultLossFunction; + } + + + /** Copy constructor. */ + public Parameters(Parameters p) { + super(p); + weightVector = p.weightVector; + learningRate = p.learningRate; + lossFunction = p.lossFunction; + } + + + /** + * Calls the appropriate Learner.setParameters(Parameters) + * method for this Parameters object. + * + * @param l The learner whose parameters will be set. + **/ + public void setParameters(Learner l) { + ((StochasticGradientDescentCL) l).setParameters(this); + } + + + /** + * Creates a string representation of these parameters in which only + * those parameters that differ from their default values are mentioned. + **/ + public String nonDefaultString() { + String result = super.nonDefaultString(); + + if (learningRate != StochasticGradientDescentCL.defaultLearningRate) + result += ", learningRate = " + learningRate; + + if (result.startsWith(", ")) result = result.substring(2); + return result; + } + } +} \ No newline at end of file From 5340690c8dbeef7fc64128481933d90b59f68790 Mon Sep 17 00:00:00 2001 From: yimingjiang Date: Sun, 12 Jun 2016 18:05:25 -0700 Subject: [PATCH 2/3] update license to pass license check on build --- .../lbjava/learn/StochasticGradientDescent.java | 11 ++++------- .../lbjava/learn/StochasticGradientDescentCL.java | 11 ++++------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java index dab7835c..4387a82d 100644 --- a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java +++ b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java @@ -1,11 +1,8 @@ /** - * This software is released under the University of Illinois/Research and - * Academic Use License. See the LICENSE file in the root folder for details. - * Copyright (c) 2016 - *

- * Developed by: - * The Cognitive Computations Group - * University of Illinois at Urbana-Champaign + * This software is released under the University of Illinois/Research and Academic Use License. See + * the LICENSE file in the root folder for details. Copyright (c) 2016 + * + * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign * http://cogcomp.cs.illinois.edu/ */ package edu.illinois.cs.cogcomp.lbjava.learn; diff --git a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java index 7b5f4913..dbb7f897 100644 --- a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java +++ b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java @@ -1,11 +1,8 @@ /** - * This software is released under the University of Illinois/Research and - * Academic Use License. See the LICENSE file in the root folder for details. - * Copyright (c) 2016 - *

- * Developed by: - * The Cognitive Computations Group - * University of Illinois at Urbana-Champaign + * This software is released under the University of Illinois/Research and Academic Use License. See + * the LICENSE file in the root folder for details. Copyright (c) 2016 + * + * Developed by: The Cognitive Computations Group, University of Illinois at Urbana-Champaign * http://cogcomp.cs.illinois.edu/ */ package edu.illinois.cs.cogcomp.lbjava.learn; From 26d067186894faa10eb08e56de26ae4cc229bebd Mon Sep 17 00:00:00 2001 From: yimingjiang Date: Mon, 13 Jun 2016 15:49:01 -0700 Subject: [PATCH 3/3] Reformatted by mvn formatter:format --- .../cs/cogcomp/lbjava/learn/Learner.java | 36 ++-- .../learn/StochasticGradientDescent.java | 185 ++++++++---------- .../learn/StochasticGradientDescentCL.java | 160 +++++++-------- 3 files changed, 180 insertions(+), 201 deletions(-) diff --git a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/Learner.java b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/Learner.java index dc331a63..ff1e5a44 100644 --- a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/Learner.java +++ b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/Learner.java @@ -124,12 +124,13 @@ public void setParameters(Parameters p) { public void setLossFlag() { lossFlag = true; } + public void unsetLossFlag() { - lossFlag=false; + lossFlag = false; } public void setCandidates(int a) { - candidates= a; + candidates = a; } /** Retrieves the parameters that are set in this learner. */ @@ -817,23 +818,24 @@ public void forget() { **/ public ScoreSet scores(Object example) { Object[] exampleArray = getExampleArray(example, false); - ScoreSet resultS = scores((int[])exampleArray[0], (double[])exampleArray[1]); + ScoreSet resultS = scores((int[]) exampleArray[0], (double[]) exampleArray[1]); if (!lossFlag) return resultS; else - return scoresAugmented(example,resultS); - } - - /** - * Update the score of each binary variable (label) based on the gold value of each example for that variable. - * When using a {@code SparseNetworkLearner} to keep the model there is an LTU for each label. - * If the gold is same as a specific label then its binary value for that label is 1 and the score for that label - * will be {@code oldScore - lossOffset}; otherwise it will be 0 and the score will be {@code oldScore + lossOffset}. - * - * @param example The object to make decisions about. - * @param resultS The original scores (see {@link #scores(Object)}). - * @return The augmented set of scores. - */ + return scoresAugmented(example, resultS); + } + + /** + * Update the score of each binary variable (label) based on the gold value of each example for + * that variable. When using a {@code SparseNetworkLearner} to keep the model there is an LTU + * for each label. If the gold is same as a specific label then its binary value for that label + * is 1 and the score for that label will be {@code oldScore - lossOffset}; otherwise it will be + * 0 and the score will be {@code oldScore + lossOffset}. + * + * @param example The object to make decisions about. + * @param resultS The original scores (see {@link #scores(Object)}). + * @return The augmented set of scores. + */ public ScoreSet scoresAugmented(Object example, ScoreSet resultS) { ScoreSet augmentedScores = new ScoreSet(); Lexicon lLexicon = getLabelLexicon(); @@ -843,7 +845,7 @@ public ScoreSet scoresAugmented(Object example, ScoreSet resultS) { double originalScore = resultS.getScore(candidate).score; double lossOffset = 1 / (double) (candidates); if (candidate.equals(gold)) - augmentedScores.put(candidate, originalScore - lossOffset); + augmentedScores.put(candidate, originalScore - lossOffset); else augmentedScores.put(candidate, originalScore + lossOffset); } diff --git a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java index 4387a82d..8f5b6507 100644 --- a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java +++ b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescent.java @@ -18,22 +18,20 @@ /** - * Gradient descent is a batch learning algorithm for function approximation - * in which the learner tries to follow the gradient of the error function to - * the solution of minimal error. This implementation is a stochastic - * approximation to gradient descent in which the approximated function is - * assumed to have linear form. + * Gradient descent is a batch learning algorithm for function approximation in which the learner + * tries to follow the gradient of the error function to the solution of minimal error. This + * implementation is a stochastic approximation to gradient descent in which the approximated + * function is assumed to have linear form. * - *

This algorithm's user-configurable parameters are stored in member - * fields of this class. They may be set via either a constructor that names - * each parameter explicitly or a constructor that takes an instance of - * {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters Parameters} as - * input. The documentation in each member field in this class indicates the - * default value of the associated parameter when using the former type of - * constructor. The documentation of the associated member field in the - * {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters Parameters} class - * indicates the default value of the parameter when using the latter type of - * constructor. + *

+ * This algorithm's user-configurable parameters are stored in member fields of this class. They may + * be set via either a constructor that names each parameter explicitly or a constructor that takes + * an instance of {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters + * Parameters} as input. The documentation in each member field in this class indicates the default + * value of the associated parameter when using the former type of constructor. The documentation of + * the associated member field in the + * {@link edu.illinois.cs.cogcomp.lbjava.learn.StochasticGradientDescent.Parameters Parameters} + * class indicates the default value of the parameter when using the latter type of constructor. * * @author Nick Rizzolo **/ @@ -41,8 +39,7 @@ public class StochasticGradientDescent extends Learner { /** Default value for {@link #learningRate}. */ public static final double defaultLearningRate = 0.1; /** Default for {@link #weightVector}. */ - public static final SparseWeightVector defaultWeightVector = - new SparseWeightVector(); + public static final SparseWeightVector defaultWeightVector = new SparseWeightVector(); /** Default loss function */ public static final String defaultLossFunction = "lms"; @@ -54,8 +51,7 @@ public class StochasticGradientDescent extends Learner { **/ protected double bias; /** - * The rate at which weights are updated; default - * {@link #defaultLearningRate}. + * The rate at which weights are updated; default {@link #defaultLearningRate}. **/ protected double learningRate; /** @@ -68,28 +64,28 @@ public class StochasticGradientDescent extends Learner { private boolean isLMS; /** - * The learning rate takes the default value, while the name of the - * classifier gets the empty string. + * The learning rate takes the default value, while the name of the classifier gets the empty + * string. **/ public StochasticGradientDescent() { this(""); } /** - * Sets the learning rate to the specified value, while the name of the - * classifier gets the empty string. + * Sets the learning rate to the specified value, while the name of the classifier gets the + * empty string. * - * @param r The desired learning rate value. + * @param r The desired learning rate value. **/ public StochasticGradientDescent(double r) { this("", r); } /** - * Initializing constructor. Sets all member variables to their associated - * settings in the {@link StochasticGradientDescent.Parameters} object. + * Initializing constructor. Sets all member variables to their associated settings in the + * {@link StochasticGradientDescent.Parameters} object. * - * @param p The settings of all parameters. + * @param p The settings of all parameters. **/ public StochasticGradientDescent(Parameters p) { this("", p); @@ -98,18 +94,17 @@ public StochasticGradientDescent(Parameters p) { /** * The learning rate takes the default value. * - * @param n The name of the classifier. + * @param n The name of the classifier. **/ public StochasticGradientDescent(String n) { this(n, defaultLearningRate); } /** - * Use this constructor to specify an alternative subclass of - * {@link SparseWeightVector}. + * Use this constructor to specify an alternative subclass of {@link SparseWeightVector}. * - * @param n The name of the classifier. - * @param r The desired learning rate value. + * @param n The name of the classifier. + * @param r The desired learning rate value. **/ public StochasticGradientDescent(String n, double r) { super(n); @@ -119,11 +114,11 @@ public StochasticGradientDescent(String n, double r) { } /** - * Initializing constructor. Sets all member variables to their associated - * settings in the {@link StochasticGradientDescent.Parameters} object. + * Initializing constructor. Sets all member variables to their associated settings in the + * {@link StochasticGradientDescent.Parameters} object. * - * @param n The name of the classifier. - * @param p The settings of all parameters. + * @param n The name of the classifier. + * @param p The settings of all parameters. **/ public StochasticGradientDescent(String n, Parameters p) { super(n); @@ -132,10 +127,9 @@ public StochasticGradientDescent(String n, Parameters p) { /** - * Sets the values of parameters that control the behavior of this learning - * algorithm. + * Sets the values of parameters that control the behavior of this learning algorithm. * - * @param p The parameters. + * @param p The parameters. **/ public void setParameters(Parameters p) { weightVector = p.weightVector; @@ -143,11 +137,9 @@ public void setParameters(Parameters p) { lossFunction = p.lossFunction; if (Objects.equals(p.lossFunction, "lms")) { isLMS = true; - } - else if (Objects.equals(p.lossFunction, "hinge")) { + } else if (Objects.equals(p.lossFunction, "hinge")) { isLMS = false; - } - else { + } else { System.out.println("Undefined loss function! lms or hinge"); System.exit(-1); } @@ -157,8 +149,8 @@ else if (Objects.equals(p.lossFunction, "hinge")) { /** * Retrieves the parameters that are set in this learner. * - * @return An object containing all the values of the parameters that - * control the behavior of this learning algorithm. + * @return An object containing all the values of the parameters that control the behavior of + * this learning algorithm. **/ public Learner.Parameters getParameters() { Parameters p = new Parameters(super.getParameters()); @@ -179,10 +171,9 @@ public double getLearningRate() { /** - * Sets the {@link #learningRate} member variable to the specified - * value. + * Sets the {@link #learningRate} member variable to the specified value. * - * @param t The new value for {@link #learningRate}. + * @param t The new value for {@link #learningRate}. **/ public void setLearningRate(double t) { learningRate = t; @@ -214,15 +205,14 @@ public String getOutputType() { /** * Trains the learning algorithm given an object as an example. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. - * @param exampleLabels The example's label(s). - * @param labelValues The labels' values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. + * @param exampleLabels The example's label(s). + * @param labelValues The labels' values. **/ - public void learn(int[] exampleFeatures, double[] exampleValues, - int[] exampleLabels, double[] labelValues) { - assert exampleLabels.length == 1 - : "Example must have a single label."; + public void learn(int[] exampleFeatures, double[] exampleValues, int[] exampleLabels, + double[] labelValues) { + assert exampleLabels.length == 1 : "Example must have a single label."; double labelValue = labelValues[0]; double wtx = weightVector.dot(exampleFeatures, exampleValues) + bias; @@ -231,8 +221,7 @@ public void learn(int[] exampleFeatures, double[] exampleValues, double multiplier = learningRate * (labelValue - wtx); weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); bias += multiplier; - } - else { + } else { if (labelValue * wtx <= 1) { double multiplier = learningRate * labelValue; weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); @@ -245,8 +234,8 @@ public void learn(int[] exampleFeatures, double[] exampleValues, /** * Since this algorithm returns a real feature, it does not return scores. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return null **/ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { @@ -255,25 +244,23 @@ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { /** - * Returns the classification of the given example as a single feature - * instead of a {@link FeatureVector}. + * Returns the classification of the given example as a single feature instead of a + * {@link FeatureVector}. * - * @param f The features array. - * @param v The values array. + * @param f The features array. + * @param v The values array. * @return The classification of the example as a feature. **/ public Feature featureValue(int[] f, double[] v) { - return - new RealPrimitiveStringFeature(containingPackage, name, "", - realValue(f, v)); + return new RealPrimitiveStringFeature(containingPackage, name, "", realValue(f, v)); } /** * Simply computes the dot product of the weight vector and the example * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return The computed real value. **/ public double realValue(int[] exampleFeatures, double[] exampleValues) { @@ -282,11 +269,11 @@ public double realValue(int[] exampleFeatures, double[] exampleValues) { /** - * Simply computes the dot product of the weight vector and the feature - * vector extracted from the example object. + * Simply computes the dot product of the weight vector and the feature vector extracted from + * the example object. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return The computed feature (in a vector). **/ public FeatureVector classify(int[] exampleFeatures, double[] exampleValues) { @@ -295,23 +282,24 @@ public FeatureVector classify(int[] exampleFeatures, double[] exampleValues) { /** - * Writes the algorithm's internal representation as text. In the first - * line of output, the name of the classifier is printed, followed by - * {@link #learningRate} and {@link #bias}. + * Writes the algorithm's internal representation as text. In the first line of output, the name + * of the classifier is printed, followed by {@link #learningRate} and {@link #bias}. * - * @param out The output stream. + * @param out The output stream. **/ public void write(PrintStream out) { out.println(name + ": " + learningRate + ", " + bias); - if (lexicon.size() == 0) weightVector.write(out); - else weightVector.write(out, lexicon); + if (lexicon.size() == 0) + weightVector.write(out); + else + weightVector.write(out, lexicon); } /** * Writes the learned function's internal representation in binary form. * - * @param out The output stream. + * @param out The output stream. **/ public void write(ExceptionlessOutputStream out) { super.write(out); @@ -322,9 +310,9 @@ public void write(ExceptionlessOutputStream out) { /** - * Reads the binary representation of a learner with this object's run-time - * type, overwriting any and all learned or manually specified parameters - * as well as the label lexicon but without modifying the feature lexicon. + * Reads the binary representation of a learner with this object's run-time type, overwriting + * any and all learned or manually specified parameters as well as the label lexicon but without + * modifying the feature lexicon. * * @param in The input stream. **/ @@ -353,21 +341,19 @@ public Object clone() { /** - * Simply a container for all of {@link StochasticGradientDescent}'s - * configurable parameters. Using instances of this class should make code - * more readable and constructors less complicated. + * Simply a container for all of {@link StochasticGradientDescent}'s configurable parameters. + * Using instances of this class should make code more readable and constructors less + * complicated. * * @author Nick Rizzolo **/ public static class Parameters extends Learner.Parameters { /** - * The hypothesis vector; default - * {@link StochasticGradientDescent#defaultWeightVector}. + * The hypothesis vector; default {@link StochasticGradientDescent#defaultWeightVector}. **/ public SparseWeightVector weightVector; /** - * The rate at which weights are updated; default - * {@link #defaultLearningRate}. + * The rate at which weights are updated; default {@link #defaultLearningRate}. **/ public double learningRate; /** @@ -385,8 +371,8 @@ public Parameters() { /** - * Sets the parameters from the parent's parameters object, giving - * defaults to all parameters declared in this object. + * Sets the parameters from the parent's parameters object, giving defaults to all + * parameters declared in this object. **/ public Parameters(Learner.Parameters p) { super(p); @@ -406,10 +392,10 @@ public Parameters(Parameters p) { /** - * Calls the appropriate Learner.setParameters(Parameters) - * method for this Parameters object. + * Calls the appropriate Learner.setParameters(Parameters) method for this + * Parameters object. * - * @param l The learner whose parameters will be set. + * @param l The learner whose parameters will be set. **/ public void setParameters(Learner l) { ((StochasticGradientDescent) l).setParameters(this); @@ -417,8 +403,8 @@ public void setParameters(Learner l) { /** - * Creates a string representation of these parameters in which only - * those parameters that differ from their default values are mentioned. + * Creates a string representation of these parameters in which only those parameters that + * differ from their default values are mentioned. **/ public String nonDefaultString() { String result = super.nonDefaultString(); @@ -426,8 +412,9 @@ public String nonDefaultString() { if (learningRate != StochasticGradientDescent.defaultLearningRate) result += ", learningRate = " + learningRate; - if (result.startsWith(", ")) result = result.substring(2); + if (result.startsWith(", ")) + result = result.substring(2); return result; } } -} \ No newline at end of file +} diff --git a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java index dbb7f897..298103d1 100644 --- a/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java +++ b/lbjava/src/main/java/edu/illinois/cs/cogcomp/lbjava/learn/StochasticGradientDescentCL.java @@ -18,8 +18,8 @@ /** * Stochastic Gradient Descent learning algorithm for classification * - * There are two user-configurable loss functions: hinge, least mean square. - * Default is least mean square, "lms". + * There are two user-configurable loss functions: hinge, least mean square. Default is least mean + * square, "lms". * * @author Yiming Jiang */ @@ -27,8 +27,7 @@ public class StochasticGradientDescentCL extends LinearThresholdUnit { /** Default value for {@link #learningRate}. */ public static final double defaultLearningRate = 0.1; /** Default for {@link #weightVector}. */ - public static final SparseWeightVector defaultWeightVector = - new SparseWeightVector(); + public static final SparseWeightVector defaultWeightVector = new SparseWeightVector(); /** Default loss function */ public static final String defaultLossFunction = "lms"; @@ -40,8 +39,7 @@ public class StochasticGradientDescentCL extends LinearThresholdUnit { **/ protected double bias; /** - * The rate at which weights are updated; default - * {@link #defaultLearningRate}. + * The rate at which weights are updated; default {@link #defaultLearningRate}. **/ protected double learningRate; /** @@ -54,28 +52,28 @@ public class StochasticGradientDescentCL extends LinearThresholdUnit { protected boolean isLMS; /** - * The learning rate takes the default value, while the name of the - * classifier gets the empty string. + * The learning rate takes the default value, while the name of the classifier gets the empty + * string. **/ public StochasticGradientDescentCL() { this(""); } /** - * Sets the learning rate to the specified value, while the name of the - * classifier gets the empty string. + * Sets the learning rate to the specified value, while the name of the classifier gets the + * empty string. * - * @param r The desired learning rate value. + * @param r The desired learning rate value. **/ public StochasticGradientDescentCL(double r) { this("", r); } /** - * Initializing constructor. Sets all member variables to their associated - * settings in the {@link StochasticGradientDescent.Parameters} object. + * Initializing constructor. Sets all member variables to their associated settings in the + * {@link StochasticGradientDescent.Parameters} object. * - * @param p The settings of all parameters. + * @param p The settings of all parameters. **/ public StochasticGradientDescentCL(Parameters p) { this("", p); @@ -84,18 +82,17 @@ public StochasticGradientDescentCL(Parameters p) { /** * The learning rate takes the default value. * - * @param n The name of the classifier. + * @param n The name of the classifier. **/ public StochasticGradientDescentCL(String n) { this(n, defaultLearningRate); } /** - * Use this constructor to specify an alternative subclass of - * {@link SparseWeightVector}. + * Use this constructor to specify an alternative subclass of {@link SparseWeightVector}. * - * @param n The name of the classifier. - * @param r The desired learning rate value. + * @param n The name of the classifier. + * @param r The desired learning rate value. **/ public StochasticGradientDescentCL(String n, double r) { super(n); @@ -105,11 +102,11 @@ public StochasticGradientDescentCL(String n, double r) { } /** - * Initializing constructor. Sets all member variables to their associated - * settings in the {@link StochasticGradientDescent.Parameters} object. + * Initializing constructor. Sets all member variables to their associated settings in the + * {@link StochasticGradientDescent.Parameters} object. * - * @param n The name of the classifier. - * @param p The settings of all parameters. + * @param n The name of the classifier. + * @param p The settings of all parameters. **/ public StochasticGradientDescentCL(String n, Parameters p) { super(n); @@ -118,10 +115,9 @@ public StochasticGradientDescentCL(String n, Parameters p) { /** - * Sets the values of parameters that control the behavior of this learning - * algorithm. + * Sets the values of parameters that control the behavior of this learning algorithm. * - * @param p The parameters. + * @param p The parameters. **/ public void setParameters(Parameters p) { weightVector = p.weightVector; @@ -129,11 +125,9 @@ public void setParameters(Parameters p) { lossFunction = p.lossFunction; if (Objects.equals(p.lossFunction, "lms")) { isLMS = true; - } - else if (Objects.equals(p.lossFunction, "hinge")) { + } else if (Objects.equals(p.lossFunction, "hinge")) { isLMS = false; - } - else { + } else { System.out.println("Undefined loss function! lms or hinge"); System.exit(-1); } @@ -143,8 +137,8 @@ else if (Objects.equals(p.lossFunction, "hinge")) { /** * Retrieves the parameters that are set in this learner. * - * @return An object containing all the values of the parameters that - * control the behavior of this learning algorithm. + * @return An object containing all the values of the parameters that control the behavior of + * this learning algorithm. **/ public Learner.Parameters getParameters() { Parameters p = new Parameters(super.getParameters()); @@ -165,10 +159,9 @@ public double getLearningRate() { /** - * Sets the {@link #learningRate} member variable to the specified - * value. + * Sets the {@link #learningRate} member variable to the specified value. * - * @param t The new value for {@link #learningRate}. + * @param t The new value for {@link #learningRate}. **/ public void setLearningRate(double t) { learningRate = t; @@ -202,21 +195,19 @@ public void demote(int[] exampleFeatures, double[] exampleValues, double rate) { /** * Trains the learning algorithm given an object as an example. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. - * @param exampleLabels The example's label(s). - * @param labelValues The labels' values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. + * @param exampleLabels The example's label(s). + * @param labelValues The labels' values. **/ - public void learn(int[] exampleFeatures, double[] exampleValues, - int[] exampleLabels, double[] labelValues) { - assert exampleLabels.length == 1 - : "Example must have a single label."; + public void learn(int[] exampleFeatures, double[] exampleValues, int[] exampleLabels, + double[] labelValues) { + assert exampleLabels.length == 1 : "Example must have a single label."; double labelValue = 1; if (exampleLabels[0] == 1) { labelValue = 1; - } - else if (exampleLabels[0] == 0) { + } else if (exampleLabels[0] == 0) { labelValue = -1; } @@ -230,8 +221,7 @@ void learnUpdate(int[] exampleFeatures, double[] exampleValues, double labelValu double multiplier = learningRate * (labelValue - wtx); weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); bias += multiplier; - } - else { + } else { if (labelValue * wtx <= 1) { double multiplier = learningRate * labelValue; weightVector.scaledAdd(exampleFeatures, exampleValues, multiplier); @@ -244,8 +234,8 @@ void learnUpdate(int[] exampleFeatures, double[] exampleValues, double labelValu /** * Since this algorithm returns a real feature, it does not return scores. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return null **/ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { @@ -254,11 +244,11 @@ public ScoreSet scores(int[] exampleFeatures, double[] exampleValues) { /** - * Returns the classification of the given example as a single feature - * instead of a {@link FeatureVector}. + * Returns the classification of the given example as a single feature instead of a + * {@link FeatureVector}. * - * @param f The features array. - * @param v The values array. + * @param f The features array. + * @param v The values array. * @return The classification of the example as a feature. **/ public Feature featureValue(int[] f, double[] v) { @@ -270,8 +260,8 @@ public Feature featureValue(int[] f, double[] v) { /** * Simply computes the dot product of the weight vector and the example * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return The computed real value. **/ public double score(int[] exampleFeatures, double[] exampleValues) { @@ -280,11 +270,11 @@ public double score(int[] exampleFeatures, double[] exampleValues) { /** - * Simply computes the dot product of the weight vector and the feature - * vector extracted from the example object. + * Simply computes the dot product of the weight vector and the feature vector extracted from + * the example object. * - * @param exampleFeatures The example's array of feature indices. - * @param exampleValues The example's array of feature values. + * @param exampleFeatures The example's array of feature indices. + * @param exampleValues The example's array of feature values. * @return The computed feature (in a vector). **/ public FeatureVector classify(int[] exampleFeatures, double[] exampleValues) { @@ -293,23 +283,24 @@ public FeatureVector classify(int[] exampleFeatures, double[] exampleValues) { /** - * Writes the algorithm's internal representation as text. In the first - * line of output, the name of the classifier is printed, followed by - * {@link #learningRate} and {@link #bias}. + * Writes the algorithm's internal representation as text. In the first line of output, the name + * of the classifier is printed, followed by {@link #learningRate} and {@link #bias}. * - * @param out The output stream. + * @param out The output stream. **/ public void write(PrintStream out) { out.println(name + ": " + learningRate + ", " + bias); - if (lexicon.size() == 0) weightVector.write(out); - else weightVector.write(out, lexicon); + if (lexicon.size() == 0) + weightVector.write(out); + else + weightVector.write(out, lexicon); } /** * Writes the learned function's internal representation in binary form. * - * @param out The output stream. + * @param out The output stream. **/ public void write(ExceptionlessOutputStream out) { super.write(out); @@ -320,9 +311,9 @@ public void write(ExceptionlessOutputStream out) { /** - * Reads the binary representation of a learner with this object's run-time - * type, overwriting any and all learned or manually specified parameters - * as well as the label lexicon but without modifying the feature lexicon. + * Reads the binary representation of a learner with this object's run-time type, overwriting + * any and all learned or manually specified parameters as well as the label lexicon but without + * modifying the feature lexicon. * * @param in The input stream. **/ @@ -351,21 +342,19 @@ public Object clone() { /** - * Simply a container for all of {@link StochasticGradientDescent}'s - * configurable parameters. Using instances of this class should make code - * more readable and constructors less complicated. + * Simply a container for all of {@link StochasticGradientDescent}'s configurable parameters. + * Using instances of this class should make code more readable and constructors less + * complicated. * * @author Nick Rizzolo **/ public static class Parameters extends Learner.Parameters { /** - * The hypothesis vector; default - * {@link StochasticGradientDescent#defaultWeightVector}. + * The hypothesis vector; default {@link StochasticGradientDescent#defaultWeightVector}. **/ public SparseWeightVector weightVector; /** - * The rate at which weights are updated; default - * {@link #defaultLearningRate}. + * The rate at which weights are updated; default {@link #defaultLearningRate}. **/ public double learningRate; /** @@ -383,8 +372,8 @@ public Parameters() { /** - * Sets the parameters from the parent's parameters object, giving - * defaults to all parameters declared in this object. + * Sets the parameters from the parent's parameters object, giving defaults to all + * parameters declared in this object. **/ public Parameters(Learner.Parameters p) { super(p); @@ -404,10 +393,10 @@ public Parameters(Parameters p) { /** - * Calls the appropriate Learner.setParameters(Parameters) - * method for this Parameters object. + * Calls the appropriate Learner.setParameters(Parameters) method for this + * Parameters object. * - * @param l The learner whose parameters will be set. + * @param l The learner whose parameters will be set. **/ public void setParameters(Learner l) { ((StochasticGradientDescentCL) l).setParameters(this); @@ -415,8 +404,8 @@ public void setParameters(Learner l) { /** - * Creates a string representation of these parameters in which only - * those parameters that differ from their default values are mentioned. + * Creates a string representation of these parameters in which only those parameters that + * differ from their default values are mentioned. **/ public String nonDefaultString() { String result = super.nonDefaultString(); @@ -424,8 +413,9 @@ public String nonDefaultString() { if (learningRate != StochasticGradientDescentCL.defaultLearningRate) result += ", learningRate = " + learningRate; - if (result.startsWith(", ")) result = result.substring(2); + if (result.startsWith(", ")) + result = result.substring(2); return result; } } -} \ No newline at end of file +}