Class DenseVector
- All Implemented Interfaces:
Serializable,Iterable<VectorTuple>,SGDVector,Tensor,ProtoSerializable<org.tribuo.math.protos.TensorProto>
- Direct Known Subclasses:
ShrinkingVector
- See Also:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intProtobuf serialization version.protected final double[]The value array.Fields inherited from interface org.tribuo.protos.ProtoSerializable
DESERIALIZATION_METHOD_NAME, PROVENANCE_SERIALIZER -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotectedDenseVector(double[] values) Does not defensively copy the input, used internally.DenseVector(int size) Creates an empty dense vector of the specified size.DenseVector(int size, double value) Creates a dense vector of the specified size where each element is initialised to the specified 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.static DenseVectordeserializeFromProto(int version, String className, com.google.protobuf.Any message) Deserialization factory.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 aToDoubleBiFunctionelementwise to thisSGDVector.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.Compute the mean and variance of this vector.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.<T> Treduce(T initialValue, DoubleUnaryOperator op, BiFunction<Double, T, T> 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.org.tribuo.math.protos.TensorProtovoidset(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.doubleSums this vector, applying the supplied function to each element first.double[]toArray()Generates a copy of the values in this DenseVector.toString()doubletwoNorm()Calculates the euclidean norm for this vector.protected static DenseVectorunpackProto(org.tribuo.math.protos.DenseTensorProto proto) Unpacks aDenseTensorProtointo aDenseVector.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
-
CURRENT_VERSION
public static final int CURRENT_VERSIONProtobuf serialization version.- See Also:
-
elements
protected final double[] elementsThe value array.
-
-
Constructor Details
-
DenseVector
public DenseVector(int size) Creates an empty dense vector of the specified size.- Parameters:
size- The vector size.
-
DenseVector
public DenseVector(int size, double value) Creates a dense vector of the specified size where each element is initialised to the specified value.- Parameters:
size- The vector size.value- The initial value.
-
DenseVector
protected DenseVector(double[] values) Does not defensively copy the input, used internally.- Parameters:
values- The values of this dense vector.
-
DenseVector
Copy constructor.- Parameters:
other- The vector to copy.
-
-
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.
-
deserializeFromProto
public static DenseVector deserializeFromProto(int version, String className, com.google.protobuf.Any message) throws com.google.protobuf.InvalidProtocolBufferException Deserialization factory.- Parameters:
version- The serialized object version.className- The class name.message- The serialized data.- Returns:
- The deserialized object.
- Throws:
com.google.protobuf.InvalidProtocolBufferException- If the protobuf could not be parsed from themessage.
-
unpackProto
Unpacks aDenseTensorProtointo aDenseVector.- Parameters:
proto- The proto to unpack.- Returns:
- The dense vector.
-
serialize
public org.tribuo.math.protos.TensorProto serialize()- Specified by:
serializein interfaceProtoSerializable<org.tribuo.math.protos.TensorProto>
-
toArray
public double[] toArray()Generates a copy of the values in this DenseVector.This implementation uses Arrays.copyOf, and should be overridden if the get function has been modified.
-
getShape
public int[] getShape()Description copied from interface:TensorReturns an int array specifying the shape of thisTensor. -
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
public int size()Description copied from interface:SGDVectorReturns the dimensionality of this vector. -
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.The first argument to the reducer is the transformed element, the second is the state.
-
reduce
Performs a reduction from left to right of this vector.The first argument to the reducer is the transformed vector element, the second is the state.
- Type Parameters:
T- The output type to reduce to.- Parameters:
initialValue- The initial value.op- The element wise operation to apply before reducing.reduction- The reduction operation (should be commutative).- Returns:
- The reduced value.
-
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
public int 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.
-
foreachIndexedInPlace
Description copied from interface:SGDVectorApplies aToDoubleBiFunctionelementwise to thisSGDVector.The first argument to the function is the index, the second argument is the current value.
- Specified by:
foreachIndexedInPlacein interfaceSGDVector- Parameters:
f- The function to apply.
-
scale
Description copied from interface:SGDVectorGenerates a new vector with each element scaled bycoefficient. -
add
public void add(int index, double value) Description copied from interface:SGDVectorAddsvalueto the element atindex. -
dot
Description copied from interface:SGDVectorCalculates the dot product between this vector andother. -
outer
Description copied from interface:SGDVectorGenerates the matrix representing the outer product between the two vectors. -
sum
public double sum()Description copied from interface:SGDVectorCalculates the sum of this vector. -
sum
Sums this vector, applying the supplied function to each element first.- Parameters:
f- The function to apply to the elements.- Returns:
- The sum of f(x).
-
twoNorm
public double twoNorm()Description copied from interface:SGDVectorCalculates the euclidean norm for this vector. -
oneNorm
public double oneNorm()Description copied from interface:SGDVectorCalculates the Manhattan norm for this vector. -
get
public double get(int index) Description copied from interface:SGDVectorGets an element from this vector. -
set
public void set(int index, double value) Description copied from interface:SGDVectorSets theindexto thevalue. -
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
public double maxValue()Description copied from interface:SGDVectorReturns the maximum value. Requires probing the array. -
minValue
public double minValue()Description copied from interface:SGDVectorReturns the minimum value. Requires probing the array. -
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
public double variance(double mean) Description copied from interface:SGDVectorCalculates the variance of this vector based on the supplied mean. -
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.
-
meanVariance
Compute the mean and variance of this vector.- Returns:
- The mean and variance.
-