Class DenseVector

java.lang.Object
org.tribuo.math.la.DenseVector
All Implemented Interfaces:
Serializable, Iterable<VectorTuple>, SGDVector, Tensor, ProtoSerializable<org.tribuo.math.protos.TensorProto>
Direct Known Subclasses:
ShrinkingVector

public class DenseVector extends Object implements SGDVector
A dense vector, backed by a double array.
See Also:
  • Field Details

    • CURRENT_VERSION

      public static final int CURRENT_VERSION
      Protobuf serialization version.
      See Also:
    • elements

      protected final double[] elements
      The 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

      protected DenseVector(DenseVector other)
      Copy constructor.
      Parameters:
      other - The vector to copy.
  • Method Details

    • createDenseVector

      public static DenseVector createDenseVector(double[] values)
      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 a DenseVector from an Example.

      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 the example.
      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 the message.
    • unpackProto

      protected static DenseVector unpackProto(org.tribuo.math.protos.DenseTensorProto proto)
      Unpacks a DenseTensorProto into a DenseVector.
      Parameters:
      proto - The proto to unpack.
      Returns:
      The dense vector.
    • serialize

      public org.tribuo.math.protos.TensorProto serialize()
      Description copied from interface: ProtoSerializable
      Serializes this object to a protobuf.
      Specified by:
      serialize in interface ProtoSerializable<org.tribuo.math.protos.TensorProto>
      Returns:
      The protobuf.
    • 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.

      Specified by:
      toArray in interface SGDVector
      Returns:
      A copy of the values in this DenseVector.
    • getShape

      public int[] getShape()
      Description copied from interface: Tensor
      Returns an int array specifying the shape of this Tensor.
      Specified by:
      getShape in interface Tensor
      Returns:
      An int array.
    • reshape

      public Tensor reshape(int[] newShape)
      Description copied from interface: Tensor
      Reshapes the Tensor to the supplied shape. Throws IllegalArgumentException if the shape isn't compatible.
      Specified by:
      reshape in interface Tensor
      Parameters:
      newShape - The desired shape.
      Returns:
      A Tensor of the desired shape.
    • copy

      public DenseVector copy()
      Description copied from interface: SGDVector
      Returns a deep copy of this vector.
      Specified by:
      copy in interface SGDVector
      Specified by:
      copy in interface Tensor
      Returns:
      A copy of this vector.
    • size

      public int size()
      Description copied from interface: SGDVector
      Returns the dimensionality of this vector.
      Specified by:
      size in interface SGDVector
      Returns:
      The dimensionality of the vector.
    • 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 interface SGDVector
      Returns:
      The number of non-zero elements.
    • reduce

      public double reduce(double initialValue, DoubleUnaryOperator op, DoubleBinaryOperator reduction)
      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.

      Specified by:
      reduce in interface SGDVector
      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.
    • reduce

      public <T> T reduce(T initialValue, DoubleUnaryOperator op, BiFunction<Double,T,T> reduction)
      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

      public boolean equals(Object other)
      Equals is defined mathematically, that is two SGDVectors are equal iff they have the same indices and the same values at those indices.
      Overrides:
      equals in class Object
      Parameters:
      other - Object to compare against.
      Returns:
      True if this vector and the other vector contain the same values in the same order.
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • add

      public DenseVector add(SGDVector other)
      Adds other to this vector, producing a new DenseVector.
      Specified by:
      add in interface SGDVector
      Parameters:
      other - The vector to add.
      Returns:
      A new DenseVector where each element value = this.get(i) + other.get(i).
    • subtract

      public DenseVector subtract(SGDVector other)
      Subtracts other from this vector, producing a new DenseVector.
      Specified by:
      subtract in interface SGDVector
      Parameters:
      other - The vector to subtract.
      Returns:
      A new DenseVector where each element value = this.get(i) - other.get(i).
    • intersectAndAddInPlace

      public void intersectAndAddInPlace(Tensor other, DoubleUnaryOperator f)
      Description copied from interface: Tensor
      Updates this Tensor by adding all the values from the intersection with other.

      The function f is applied to all values from other before the addition.

      Each value is updated as value += f(otherValue).

      Specified by:
      intersectAndAddInPlace in interface Tensor
      Parameters:
      other - The other Tensor.
      f - A function to apply.
    • hadamardProductInPlace

      public void hadamardProductInPlace(Tensor other, DoubleUnaryOperator f)
      Description copied from interface: Tensor
      Updates this Tensor with the Hadamard product (i.e., a term by term multiply) of this and other.

      The function f is applied to all values from other before the addition.

      Each value is updated as value *= f(otherValue).

      Specified by:
      hadamardProductInPlace in interface Tensor
      Parameters:
      other - The other Tensor.
      f - A function to apply.
    • foreachInPlace

      public void foreachInPlace(DoubleUnaryOperator f)
      Description copied from interface: Tensor
      Applies a DoubleUnaryOperator elementwise to this Tensor.
      Specified by:
      foreachInPlace in interface Tensor
      Parameters:
      f - The function to apply.
    • foreachIndexedInPlace

      public void foreachIndexedInPlace(ToDoubleBiFunction<Integer,Double> f)
      Description copied from interface: SGDVector
      Applies a ToDoubleBiFunction elementwise to this SGDVector.

      The first argument to the function is the index, the second argument is the current value.

      Specified by:
      foreachIndexedInPlace in interface SGDVector
      Parameters:
      f - The function to apply.
    • scale

      public DenseVector scale(double coefficient)
      Description copied from interface: SGDVector
      Generates a new vector with each element scaled by coefficient.
      Specified by:
      scale in interface SGDVector
      Parameters:
      coefficient - The coefficient to scale the elements by.
      Returns:
      A new SGDVector.
    • add

      public void add(int index, double value)
      Description copied from interface: SGDVector
      Adds value to the element at index.
      Specified by:
      add in interface SGDVector
      Parameters:
      index - The index to update.
      value - The value to add.
    • dot

      public double dot(SGDVector other)
      Description copied from interface: SGDVector
      Calculates the dot product between this vector and other.
      Specified by:
      dot in interface SGDVector
      Parameters:
      other - The other vector.
      Returns:
      The dot product.
    • outer

      public Matrix outer(SGDVector other)
      Description copied from interface: SGDVector
      Generates the matrix representing the outer product between the two vectors.
      Specified by:
      outer in interface SGDVector
      Parameters:
      other - Another SGDVector
      Returns:
      The outer product Matrix.
    • sum

      public double sum()
      Description copied from interface: SGDVector
      Calculates the sum of this vector.
      Specified by:
      sum in interface SGDVector
      Returns:
      The sum.
    • sum

      public double sum(DoubleUnaryOperator f)
      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: SGDVector
      Calculates the euclidean norm for this vector.
      Specified by:
      twoNorm in interface SGDVector
      Specified by:
      twoNorm in interface Tensor
      Returns:
      The euclidean norm.
    • oneNorm

      public double oneNorm()
      Description copied from interface: SGDVector
      Calculates the Manhattan norm for this vector.
      Specified by:
      oneNorm in interface SGDVector
      Returns:
      The Manhattan norm.
    • get

      public double get(int index)
      Description copied from interface: SGDVector
      Gets an element from this vector.
      Specified by:
      get in interface SGDVector
      Parameters:
      index - The index of the element.
      Returns:
      The value at that index.
    • set

      public void set(int index, double value)
      Description copied from interface: SGDVector
      Sets the index to the value.
      Specified by:
      set in interface SGDVector
      Parameters:
      index - The index to set.
      value - The value to set it to.
    • setElements

      public void setElements(DenseVector other)
      Sets all the elements of this vector to be the same as other.
      Parameters:
      other - The DenseVector to copy.
    • fill

      public void fill(double value)
      Fills this DenseVector with value.
      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 interface SGDVector
      Returns:
      The index of the maximum value.
    • maxValue

      public double maxValue()
      Description copied from interface: SGDVector
      Returns the maximum value. Requires probing the array.
      Specified by:
      maxValue in interface SGDVector
      Returns:
      The maximum value.
    • minValue

      public double minValue()
      Description copied from interface: SGDVector
      Returns the minimum value. Requires probing the array.
      Specified by:
      minValue in interface SGDVector
      Returns:
      The minimum value.
    • normalize

      public void normalize(VectorNormalizer normalizer)
      Description copied from interface: SGDVector
      Normalizes the vector using the supplied vector normalizer.
      Specified by:
      normalize in interface SGDVector
      Parameters:
      normalizer - The kind of normalization to apply.
    • 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

      public String toString()
      Overrides:
      toString in class Object
    • variance

      public double variance(double mean)
      Description copied from interface: SGDVector
      Calculates the variance of this vector based on the supplied mean.
      Specified by:
      variance in interface SGDVector
      Parameters:
      mean - The mean of the vector.
      Returns:
      The variance of the vector.
    • iterator

      public VectorIterator iterator()
      Specified by:
      iterator in interface Iterable<VectorTuple>
    • sparsify

      public SparseVector sparsify()
      Generates a SparseVector representation from this dense vector, removing all values with absolute value below VectorTuple.DELTA.
      Returns:
      A SparseVector.
    • sparsify

      public SparseVector sparsify(double tolerance)
      Generates a SparseVector 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

      public double euclideanDistance(SGDVector other)
      The l2 or euclidean distance between this vector and the other vector.
      Specified by:
      euclideanDistance in interface SGDVector
      Parameters:
      other - The other vector.
      Returns:
      The euclidean distance between them.
    • l1Distance

      public double l1Distance(SGDVector other)
      The l1 or Manhattan distance between this vector and the other vector.
      Specified by:
      l1Distance in interface SGDVector
      Parameters:
      other - The other vector.
      Returns:
      The l1 distance.
    • meanVariance

      public MeanVarianceAccumulator meanVariance()
      Compute the mean and variance of this vector.
      Returns:
      The mean and variance.