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
ConstructorsModifierConstructorDescriptionprotectedDenseVector(double[] values) Does not defensively copy the input, used internally.DenseVector(int size) DenseVector(int size, double value) protectedDenseVector(DenseVector other) Copy constructor. -
Method Summary
Modifier and TypeMethodDescriptionvoidadd(int index, double value) Addsvalueto the element atindex.Addsotherto this vector, producing a newDenseVector.copy()Returns a deep copy of this vector.static DenseVectorcreateDenseVector(double[] values) Defensively copies the values before construction.static <T extends Output<T>>
DenseVectorcreateDenseVector(Example<T> example, ImmutableFeatureMap featureInfo, boolean addBias) Builds aDenseVectorfrom anExample.doubleCalculates the dot product between this vector andother.booleanEquals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices.doubleeuclideanDistance(SGDVector other) The l2 or euclidean distance between this vector and the other vector.voidexpNormalize(double total) An optimisation for the exponential normalizer when you already know the normalization constant.voidfill(double value) Fills thisDenseVectorwithvalue.voidApplies aDoubleUnaryOperatorelementwise to thisTensor.doubleget(int index) Gets an element from this vector.int[]getShape()Returns an int array specifying the shape of thisTensor.voidhadamardProductInPlace(Tensor other, DoubleUnaryOperator f) Updates thisTensorwith the Hadamard product (i.e., a term by term multiply) of this andother.inthashCode()intReturns the index of the maximum value.voidintersectAndAddInPlace(Tensor other, DoubleUnaryOperator f) Updates thisTensorby adding all the values from the intersection withother.iterator()doublel1Distance(SGDVector other) The l1 or Manhattan distance between this vector and the other vector.doublemaxValue()Returns the maximum value.doubleminValue()Returns the minimum value.voidnormalize(VectorNormalizer normalizer) Normalizes the vector using the supplied vector normalizer.intReturns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).doubleoneNorm()Calculates the Manhattan norm for this vector.Generates the matrix representing the outer product between the two vectors.doublereduce(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.voidset(int index, double value) Sets theindexto thevalue.voidsetElements(DenseVector other) Sets all the elements of this vector to be the same asother.intsize()Returns the dimensionality of this vector.sparsify()Generates aSparseVectorrepresentation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA.sparsify(double tolerance) Generates aSparseVectorrepresentation from this dense vector, removing all values with absolute value below the supplied tolerance.Subtractsotherfrom this vector, producing a newDenseVector.doublesum()Calculates the sum of this vector.doubledouble[]toArray()Generates a copy of the values in this DenseVector.toString()doubletwoNorm()Calculates the euclidean norm for this vector.doublevariance(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, waitMethods inherited from interface java.lang.Iterable
forEach, spliteratorMethods inherited from interface org.tribuo.math.la.SGDVector
cosineDistance, cosineSimilarity, l2Distance, varianceMethods 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 aDenseVectorfrom anExample.Used in training and inference.
Throws
IllegalArgumentExceptionif 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:TensorReshapes the Tensor to the supplied shape. ThrowsIllegalArgumentExceptionif the shape isn't compatible. -
copy
Description copied from interface:SGDVectorReturns a deep copy of this vector. -
size
-
numActiveElements
public int numActiveElements()Description copied from interface:SGDVectorReturns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).- Specified by:
numActiveElementsin 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
Addsotherto this vector, producing a newDenseVector.- Specified by:
addin interfaceSGDVector- Parameters:
other- The vector to add.- Returns:
- A new
DenseVectorwhere each element value = this.get(i) + other.get(i).
-
subtract
Subtractsotherfrom this vector, producing a newDenseVector.- Specified by:
subtractin interfaceSGDVector- Parameters:
other- The vector to subtract.- Returns:
- A new
DenseVectorwhere each element value = this.get(i) - other.get(i).
-
intersectAndAddInPlace
Description copied from interface:TensorUpdates thisTensorby adding all the values from the intersection withother.The function
fis applied to all values fromotherbefore the addition.Each value is updated as value += f(otherValue).
- Specified by:
intersectAndAddInPlacein interfaceTensor- Parameters:
other- The otherTensor.f- A function to apply.
-
hadamardProductInPlace
Description copied from interface:TensorUpdates thisTensorwith the Hadamard product (i.e., a term by term multiply) of this andother.The function
fis applied to all values fromotherbefore the addition.Each value is updated as value *= f(otherValue).
- Specified by:
hadamardProductInPlacein interfaceTensor- Parameters:
other- The otherTensor.f- A function to apply.
-
foreachInPlace
Description copied from interface:TensorApplies aDoubleUnaryOperatorelementwise to thisTensor.- Specified by:
foreachInPlacein interfaceTensor- Parameters:
f- The function to apply.
-
scale
Description copied from interface:SGDVectorGenerates 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- TheDenseVectorto copy.
-
fill
public void fill(double value) Fills thisDenseVectorwithvalue.- Parameters:
value- The value to store in this vector.
-
indexOfMax
public int indexOfMax()Description copied from interface:SGDVectorReturns the index of the maximum value. Requires probing the array.- Specified by:
indexOfMaxin interfaceSGDVector- Returns:
- The index of the maximum value.
-
maxValue
-
minValue
-
normalize
Description copied from interface:SGDVectorNormalizes 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:
iteratorin interfaceIterable<VectorTuple>
-
sparsify
Generates aSparseVectorrepresentation from this dense vector, removing all values with absolute value belowVectorTuple.DELTA.- Returns:
- A
SparseVector.
-
sparsify
Generates aSparseVectorrepresentation 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:
euclideanDistancein 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:
l1Distancein interfaceSGDVector- Parameters:
other- The other vector.- Returns:
- The l1 distance.
-