LBJ2.learn
Class LinearThresholdUnit

java.lang.Object
  extended by LBJ2.classify.Classifier
      extended by LBJ2.learn.Learner
          extended by LBJ2.learn.LinearThresholdUnit
All Implemented Interfaces:
java.io.Serializable, java.lang.Cloneable
Direct Known Subclasses:
SparsePerceptron, SparseWinnow

public abstract class LinearThresholdUnit
extends Learner

A LinearThresholdUnit is a Learner for binary classification in which a score is computed as a linear function a weight vector and the input example, and the decision is made by comparing the score to some threshold quantity. Deriving a linear threshold algorithm from this class gives the programmer more flexible access to the score it computes as well as its promotion and demotion methods (if it's on-line).

On-line, mistake driven algorithms derived from this class need only override the promote(Object), and demote(Object) methods, assuming the score returned by the score(Object) method need only be compared with threshold to make a prediction. Otherwise, the classify(Object) method also needs to be overridden. If the algorithm is not mistake driven, the learn(Object) method needs to be overridden as well.

It is assumed that Learner.labeler is a single discrete classifier that produces the same feature for every example object and that the values that feature may take are available through the Classifier.allowableValues() method. The first value returned from Classifier.allowableValues() is treated as "negative", and it is assumed there are exactly 2 allowable values. Assertions will produce error messages if these assumptions do not hold.

Fitting a "thick separator" instead of just a hyperplane is also supported through this class.

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 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 Parameters class indicates the default value of the parameter when using the latter type of constructor.

See Also:
Serialized Form

Nested Class Summary
static class LinearThresholdUnit.Parameters
          Simply a container for all of LinearThresholdUnit's configurable parameters.
 
Field Summary
protected  java.lang.String[] allowableValues
          The label producing classifier's allowable values.
protected  double bias
          The bias is stored here rather than as an element of the weight vector.
static double defaultInitialWeight
          Default for initialWeight.
static double defaultThickness
          Default for positiveThickness.
static double defaultThreshold
          Default for threshold.
static SparseWeightVector defaultWeightVector
          Default for weightVector.
protected  double initialWeight
          The weight associated with a feature when first added to the vector; default defaultInitialWeight.
protected  double negativeThickness
          The thickness of the hyperplane on the negative side; default equal to positiveThickness.
protected  double positiveThickness
          The thickness of the hyperplane on the positive side; default defaultThickness.
protected  double threshold
          The score is compared against this value to make predictions; default defaultThreshold.
protected  SparseWeightVector weightVector
          The LTU's weight vector; default is an empty vector.
 
Fields inherited from class LBJ2.learn.Learner
extractor, labeler
 
Fields inherited from class LBJ2.classify.Classifier
containingPackage, name
 
Constructor Summary
protected LinearThresholdUnit(java.lang.String n)
          Default constructor.
protected LinearThresholdUnit(java.lang.String n, double t)
          Initializing constructor.
protected LinearThresholdUnit(java.lang.String n, double t, double pt)
          Initializing constructor.
protected LinearThresholdUnit(java.lang.String n, double t, double pt, double nt)
          Initializing constructor.
protected LinearThresholdUnit(java.lang.String n, double t, double pt, double nt, SparseWeightVector v)
          Initializing constructor.
protected LinearThresholdUnit(java.lang.String n, LinearThresholdUnit.Parameters p)
          Initializing constructor.
 
Method Summary
 java.lang.String[] allowableValues()
          Returns the array of allowable values that a feature returned by this classifier may take.
 FeatureVector classify(java.lang.Object example)
          The default evaluation method simply computes the score for the example and returns a DiscreteFeature set to either the second value from the label classifier's array of allowable values if the score is greater than or equal to threshold or the first otherwise.
abstract  void demote(java.lang.Object example)
          If the LinearThresholdUnit is mistake driven, this method should be overridden and used to update the internal representation when a mistake is made on a negative example.
 void forget()
          Resets the weight vector to associate the default weight with all features.
 double getInitialWeight()
          Returns the current value of the initialWeight variable.
 double getNegativeThickness()
          Returns the current value of the negativeThickness variable.
 double getPositiveThickness()
          Returns the current value of the positiveThickness variable.
 double getThreshold()
          Returns the current value of the threshold variable.
 void learn(java.lang.Object example)
          The default training algorithm for a linear threshold unit consists of evaluating the example object with the score(Object) method and threshold, checking the result of evaluation against the label, and, if they are different, promoting when the label is positive or demoting when the label is negative.
abstract  void promote(java.lang.Object example)
          If the LinearThresholdUnit is mistake driven, this method should be overridden and used to update the internal representation when a mistake is made on a positive example.
 double score(java.lang.Object example)
          Computes the score for the specified example vector which will be thresholded to make the binary classification.
 ScoreSet scores(java.lang.Object example)
          An LTU returns two scores; one for the negative classification and one for the positive classification.
 void setInitialWeight(double w)
          Sets the initialWeight member variable to the specified value.
 void setLabeler(Classifier l)
          Sets the labels list.
 void setNegativeThickness(double t)
          Sets the negativeThickness member variable to the specified value.
 void setPositiveThickness(double t)
          Sets the positiveThickness member variable to the specified value.
 void setThickness(double t)
          Sets the positiveThickness and negativeThickness member variables to the specified value.
 void setThreshold(double t)
          Sets the threshold member variable to the specified value.
 
Methods inherited from class LBJ2.learn.Learner
doneLearning, getExtractor, getLabeler, learn, save, setExtractor, write
 
Methods inherited from class LBJ2.classify.Classifier
binaryRead, binaryRead, binaryRead, binaryRead, binaryWrite, binaryWrite, classify, clone, discreteValue, discreteValueArray, getCompositeChildren, getInputType, getOutputType, realValue, realValueArray, test, toString, valueIndexOf
 
Methods inherited from class java.lang.Object
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

defaultInitialWeight

public static final double defaultInitialWeight
Default for initialWeight.

See Also:
Constant Field Values

defaultThreshold

public static final double defaultThreshold
Default for threshold.

See Also:
Constant Field Values

defaultThickness

public static final double defaultThickness
Default for positiveThickness.

See Also:
Constant Field Values

defaultWeightVector

public static final SparseWeightVector defaultWeightVector
Default for weightVector.


weightVector

protected SparseWeightVector weightVector
The LTU's weight vector; default is an empty vector.


initialWeight

protected double initialWeight
The weight associated with a feature when first added to the vector; default defaultInitialWeight.


threshold

protected double threshold
The score is compared against this value to make predictions; default defaultThreshold.


bias

protected double bias
The bias is stored here rather than as an element of the weight vector.


positiveThickness

protected double positiveThickness
The thickness of the hyperplane on the positive side; default defaultThickness.


negativeThickness

protected double negativeThickness
The thickness of the hyperplane on the negative side; default equal to positiveThickness.


allowableValues

protected java.lang.String[] allowableValues
The label producing classifier's allowable values.

Constructor Detail

LinearThresholdUnit

protected LinearThresholdUnit(java.lang.String n)
Default constructor. Sets the threshold, positive thickness, and negative thickness to their default values.

Parameters:
n - The name of the classifier.

LinearThresholdUnit

protected LinearThresholdUnit(java.lang.String n,
                              double t)
Initializing constructor. Sets the threshold to the specified value, while the positive and negative thicknesses get their defaults.

Parameters:
n - The name of the classifier.
t - The desired value for the threshold.

LinearThresholdUnit

protected LinearThresholdUnit(java.lang.String n,
                              double t,
                              double pt)
Initializing constructor. Sets the threshold and positive thickness to the specified values, and the negative thickness is set to the same value as the positive thickness.

Parameters:
n - The name of the classifier.
t - The desired value for the threshold.
pt - The desired thickness.

LinearThresholdUnit

protected LinearThresholdUnit(java.lang.String n,
                              double t,
                              double pt,
                              double nt)
Initializing constructor. Sets the threshold, positive thickness, and negative thickness to the specified values.

Parameters:
n - The name of the classifier.
t - The desired value for the threshold.
pt - The desired positive thickness.
nt - The desired negative thickness.

LinearThresholdUnit

protected LinearThresholdUnit(java.lang.String n,
                              double t,
                              double pt,
                              double nt,
                              SparseWeightVector v)
Initializing constructor. Sets the threshold, positive thickness, and negative thickness to the specified values.

Parameters:
n - The name of the classifier.
t - The desired value for the threshold.
pt - The desired positive thickness.
nt - The desired negative thickness.
v - An initial weight vector.

LinearThresholdUnit

protected LinearThresholdUnit(java.lang.String n,
                              LinearThresholdUnit.Parameters p)
Initializing constructor. Sets all member variables to their associated settings in the LinearThresholdUnit.Parameters object.

Parameters:
n - The name of the classifier.
p - The settings of all parameters.
Method Detail

setLabeler

public void setLabeler(Classifier l)
Sets the labels list.

Overrides:
setLabeler in class Learner
Parameters:
l - A new label producing classifier.

getInitialWeight

public double getInitialWeight()
Returns the current value of the initialWeight variable.

Returns:
The value of the initialWeight variable.

setInitialWeight

public void setInitialWeight(double w)
Sets the initialWeight member variable to the specified value.

Parameters:
w - The new value for initialWeight.

getThreshold

public double getThreshold()
Returns the current value of the threshold variable.

Returns:
The value of the threshold variable.

setThreshold

public void setThreshold(double t)
Sets the threshold member variable to the specified value.

Parameters:
t - The new value for threshold.

getPositiveThickness

public double getPositiveThickness()
Returns the current value of the positiveThickness variable.

Returns:
The value of the positiveThickness variable.

setPositiveThickness

public void setPositiveThickness(double t)
Sets the positiveThickness member variable to the specified value.

Parameters:
t - The new value for positiveThickness.

getNegativeThickness

public double getNegativeThickness()
Returns the current value of the negativeThickness variable.

Returns:
The value of the negativeThickness variable.

setNegativeThickness

public void setNegativeThickness(double t)
Sets the negativeThickness member variable to the specified value.

Parameters:
t - The new value for negativeThickness.

setThickness

public void setThickness(double t)
Sets the positiveThickness and negativeThickness member variables to the specified value.

Parameters:
t - The new thickness value.

allowableValues

public java.lang.String[] allowableValues()
Returns the array of allowable values that a feature returned by this classifier may take.

Overrides:
allowableValues in class Classifier
Returns:
If a labeler has not yet been established for this LTU, new String[]{ "*", "*" } is returned, which indicates to the compiler that classifiers derived from this learner will return features that take one of two values that are specified in the source code. Otherwise, the allowable values of the labeler are returned.

learn

public void learn(java.lang.Object example)
The default training algorithm for a linear threshold unit consists of evaluating the example object with the score(Object) method and threshold, checking the result of evaluation against the label, and, if they are different, promoting when the label is positive or demoting when the label is negative.

This method does not call classify(Object); it calls score(Object) directly.

Specified by:
learn in class Learner
Parameters:
example - The example object.

scores

public ScoreSet scores(java.lang.Object example)
An LTU returns two scores; one for the negative classification and one for the positive classification. By default, the score for the positive classification is the result of score(Object) minus the threshold, and the score for the negative classification is the opposite of the positive classification's score.

Specified by:
scores in class Learner
Parameters:
example - The object to make decisions about.
Returns:
Two scores as described above.

classify

public FeatureVector classify(java.lang.Object example)
The default evaluation method simply computes the score for the example and returns a DiscreteFeature set to either the second value from the label classifier's array of allowable values if the score is greater than or equal to threshold or the first otherwise.

Specified by:
classify in class Classifier
Parameters:
example - The example to be evaluated.
Returns:
The computed feature (in a vector).

score

public double score(java.lang.Object example)
Computes the score for the specified example vector which will be thresholded to make the binary classification.

Parameters:
example - The example object.
Returns:
The score for the given example vector.

forget

public void forget()
Resets the weight vector to associate the default weight with all features.

Specified by:
forget in class Learner

promote

public abstract void promote(java.lang.Object example)
If the LinearThresholdUnit is mistake driven, this method should be overridden and used to update the internal representation when a mistake is made on a positive example.

Parameters:
example - The example object.

demote

public abstract void demote(java.lang.Object example)
If the LinearThresholdUnit is mistake driven, this method should be overridden and used to update the internal representation when a mistake is made on a negative example.

Parameters:
example - The example object.