public class SparseWeightVector extends Object implements Cloneable, Serializable
Feature
s are
associated with Double
s and/or with double[]
s representing the weights
of the features they produce. Features not appearing in the vector are assumed to have the
defaultWeight
.Modifier and Type | Field and Description |
---|---|
protected static int |
defaultCapacity
The initial capacity for
weights if not specified otherwise. |
protected static double |
defaultWeight
When a feature appears in an example but not in this vector, it is assumed to have this
weight.
|
protected edu.illinois.cs.cogcomp.core.datastructures.vectors.DVector |
weights
The weights in the vector indexed by their
Lexicon key. |
Constructor and Description |
---|
SparseWeightVector()
Simply instantiates
weights . |
SparseWeightVector(double[] w)
Simply initializes
weights . |
SparseWeightVector(edu.illinois.cs.cogcomp.core.datastructures.vectors.DVector w)
Simply initializes
weights . |
Modifier and Type | Method and Description |
---|---|
void |
clear()
Empties the weight map.
|
Object |
clone()
Returns a copy of this
SparseWeightVector in which the weights variable
has been cloned deeply. |
double |
dot(int[] exampleFeatures,
double[] exampleValues)
Takes the dot product of this
SparseWeightVector with the argument vector, using
the hard coded default weight. |
double |
dot(int[] exampleFeatures,
double[] exampleValues,
double defaultW)
Takes the dot product of this
SparseWeightVector with the argument vector, using
the specified default weight when one is not yet present in this vector. |
SparseWeightVector |
emptyClone()
Returns a new, empty weight vector with the same parameter settings as this one.
|
double |
getWeight(int featureIndex)
Returns the weight of the given feature.
|
double |
getWeight(int featureIndex,
double defaultW)
Returns the weight of the given feature.
|
Object[] |
pairwiseMultiply(int[] exampleFeatures,
double[] exampleValues,
double defaultW,
boolean inverse)
The strength of each feature in the argument vector is multiplied by the corresponding weight
in this weight vector and the result is returned as an array of arrays.
|
void |
read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
Reads the representation of a weight vector with this object's run-time type from the given
stream, overwriting the data in this object.
|
static SparseWeightVector |
readWeightVector(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
Reads the binary representation of a weight vector of any type from the given stream.
|
void |
scaledAdd(int[] exampleFeatures,
double[] exampleValues)
Self-modifying vector addition.
|
void |
scaledAdd(int[] exampleFeatures,
double[] exampleValues,
double factor)
Self-modifying vector addition where the argument vector is first scaled by the given factor.
|
void |
scaledAdd(int[] exampleFeatures,
double[] exampleValues,
double factor,
double defaultW)
Self-modifying vector addition where the argument vector is first scaled by the given factor.
|
void |
scaledMultiply(int[] exampleFeatures,
double[] exampleValues,
double factor)
Self-modifying vector multiplication where the argument vector is first scaled by the given
factor.
|
void |
scaledMultiply(int[] exampleFeatures,
double[] exampleValues,
double factor,
double defaultW)
Self-modifying vector multiplication where the argument vector is first scaled by the given
factor.
|
protected void |
setWeight(int featureIndex,
double w)
Sets the weight of the given feature.
|
protected void |
setWeight(int featureIndex,
double w,
double defaultW)
Sets the weight of the given feature.
|
int |
size()
Returns the length of the weight vector.
|
String |
toString()
Creates a string representation of this
SparseWeightVector . |
String |
toString(Lexicon lex)
Creates a string representation of this
SparseWeightVector . |
void |
toStringJustWeights(PrintStream out)
Outputs a textual representation of this
SparseWeightVector to a stream just
like write(PrintStream) , but without the "Begin" and "End"
annotations. |
void |
toStringJustWeights(PrintStream out,
int min,
Lexicon lex)
Outputs a textual representation of this
SparseWeightVector to a stream just
like write(PrintStream) , but without the "Begin" and "End"
annotations. |
void |
write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
Writes the weight vector's internal representation in binary form.
|
void |
write(PrintStream out)
Outputs the contents of this
SparseWeightVector into the specified
PrintStream . |
void |
write(PrintStream out,
Lexicon lex)
Outputs the contents of this
SparseWeightVector into the specified
PrintStream . |
protected static final double defaultWeight
protected static final int defaultCapacity
weights
if not specified otherwise.protected edu.illinois.cs.cogcomp.core.datastructures.vectors.DVector weights
Lexicon
key.public SparseWeightVector()
weights
.public SparseWeightVector(double[] w)
weights
.w
- An array of weights.public SparseWeightVector(edu.illinois.cs.cogcomp.core.datastructures.vectors.DVector w)
weights
.w
- A vector of weights.public double getWeight(int featureIndex)
featureIndex
- The feature index.public double getWeight(int featureIndex, double defaultW)
featureIndex
- The feature index.defaultW
- The default weight.protected void setWeight(int featureIndex, double w)
featureIndex
- The feature index.w
- The new weight.protected void setWeight(int featureIndex, double w, double defaultW)
featureIndex
- The feature index.w
- The new weight.defaultW
- The default weight.public double dot(int[] exampleFeatures, double[] exampleValues)
SparseWeightVector
with the argument vector, using
the hard coded default weight.exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.public double dot(int[] exampleFeatures, double[] exampleValues, double defaultW)
SparseWeightVector
with the argument vector, using
the specified default weight when one is not yet present in this vector.exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.defaultW
- The default weight.public void scaledAdd(int[] exampleFeatures, double[] exampleValues)
exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.public void scaledAdd(int[] exampleFeatures, double[] exampleValues, double factor)
exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.factor
- The scaling factor.public void scaledAdd(int[] exampleFeatures, double[] exampleValues, double factor, double defaultW)
exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.factor
- The scaling factor.defaultW
- An initial weight for previously unseen features.public void scaledMultiply(int[] exampleFeatures, double[] exampleValues, double factor)
exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.factor
- The scaling factor.public void scaledMultiply(int[] exampleFeatures, double[] exampleValues, double factor, double defaultW)
exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.factor
- The scaling factor.defaultW
- An initial weight for previously unseen features.public Object[] pairwiseMultiply(int[] exampleFeatures, double[] exampleValues, double defaultW, boolean inverse)
exampleFeatures
- The example's feature indices.exampleValues
- The example's feature values.defaultW
- An initial weight for previously unseen features.inverse
- When set to true
the weight in this vector is inverted before the
multiplication takes place.public void clear()
public int size()
public void write(PrintStream out)
SparseWeightVector
into the specified
PrintStream
. The string representation starts with a "Begin"
annotation, ends with an "End"
annotation, and without a Lexicon
passed as a parameter, the weights are simply printed in the order of their integer indices.out
- The stream to write to.public void write(PrintStream out, Lexicon lex)
SparseWeightVector
into the specified
PrintStream
. The string representation starts with a "Begin"
annotation, ends with an "End"
annotation, and lists each feature with its
corresponding weight on the same, separate line in between.out
- The stream to write to.lex
- The feature lexicon.public void toStringJustWeights(PrintStream out)
SparseWeightVector
to a stream just
like write(PrintStream)
, but without the "Begin"
and "End"
annotations. Without a Lexicon
passed as a parameter, the weights are simply
printed in the order of their integer indices.out
- The stream to write to.public void toStringJustWeights(PrintStream out, int min, Lexicon lex)
SparseWeightVector
to a stream just
like write(PrintStream)
, but without the "Begin"
and "End"
annotations. With a Lexicon
passed as a parameter, the feature is printed along
with each weight.out
- The stream to write to.min
- Sets the minimum width for the textual representation of all features.lex
- The feature lexicon.public String toString()
SparseWeightVector
. This method merely
returns the data computed by write(PrintStream)
.public String toString(Lexicon lex)
SparseWeightVector
. This method merely
returns the data computed by write(PrintStream,Lexicon)
.lex
- The feature lexicon.public void write(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessOutputStream out)
out
- The output stream.public static SparseWeightVector readWeightVector(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
-1
appears instead, this method returns
null
.
This method is appropriate for reading weight vectors as written by
write(ExceptionlessOutputStream)
.
in
- The input stream.public void read(edu.illinois.cs.cogcomp.core.datastructures.vectors.ExceptionlessInputStream in)
This method is appropriate for reading weight vectors as written by
write(ExceptionlessOutputStream)
.
in
- The input stream.public Object clone()
SparseWeightVector
in which the weights
variable
has been cloned deeply.public SparseWeightVector emptyClone()
Copyright © 2016. All rights reserved.