public class SparseAveragedPerceptron extends SparsePerceptron
During training, after each example ei is processed, the weight vector wi becomes the active weight vector used to make predictions on future training examples. If a mistake was made on ei, wi will be different than wi - 1. Otherwise, it will remain unchanged.
After training, each distinct weight vector arrived at during training is associated with an
integer weight equal to the number of examples whose training made that weight vector active. A
new weight vector w* is computed by taking the average of all these weight
vectors weighted as described. w* is used to make all predictions returned to
the user through methods such as Classifier.classify(Object)
or
Classifier.discreteValue(Object)
.
The above description is a useful way to think about the operation of this Learner
.
However, the user should note that this implementation never explicitly stores
w*. Instead, it is computed efficiently on demand. Thus, interspersed online
training and evaluation is efficient and operates as expected.
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 second value returned from
Classifier.allowableValues()
is treated as "positive", and it is assumed there are
exactly 2 allowable values. Assertions will produce error messages if these assumptions do not
hold.
Modifier and Type | Class and Description |
---|---|
static class |
SparseAveragedPerceptron.AveragedWeightVector
This implementation of a sparse weight vector associates two
double s with each
Feature . |
static class |
SparseAveragedPerceptron.Parameters
Simply a container for all of
SparseAveragedPerceptron 's configurable parameters. |
Modifier and Type | Field and Description |
---|---|
protected double |
averagedBias
Keeps the extra information necessary to compute the averaged bias.
|
protected SparseAveragedPerceptron.AveragedWeightVector |
awv
Holds the same reference as
LinearThresholdUnit.weightVector casted to
SparseAveragedPerceptron.AveragedWeightVector . |
static SparseAveragedPerceptron.AveragedWeightVector |
defaultWeightVector
Default for
LinearThresholdUnit.weightVector . |
allowableValues, bias, defaultInitialWeight, defaultLearningRate, defaultThickness, defaultThreshold, initialWeight, learningRate, negativeThickness, positiveThickness, threshold, weightVector
candidates, encoding, extractor, labeler, labelLexicon, lcFilePath, lexFilePath, lexicon, lossFlag, predictions, readLexiconOnDemand
containingPackage, name
Constructor and Description |
---|
SparseAveragedPerceptron()
The learning rate and threshold take default values, while the name of the classifier gets
the empty string.
|
SparseAveragedPerceptron(double r)
Sets the learning rate to the specified value, and the threshold takes the default, while the
name of the classifier gets the empty string.
|
SparseAveragedPerceptron(double r,
double t)
Sets the learning rate and threshold to the specified values, while the name of the
classifier gets the empty string.
|
SparseAveragedPerceptron(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.
|
SparseAveragedPerceptron(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.
|
SparseAveragedPerceptron(SparseAveragedPerceptron.Parameters p)
Initializing constructor.
|
SparseAveragedPerceptron(String n)
The learning rate and threshold take default values.
|
SparseAveragedPerceptron(String n,
double r)
Sets the learning rate to the specified value, and the threshold takes the default.
|
SparseAveragedPerceptron(String n,
double r,
double t)
Sets the learning rate and threshold to the specified values.
|
SparseAveragedPerceptron(String n,
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.
|
SparseAveragedPerceptron(String n,
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.
|
SparseAveragedPerceptron(String n,
SparseAveragedPerceptron.Parameters p)
Initializing constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
demote(int[] exampleFeatures,
double[] exampleValues,
double rate)
Scales the feature vector produced by the extractor by the learning rate and subtracts it
from the weight vector.
|
void |
forget()
Resets the weight vector to all zeros.
|
Learner.Parameters |
getParameters()
Retrieves the parameters that are set in this learner.
|
void |
initialize(int numExamples,
int numFeatures)
Initializes the weight vector array to the size of the supplied number of features, with each
cell taking the default value of
LinearThresholdUnit.initialWeight . |
void |
learn(int[] exampleFeatures,
double[] exampleValues,
int[] exampleLabels,
double[] labelValues)
This method works just like
LinearThresholdUnit.learn(int[],double[],int[],double[]) ,
except it notifies its weight vector when it got an example correct in addition to updating
it when it makes a mistake. |
void |
promote(int[] exampleFeatures,
double[] exampleValues,
double rate)
Scales the feature vector produced by the extractor by the learning rate and adds it to the
weight vector.
|
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)
The score of the specified object is equal to
w * x + bias where *
is dot product, w is the weight vector, and x is the feature vector
produced by the extractor. |
void |
setParameters(SparseAveragedPerceptron.Parameters p)
Sets the values of parameters that control the behavior of this learning algorithm.
|
void |
write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
Writes the learned function's internal representation in binary form.
|
void |
write(PrintStream out)
Writes the algorithm's internal representation as text.
|
getLearningRate, setLearningRate
allowableValues, classify, clone, computeLearningRate, discreteValue, featureValue, getAllowableValues, getBias, getInitialWeight, getNegativeThickness, getPositiveThickness, getThreshold, getWeightVector, score, scores, setInitialWeight, setLabeler, setNegativeThickness, setParameters, setPositiveThickness, setThickness, setThreshold, shouldDemote, shouldPromote
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, writeLexicon, writeModel, writeParameters
classify, discreteValueArray, getCompositeChildren, getInputType, getOutputType, realValueArray, test, toString, valueIndexOf
public static final SparseAveragedPerceptron.AveragedWeightVector defaultWeightVector
LinearThresholdUnit.weightVector
.protected SparseAveragedPerceptron.AveragedWeightVector awv
LinearThresholdUnit.weightVector
casted to
SparseAveragedPerceptron.AveragedWeightVector
.protected double averagedBias
public SparseAveragedPerceptron()
public SparseAveragedPerceptron(double r)
r
- The desired learning rate value.public SparseAveragedPerceptron(double r, double t)
r
- The desired learning rate value.t
- The desired threshold value.public SparseAveragedPerceptron(double r, double t, double pt)
r
- The desired learning rate value.t
- The desired threshold value.pt
- The desired thickness.public SparseAveragedPerceptron(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.public SparseAveragedPerceptron(SparseAveragedPerceptron.Parameters p)
SparseAveragedPerceptron.Parameters
object.p
- The settings of all parameters.public SparseAveragedPerceptron(String n)
n
- The name of the classifier.public SparseAveragedPerceptron(String n, double r)
n
- The name of the classifier.r
- The desired learning rate value.public SparseAveragedPerceptron(String n, double r, double t)
n
- The name of the classifier.r
- The desired learning rate value.t
- The desired threshold value.public SparseAveragedPerceptron(String n, double r, double t, double pt)
n
- The name of the classifier.r
- The desired learning rate value.t
- The desired threshold value.pt
- The desired thickness.public SparseAveragedPerceptron(String n, double r, double t, double pt, double nt)
n
- The name of the classifier.r
- The desired learning rate value.t
- The desired threshold value.pt
- The desired positive thickness.nt
- The desired negative thickness.public SparseAveragedPerceptron(String n, SparseAveragedPerceptron.Parameters p)
SparseAveragedPerceptron.Parameters
object.n
- The name of the classifier.p
- The settings of all parameters.public Learner.Parameters getParameters()
getParameters
in class SparsePerceptron
public void setParameters(SparseAveragedPerceptron.Parameters p)
p
- The parameters.public double score(int[] exampleFeatures, double[] exampleValues)
w * x + bias
where *
is dot product, w
is the weight vector, and x
is the feature vector
produced by the extractor.score
in class LinearThresholdUnit
exampleFeatures
- The example's array of feature indices.exampleValues
- The example's array of feature values.public void promote(int[] exampleFeatures, double[] exampleValues, double rate)
promote
in class SparsePerceptron
exampleFeatures
- The example's array of feature indices.exampleValues
- The example's array of feature values.rate
- The learning rate at which the weights are updated.public void demote(int[] exampleFeatures, double[] exampleValues, double rate)
demote
in class SparsePerceptron
exampleFeatures
- The example's array of feature indices.exampleValues
- The example's array of feature values.rate
- The learning rate at which the weights are updated.public void learn(int[] exampleFeatures, double[] exampleValues, int[] exampleLabels, double[] labelValues)
LinearThresholdUnit.learn(int[],double[],int[],double[])
,
except it notifies its weight vector when it got an example correct in addition to updating
it when it makes a mistake.learn
in class LinearThresholdUnit
exampleFeatures
- The example's array of feature indicesexampleValues
- The example's array of feature valuesexampleLabels
- The example's label(s)labelValues
- The labels' valuespublic void initialize(int numExamples, int numFeatures)
LinearThresholdUnit.initialWeight
.initialize
in class LinearThresholdUnit
numExamples
- The number of examplesnumFeatures
- The number of featurespublic void forget()
forget
in class LinearThresholdUnit
public void write(PrintStream out)
LinearThresholdUnit.learningRate
,
LinearThresholdUnit.initialWeight
, LinearThresholdUnit.threshold
,
LinearThresholdUnit.positiveThickness
, LinearThresholdUnit.negativeThickness
,
LinearThresholdUnit.bias
, and finally averagedBias
.write
in class SparsePerceptron
out
- The output stream.public void write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
write
in class LinearThresholdUnit
out
- The output stream.public void read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
read
in class LinearThresholdUnit
in
- The input stream.Copyright © 2016. All rights reserved.