Class DenseVector
java.lang.Object
org.tribuo.math.la.DenseVector
- All Implemented Interfaces:
Serializable
,Iterable<VectorTuple>
,SGDVector
,Tensor
- Direct Known Subclasses:
ShrinkingVector
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
DenseVector
(double[] values) Does not defensively copy the input, used internally.DenseVector
(int size) DenseVector
(int size, double value) protected
DenseVector
(DenseVector other) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoid
add
(int index, double value) Addsvalue
to the element atindex
.Addsother
to this vector, producing a newDenseVector
.copy()
Returns a deep copy of this vector.static DenseVector
createDenseVector
(double[] values) Defensively copies the values before construction.static <T extends Output<T>>
DenseVectorcreateDenseVector
(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias) Builds aDenseVector
from anExample
.double
Calculates the dot product between this vector andother
.boolean
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices.double
euclideanDistance
(SGDVector other) The l2 or euclidean distance between this vector and the other vector.void
expNormalize
(double total) An optimisation for the exponential normalizer when you already know the normalization constant.void
fill
(double value) Fills thisDenseVector
withvalue
.void
Applies aDoubleUnaryOperator
elementwise to thisTensor
.double
get
(int index) Gets an element from this vector.int[]
getShape()
Returns an int array specifying the shape of thisTensor
.void
hadamardProductInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.int
hashCode()
int
Returns the index of the maximum value.void
intersectAndAddInPlace
(Tensor other, DoubleUnaryOperator f) Updates thisTensor
by adding all the values from the intersection withother
.iterator()
double
l1Distance
(SGDVector other) The l1 or Manhattan distance between this vector and the other vector.double
maxValue()
Returns the maximum value.double
minValue()
Returns the minimum value.void
normalize
(VectorNormalizer normalizer) Normalizes the vector using the supplied vector normalizer.int
Returns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).double
oneNorm()
Calculates the Manhattan norm for this vector.Generates the matrix representing the outer product between the two vectors.double
reduce
(double initialValue, DoubleUnaryOperator op, DoubleBinaryOperator reduction) Performs a reduction from left to right of this vector.reshape
(int[] newShape) Reshapes the Tensor to the supplied shape.scale
(double coefficient) Generates a new vector with each element scaled bycoefficient
.void
set
(int index, double value) Sets theindex
to thevalue
.void
setElements
(DenseVector other) Sets all the elements of this vector to be the same asother
.int
size()
Returns the dimensionality of this vector.sparsify()
Generates aSparseVector
representation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA
.sparsify
(double tolerance) Generates aSparseVector
representation from this dense vector, removing all values with absolute value below the supplied tolerance.Subtractsother
from this vector, producing a newDenseVector
.double
sum()
Calculates the sum of this vector.double
double[]
toArray()
Generates a copy of the values in this DenseVector.toString()
double
twoNorm()
Calculates the euclidean norm for this vector.double
variance
(double mean) Calculates the variance of this vector based on the supplied mean.Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
Methods inherited from interface java.lang.Iterable
forEach, spliterator
Methods inherited from interface org.tribuo.math.la.SGDVector
cosineDistance, cosineSimilarity, l2Distance, variance
Methods inherited from interface org.tribuo.math.la.Tensor
hadamardProductInPlace, intersectAndAddInPlace, scalarAddInPlace, scaleInPlace
-
Field Details
-
elements
protected final double[] elements
-
-
Constructor Details
-
DenseVector
public DenseVector(int size) -
DenseVector
public DenseVector(int size, double value) -
DenseVector
protected DenseVector(double[] values) Does not defensively copy the input, used internally.- Parameters:
values
- The values of this dense vector.
-
DenseVector
-
-
Method Details
-
createDenseVector
Defensively copies the values before construction.- Parameters:
values
- The values of this dense vector.- Returns:
- A new dense vector.
-
createDenseVector
public static <T extends Output<T>> DenseVector createDenseVector(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias) Builds aDenseVector
from anExample
.Used in training and inference.
Throws
IllegalArgumentException
if the Example contains NaN-valued features or if no features in this Example are present in the feature map..Unspecified features are set to zero.
- Type Parameters:
T
- The type parameter of theexample
.- Parameters:
example
- The example to convert.featureInfo
- The feature information, used to calculate the dimension of this DenseVector.addBias
- Add a bias feature.- Returns:
- A DenseVector representing the example's features.
-
toArray
-
getShape
-
reshape
Description copied from interface:Tensor
Reshapes the Tensor to the supplied shape. ThrowsIllegalArgumentException
if the shape isn't compatible. -
copy
Description copied from interface:SGDVector
Returns a deep copy of this vector. -
size
-
numActiveElements
public int numActiveElements()Description copied from interface:SGDVector
Returns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).- Specified by:
numActiveElements
in interfaceSGDVector
- Returns:
- The number of non-zero elements.
-
reduce
Performs a reduction from left to right of this vector. -
equals
Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices. -
hashCode
-
add
Addsother
to this vector, producing a newDenseVector
.- Specified by:
add
in interfaceSGDVector
- Parameters:
other
- The vector to add.- Returns:
- A new
DenseVector
where each element value = this.get(i) + other.get(i).
-
subtract
Subtractsother
from this vector, producing a newDenseVector
.- Specified by:
subtract
in interfaceSGDVector
- Parameters:
other
- The vector to subtract.- Returns:
- A new
DenseVector
where each element value = this.get(i) - other.get(i).
-
intersectAndAddInPlace
Description copied from interface:Tensor
Updates thisTensor
by adding all the values from the intersection withother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value += f(otherValue).
- Specified by:
intersectAndAddInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
hadamardProductInPlace
Description copied from interface:Tensor
Updates thisTensor
with the Hadamard product (i.e., a term by term multiply) of this andother
.The function
f
is applied to all values fromother
before the addition.Each value is updated as value *= f(otherValue).
- Specified by:
hadamardProductInPlace
in interfaceTensor
- Parameters:
other
- The otherTensor
.f
- A function to apply.
-
foreachInPlace
Description copied from interface:Tensor
Applies aDoubleUnaryOperator
elementwise to thisTensor
.- Specified by:
foreachInPlace
in interfaceTensor
- Parameters:
f
- The function to apply.
-
scale
Description copied from interface:SGDVector
Generates a new vector with each element scaled bycoefficient
. -
add
-
dot
-
outer
-
sum
-
sum
-
twoNorm
-
oneNorm
-
get
-
set
-
setElements
Sets all the elements of this vector to be the same asother
.- Parameters:
other
- TheDenseVector
to copy.
-
fill
public void fill(double value) Fills thisDenseVector
withvalue
.- Parameters:
value
- The value to store in this vector.
-
indexOfMax
public int indexOfMax()Description copied from interface:SGDVector
Returns the index of the maximum value. Requires probing the array.- Specified by:
indexOfMax
in interfaceSGDVector
- Returns:
- The index of the maximum value.
-
maxValue
-
minValue
-
normalize
Description copied from interface:SGDVector
Normalizes the vector using the supplied vector normalizer. -
expNormalize
public void expNormalize(double total) An optimisation for the exponential normalizer when you already know the normalization constant. Used in the CRF.- Parameters:
total
- The normalization constant.
-
toString
-
variance
-
iterator
- Specified by:
iterator
in interfaceIterable<VectorTuple>
-
sparsify
Generates aSparseVector
representation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA
.- Returns:
- A
SparseVector
.
-
sparsify
Generates aSparseVector
representation from this dense vector, removing all values with absolute value below the supplied tolerance.- Parameters:
tolerance
- The threshold below which to set a value to zero.- Returns:
- A
SparseVector
.
-
euclideanDistance
The l2 or euclidean distance between this vector and the other vector.- Specified by:
euclideanDistance
in interfaceSGDVector
- Parameters:
other
- The other vector.- Returns:
- The euclidean distance between them.
-
l1Distance
The l1 or Manhattan distance between this vector and the other vector.- Specified by:
l1Distance
in interfaceSGDVector
- Parameters:
other
- The other vector.- Returns:
- The l1 distance.
-