Interface SGDVector

All Superinterfaces:
Iterable<VectorTuple>, ProtoSerializable<org.tribuo.math.protos.TensorProto>, Serializable, Tensor
All Known Implementing Classes:
DenseVector, ShrinkingVector, SparseVector

public interface SGDVector extends Tensor, Iterable<VectorTuple>
Interface for 1 dimensional Tensors.

Vectors have immutable sizes and immutable indices (so SparseVector can't grow).

  • Method Details

    • foreachIndexedInPlace

      void foreachIndexedInPlace(ToDoubleBiFunction<Integer,Double> f)
      Applies a ToDoubleBiFunction elementwise to this SGDVector.

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

      Parameters:
      f - The function to apply.
    • copy

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

      int size()
      Returns the dimensionality of this vector.
      Returns:
      The dimensionality of the vector.
    • numActiveElements

      int numActiveElements()
      Returns the number of non-zero elements (on construction, an element could be set to zero and it would still remain active).
      Returns:
      The number of non-zero elements.
    • scale

      SGDVector scale(double coefficient)
      Generates a new vector with each element scaled by coefficient.
      Parameters:
      coefficient - The coefficient to scale the elements by.
      Returns:
      A new SGDVector.
    • add

      void add(int index, double value)
      Adds value to the element at index.
      Parameters:
      index - The index to update.
      value - The value to add.
    • add

      SGDVector add(SGDVector other)
      Adds other to this vector, producing a new SGDVector. Adding Dense to Dense/Sparse produces a DenseVector, adding Sparse to Sparse produces a SparseVector.
      Parameters:
      other - The vector to add.
      Returns:
      A new SGDVector where each element value = this.get(i) + other.get(i).
    • subtract

      SGDVector subtract(SGDVector other)
      Subtracts other from this vector, producing a new SGDVector. Subtracting Dense from Dense/Sparse produces a DenseVector, subtracting Sparse from Sparse produces a SparseVector.
      Parameters:
      other - The vector to subtract.
      Returns:
      A new SGDVector where each element value = this.get(i) - other.get(i).
    • dot

      double dot(SGDVector other)
      Calculates the dot product between this vector and other.
      Parameters:
      other - The other vector.
      Returns:
      The dot product.
    • outer

      Matrix outer(SGDVector other)
      Generates the matrix representing the outer product between the two vectors.
      Parameters:
      other - Another SGDVector
      Returns:
      The outer product Matrix.
    • sum

      double sum()
      Calculates the sum of this vector.
      Returns:
      The sum.
    • twoNorm

      double twoNorm()
      Calculates the euclidean norm for this vector.
      Specified by:
      twoNorm in interface Tensor
      Returns:
      The euclidean norm.
    • oneNorm

      double oneNorm()
      Calculates the Manhattan norm for this vector.
      Returns:
      The Manhattan norm.
    • get

      double get(int index)
      Gets an element from this vector.
      Parameters:
      index - The index of the element.
      Returns:
      The value at that index.
    • set

      void set(int index, double value)
      Sets the index to the value.
      Parameters:
      index - The index to set.
      value - The value to set it to.
    • indexOfMax

      int indexOfMax()
      Returns the index of the maximum value. Requires probing the array.
      Returns:
      The index of the maximum value.
    • maxValue

      double maxValue()
      Returns the maximum value. Requires probing the array.
      Returns:
      The maximum value.
    • minValue

      double minValue()
      Returns the minimum value. Requires probing the array.
      Returns:
      The minimum value.
    • normalize

      void normalize(VectorNormalizer normalizer)
      Normalizes the vector using the supplied vector normalizer.
      Parameters:
      normalizer - The kind of normalization to apply.
    • reduce

      double reduce(double initial, DoubleUnaryOperator transform, DoubleBinaryOperator reduction)
      Reduces the vector, applying the transformation to every value (including the implicit zeros) and reducing the output by applying the supplied reduction operator (where the right argument is the current reduction value, and the left argument is the transformed value). The reduction operation is seeded with the initial value.
      Parameters:
      initial - The initial value for the reduction.
      transform - The transformation operator.
      reduction - The reduction operator.
      Returns:
      The reduction of this vector.
    • l2Distance

      default double l2Distance(SGDVector other)
      Synonym for euclideanDistance.
      Parameters:
      other - The other vector.
      Returns:
      The l2 norm of the difference between the two vectors.
    • euclideanDistance

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

      double l1Distance(SGDVector other)
      The l1 or Manhattan distance between this vector and the other vector.
      Parameters:
      other - The other vector.
      Returns:
      The l1 distance.
    • cosineDistance

      default double cosineDistance(SGDVector other)
      Calculates the cosine distance of two vectors. 1 - cos(x,y)
      Parameters:
      other - The other vector.
      Returns:
      1 - cosine similarity (this,other)
    • cosineSimilarity

      default double cosineSimilarity(SGDVector other)
      Calculates the cosine similarity of two vectors. cos(x,y) = dot(x,y) / (norm(x) * norm(y))
      Parameters:
      other - The other vector.
      Returns:
      cosine similarity (this,other)
    • variance

      default double variance()
      Calculates the variance of this vector.
      Returns:
      The variance of the vector.
    • variance

      double variance(double mean)
      Calculates the variance of this vector based on the supplied mean.
      Parameters:
      mean - The mean of the vector.
      Returns:
      The variance of the vector.
    • toArray

      double[] toArray()
      Returns an array containing all the values in the vector (including any implicit zeros).
      Returns:
      An array copy.