public abstract class LinearThresholdUnit extends Learner
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(int[],double[],double)
, and demote(int[],double[],double)
methods,
assuming the score returned by the score(Object)
method need only be compared with
threshold
to make a prediction. Otherwise, the Learner.classify(Object)
method also
needs to be overridden. If the algorithm is not mistake driven, the Learner.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.
Modifier and Type | Class and Description |
---|---|
static class |
LinearThresholdUnit.Parameters
Simply a container for all of
LinearThresholdUnit 's configurable parameters. |
Modifier and Type | Field and Description |
---|---|
protected 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 |
defaultLearningRate
Default value for
learningRate . |
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 |
learningRate
The rate at which weights are updated; default
defaultLearningRate . |
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.
|
candidates, encoding, extractor, labeler, labelLexicon, lcFilePath, lexFilePath, lexicon, lossFlag, predictions, readLexiconOnDemand
containingPackage, name
Modifier | Constructor and Description |
---|---|
|
LinearThresholdUnit()
Default constructor.
|
|
LinearThresholdUnit(double r)
Initializing constructor.
|
|
LinearThresholdUnit(double r,
double t)
Sets the learning rate and threshold to the specified values, while the name of the
classifier gets the empty string.
|
|
LinearThresholdUnit(double r,
double t,
double pt)
Use this constructor to fit a thick separator, where both the positive and negative sides of
the hyperplane will be given the specified thickness, while the name of the classifier gets
the empty string.
|
|
LinearThresholdUnit(double r,
double t,
double pt,
double nt)
Use this constructor to fit a thick separator, where the positive and negative sides of the
hyperplane will be given the specified separate thicknesses, while the name of the classifier
gets the empty string.
|
protected |
LinearThresholdUnit(LinearThresholdUnit.Parameters p)
Initializing constructor.
|
protected |
LinearThresholdUnit(String n)
Initializing constructor.
|
protected |
LinearThresholdUnit(String n,
double r)
Default constructor.
|
protected |
LinearThresholdUnit(String n,
double r,
double t)
Initializing constructor.
|
protected |
LinearThresholdUnit(String n,
double r,
double t,
double pt)
Initializing constructor.
|
protected |
LinearThresholdUnit(String n,
double r,
double t,
double pt,
double nt)
Initializing constructor.
|
protected |
LinearThresholdUnit(String n,
double r,
double t,
double pt,
double nt,
SparseWeightVector v)
Initializing constructor.
|
protected |
LinearThresholdUnit(String n,
LinearThresholdUnit.Parameters p)
Initializing constructor.
|
Modifier and Type | Method and Description |
---|---|
String[] |
allowableValues()
Returns the array of allowable values that a feature returned by this classifier may take.
|
FeatureVector |
classify(int[] exampleFeatures,
double[] exampleValues)
The default evaluation method simply computes the score for the example and returns a
DiscretePrimitiveStringFeature 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. |
Object |
clone()
Returns a deep clone of this learning algorithm.
|
double |
computeLearningRate(int[] exampleFeatures,
double[] exampleValues,
double s,
boolean label)
Computes the value of the
learningRate variable if needed and returns the value. |
abstract void |
demote(int[] exampleFeatures,
double[] exampleValues,
double rate)
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. |
String |
discreteValue(int[] exampleFeatures,
double[] exampleValues)
The default evaluation method simply computes the score for the example and returns a
DiscretePrimitiveStringFeature 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. |
Feature |
featureValue(int[] f,
double[] v)
Returns the classification of the given example as a single feature instead of a
FeatureVector . |
void |
forget()
Resets the weight vector to associate the default weight with all features.
|
String[] |
getAllowableValues() |
double |
getBias() |
double |
getInitialWeight()
Returns the current value of the
initialWeight variable. |
double |
getNegativeThickness()
Returns the current value of the
negativeThickness variable. |
Learner.Parameters |
getParameters()
Retrieves the parameters that are set in this learner.
|
double |
getPositiveThickness()
Returns the current value of the
positiveThickness variable. |
double |
getThreshold()
Returns the current value of the
threshold variable. |
SparseWeightVector |
getWeightVector() |
void |
initialize(int numExamples,
int numFeatures)
Initializes the weight vector array to the size of the specified number of features, setting
each weight equal to
initialWeight . |
void |
learn(int[] exampleFeatures,
double[] exampleValues,
int[] exampleLabels,
double[] labelValues)
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(int[] exampleFeatures,
double[] exampleValues,
double rate)
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. |
void |
read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
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.
|
double |
score(int[] exampleFeatures,
double[] exampleValues)
Computes the score for the specified example vector which will be thresholded to make the
binary classification.
|
double |
score(Object example)
Computes the score for the specified example vector which will be thresholded to make the
binary classification.
|
ScoreSet |
scores(int[] exampleFeatures,
double[] exampleValues)
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 |
setParameters(LinearThresholdUnit.Parameters p)
Sets the values of parameters that control the behavior of this learning algorithm.
|
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. |
boolean |
shouldDemote(boolean label,
double s,
double threshold,
double negativeThickness)
Determines if the weights should be demoted
|
boolean |
shouldPromote(boolean label,
double s,
double threshold,
double positiveThickness)
Determines if the weights should be promoted
|
void |
write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
Writes the learned function's internal representation in binary form.
|
classify, classify, classify, classify, countFeatures, createPrediction, createPrediction, demandLexicon, discreteValue, discreteValue, doneLearning, doneWithRound, emptyClone, featureValue, featureValue, getCurrentLexicon, getExampleArray, getExampleArray, getExtractor, getLabeler, getLabelLexicon, getLexicon, getLexiconDiscardCounts, getLexiconLocation, getModelLocation, getPrunedLexiconSize, learn, learn, learn, learn, read, readLabelLexicon, readLearner, readLearner, readLearner, readLearner, readLearner, readLearner, readLexicon, readLexicon, readLexiconOnDemand, readLexiconOnDemand, readModel, readModel, readParameters, realValue, realValue, realValue, save, saveLexicon, saveModel, scores, scores, scoresAugmented, setCandidates, setEncoding, setExtractor, setLabelLexicon, setLexicon, setLexiconLocation, setLexiconLocation, setLossFlag, setModelLocation, setModelLocation, setParameters, setReadLexiconOnDemand, unclone, unsetLossFlag, write, write, writeLexicon, writeModel, writeParameters
classify, discreteValueArray, getCompositeChildren, getInputType, getOutputType, realValueArray, test, toString, valueIndexOf
public static final double defaultInitialWeight
initialWeight
.public static final double defaultThreshold
threshold
.public static final double defaultThickness
positiveThickness
.public static final double defaultLearningRate
learningRate
.public static final SparseWeightVector defaultWeightVector
weightVector
.protected double learningRate
defaultLearningRate
.protected SparseWeightVector weightVector
protected double initialWeight
defaultInitialWeight
.protected double threshold
defaultThreshold
.protected double bias
protected double positiveThickness
defaultThickness
.protected double negativeThickness
positiveThickness
.protected String[] allowableValues
public LinearThresholdUnit()
public LinearThresholdUnit(double r)
r
- The desired learning rate.public LinearThresholdUnit(double r, double t)
r
- The desired learning rate value.t
- The desired threshold value.public LinearThresholdUnit(double r, double t, double pt)
r
- The desired learning rate value.t
- The desired threshold value.pt
- The desired thickness.public LinearThresholdUnit(double r, double t, double pt, double nt)
r
- The desired learning rate value.t
- The desired threshold value.pt
- The desired positive thickness.nt
- The desired negative thickness.protected LinearThresholdUnit(String n)
n
- The name of the classifier.protected LinearThresholdUnit(String n, double r)
n
- The name of the classifier.r
- The desired learning rate.protected LinearThresholdUnit(String n, double r, double t)
n
- The name of the classifier.r
- The desired learning rate.t
- The desired value for the threshold.protected LinearThresholdUnit(String n, double r, double t, double pt)
n
- The name of the classifier.r
- The desired learning rate.t
- The desired value for the threshold.pt
- The desired thickness.protected LinearThresholdUnit(String n, double r, double t, double pt, double nt)
n
- The name of the classifier.r
- The desired learning rate.t
- The desired value for the threshold.pt
- The desired positive thickness.nt
- The desired negative thickness.protected LinearThresholdUnit(String n, double r, double t, double pt, double nt, SparseWeightVector v)
n
- The name of the classifier.r
- The desired learning rate.t
- The desired value for the threshold.pt
- The desired positive thickness.nt
- The desired negative thickness.v
- An initial weight vector.protected LinearThresholdUnit(LinearThresholdUnit.Parameters p)
LinearThresholdUnit.Parameters
object. The name of the classifier is the empty
string.p
- The settings of all parameters.protected LinearThresholdUnit(String n, LinearThresholdUnit.Parameters p)
LinearThresholdUnit.Parameters
object.n
- The name of the classifier.p
- The settings of all parameters.public SparseWeightVector getWeightVector()
public double getBias()
public String[] getAllowableValues()
public void setParameters(LinearThresholdUnit.Parameters p)
p
- The parameters.public Learner.Parameters getParameters()
getParameters
in class Learner
public void setLabeler(Classifier l)
setLabeler
in class Learner
l
- A new label producing classifier.public double getInitialWeight()
initialWeight
variable.initialWeight
variable.public void setInitialWeight(double w)
initialWeight
member variable to the specified value.w
- The new value for initialWeight
.public double getThreshold()
threshold
variable.threshold
variable.public void setThreshold(double t)
threshold
member variable to the specified value.t
- The new value for threshold
.public double getPositiveThickness()
positiveThickness
variable.positiveThickness
variable.public void setPositiveThickness(double t)
positiveThickness
member variable to the specified value.t
- The new value for positiveThickness
.public double getNegativeThickness()
negativeThickness
variable.negativeThickness
variable.public void setNegativeThickness(double t)
negativeThickness
member variable to the specified value.t
- The new value for negativeThickness
.public void setThickness(double t)
positiveThickness
and negativeThickness
member variables to the
specified value.t
- The new thickness value.public String[] allowableValues()
allowableValues
in class Classifier
{ "*", "*" }
are 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.public void learn(int[] exampleFeatures, double[] exampleValues, int[] exampleLabels, double[] labelValues)
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 Learner.classify(Object)
; it calls score(Object)
directly.
public double computeLearningRate(int[] exampleFeatures, double[] exampleValues, double s, boolean label)
learningRate
variable if needed and returns the value. By
default, the current value of learningRate
is returned.exampleFeatures
- The example's array of feature indicesexampleValues
- The example's array of feature valuess
- The score of the example objectlabel
- The label of the example objectlearningRate
variablepublic boolean shouldPromote(boolean label, double s, double threshold, double positiveThickness)
label
- The label of the example objects
- The score of the example objectthreshold
- The LTU thresholdpositiveThickness
- The thickness of the hyperplane on the positive sidepublic boolean shouldDemote(boolean label, double s, double threshold, double negativeThickness)
label
- The label of the example objects
- The score of the example objectthreshold
- The LTU thresholdnegativeThickness
- The thickness of the hyperplane on the negative sidepublic void initialize(int numExamples, int numFeatures)
initialWeight
.initialize
in class Learner
numExamples
- The number of examples that will be observed during training.numFeatures
- The number of features that will be observed during training.public ScoreSet scores(int[] exampleFeatures, double[] exampleValues)
score(Object)
minus the threshold
, and the score for the negative
classification is the opposite of the positive classification's score.public Feature featureValue(int[] f, double[] v)
FeatureVector
.featureValue
in class Learner
f
- The features array.v
- The values array.public String discreteValue(int[] exampleFeatures, double[] exampleValues)
DiscretePrimitiveStringFeature
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.discreteValue
in class Learner
exampleFeatures
- The example's array of feature indicesexampleValues
- The example's array of feature valuespublic FeatureVector classify(int[] exampleFeatures, double[] exampleValues)
DiscretePrimitiveStringFeature
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.public double score(Object example)
example
- The example object.public double score(int[] exampleFeatures, double[] exampleValues)
exampleFeatures
- The example's array of feature indicesexampleValues
- The example's array of feature valuespublic void forget()
public abstract void promote(int[] exampleFeatures, double[] exampleValues, double rate)
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.exampleFeatures
- The example's array of feature indicesexampleValues
- The example's array of feature valuesrate
- The learning rate at which the weights are updated.public abstract void demote(int[] exampleFeatures, double[] exampleValues, double rate)
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.exampleFeatures
- The example's array of feature indicesexampleValues
- The example's array of feature valuesrate
- The learning rate at which the weights are updated.public void write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
public void read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
Copyright © 2016. All rights reserved.