Class MathOps

java.lang.Object
org.tensorflow.framework.op.MathOps

public class MathOps extends Object
  • Method Details

    • l2Normalize

      public <T extends TNumber> Operand<T> l2Normalize(Operand<T> x, int[] axis)
      Normalizes along dimension axis using an L2 norm.
      Type Parameters:
      T - the data type for the input and the result
      Parameters:
      x - the input
      axis - Dimension along which to normalize.
      Returns:
      the normalized values based on L2 norm
    • confusionMatrix

      public <T extends TNumber> Operand<T> confusionMatrix(Operand<T> labels, Operand<T> predictions)
      Computes the confusion matrix from predictions and labels.

      The matrix columns represent the prediction labels and the rows represent the real labels. The confusion matrix is always a 2-D array of shape [n,n], where n is the number of valid labels for a given classification task. Both prediction and labels must be 1-D arrays of the same shape in order for this function to work.

      If numClasses is null, then numClasses will be set to one plus the maximum value in either predictions or labels. Class labels are expected to start at 0. For example, if numClasses is 3, then the possible labels would be [0, 1, 2].

      If weights is not null, then each prediction contributes its corresponding weight to the total value of the confusion matrix cell.

      For example:

      fops.math.confusion_matrix(tf.constant(new int[] {1, 2, 4}), tf.constant(new int[] {2, 2, 4})) ==>
          [[0 0 0 0 0]
           [0 0 1 0 0]
           [0 0 1 0 0]
           [0 0 0 0 0]
           [0 0 0 0 1]]
      

      Note that the possible labels are assumed to be [0, 1, 2, 3, 4], resulting in a 5x5 confusion matrix.

      Type Parameters:
      T - Data type of the confusion matrix.
      Parameters:
      labels - 1-D Operand of real labels for the classification task.
      predictions - 1-D Operand of predictions for a given classification.
      Returns:
      An Operand of type type with shape [n, n] representing the confusion matrix, where n is the number of possible labels in the classification task.
      Throws:
      IllegalArgumentException - If both predictions and labels are not 1-D vectors and have mismatched shapes, or if weights is not null and its shape doesn't match predictions.
    • confusionMatrix

      public <T extends TNumber> Operand<T> confusionMatrix(Operand<T> labels, Operand<T> predictions, Operand<T> weights)
      Computes the confusion matrix from predictions and labels.

      The matrix columns represent the prediction labels and the rows represent the real labels. The confusion matrix is always a 2-D array of shape [n,n], where n is the number of valid labels for a given classification task. Both prediction and labels must be 1-D arrays of the same shape in order for this function to work.

      If numClasses is null, then numClasses will be set to one plus the maximum value in either predictions or labels. Class labels are expected to start at 0. For example, if numClasses is 3, then the possible labels would be [0, 1, 2].

      If weights is not null, then each prediction contributes its corresponding weight to the total value of the confusion matrix cell.

      For example:

      fops.math.confusion_matrix(tf.constant(new int[] {1, 2, 4}), tf.constant(new int[] {2, 2, 4})) ==>
          [[0 0 0 0 0]
           [0 0 1 0 0]
           [0 0 1 0 0]
           [0 0 0 0 0]
           [0 0 0 0 1]]
      

      Note that the possible labels are assumed to be [0, 1, 2, 3, 4], resulting in a 5x5 confusion matrix.

      Type Parameters:
      T - Data type of the confusion matrix.
      Parameters:
      labels - 1-D Operand of real labels for the classification task.
      predictions - 1-D Operand of predictions for a given classification.
      weights - An optional Operand whose shape matches predictions.
      Returns:
      An Operand of type type with shape [n, n] representing the confusion matrix, where n is the number of possible labels in the classification task.
      Throws:
      IllegalArgumentException - If both predictions and labels are not 1-D vectors and have mismatched shapes, or if weights is not null and its shape doesn't match predictions.
    • confusionMatrix

      public <T extends TNumber> Operand<T> confusionMatrix(Operand<T> labels, Operand<T> predictions, Operand<T> weights, Operand<TInt64> numClasses)
      Computes the confusion matrix from predictions and labels.

      The matrix columns represent the prediction labels and the rows represent the real labels. The confusion matrix is always a 2-D array of shape [n,n], where n is the number of valid labels for a given classification task. Both prediction and labels must be 1-D arrays of the same shape in order for this function to work.

      If numClasses is null, then numClasses will be set to one plus the maximum value in either predictions or labels. Class labels are expected to start at 0. For example, if numClasses is 3, then the possible labels would be [0, 1, 2].

      If weights is not null, then each prediction contributes its corresponding weight to the total value of the confusion matrix cell.

      For example:

      fops.math.confusion_matrix(tf.constant(new int[] {1, 2, 4}), tf.constant(new int[] {2, 2, 4})) ==>
          [[0 0 0 0 0]
           [0 0 1 0 0]
           [0 0 1 0 0]
           [0 0 0 0 0]
           [0 0 0 0 1]]
      

      Note that the possible labels are assumed to be [0, 1, 2, 3, 4], resulting in a 5x5 confusion matrix.

      Type Parameters:
      T - Data type of the confusion matrix.
      Parameters:
      labels - 1-D Operand of real labels for the classification task.
      predictions - 1-D Operand of predictions for a given classification.
      weights - An optional Operand whose shape matches predictions.
      numClasses - The possible number of labels the classification task can have. If this value is null, it will be calculated using both predictions and labels.
      Returns:
      An Operand of type type with shape [n, n] representing the confusion matrix, where n is the number of possible labels in the classification task.
      Throws:
      IllegalArgumentException - If both predictions and labels are not 1-D vectors and have mismatched shapes, or if weights is not null and its shape doesn't match predictions.
    • allAxes

      public Operand<TInt32> allAxes(Operand<? extends TType> op)
      Creates an Operand that has all axes contained in the Operand's shape.
      Parameters:
      op - the Operand
      Returns:
      an Operand that has all axes contained in the Operand's shape..
    • tensordot

      public <T extends TFloating> Operand<T> tensordot(Operand<T> a, Operand<T> b, int axis)
      Tensor contraction of a and b along specified axes and outer product.

      Tensordot (also known as tensor contraction) sums the product of elements from a and b over the indices specified by a_axes and b_axes. The lists a_axes and b_axes specify those pairs of axes along which to contract the tensors. The axis a_axes[i] of a must have the same dimension as axis b_axes[i] of b for all i in range(0, len(a_axes)). The lists a_axes and b_axes must have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passing axes=0.

      This operation corresponds to numpy.tensordot(a, b, axes).

      Example 1: When a and b are matrices (order 2), the case axes = 1 is equivalent to matrix multiplication.

      Example 2: When a and b are matrices (order 2), the case axes = [[1], [0]] is equivalent to matrix multiplication.

      Example 3: When a and b are matrices (order 2), the case axes=0 gives the outer product, a tensor of order 4.

      Example 4: Suppose that aijk and blmn represent two tensors of order 3. Then, contract(a, b, [[0], [2]]) is the order 4 tensor cjklm whose entry corresponding to the indices (j,k,l,m) is given by: cjklm = Σi aijk blmi .

      In general, order(c) = order(a) + order(b) - 2*len(axes[0]).

      Type Parameters:
      T - the datatype of the Operands, must be either TFloat32 or TFloat64
      Parameters:
      a - Operand of type TFloat32 or TFloat64.
      b - Operand with the same type as a.
      axis - sum over the last N axes of a and the first N axes of b in order. If axis=0, computes the outer product between a and b.
      Returns:
      A Operand with the same type as a.
      Throws:
      IllegalArgumentException - if a is not a float32 or float64 data type and if a and b are not the same data type
    • tensordot

      public <T extends TFloating> Operand<T> tensordot(Operand<T> a, Operand<T> b, Operand<TInt32> axes)
      Tensor contraction of a and b along specified axes and outer product.

      Tensordot (also known as tensor contraction) sums the product of elements from a and b over the indices specified by a_axes and b_axes. The lists a_axes and b_axes specify those pairs of axes along which to contract the tensors. The axis a_axes[i] of a must have the same dimension as axis b_axes[i] of b for all i in range(0, len(a_axes)). The lists a_axes and b_axes must have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passing axes=0.

      This operation corresponds to numpy.tensordot(a, b, axes).

      Example 1: When a and b are matrices (order 2), the case axes = 1 is equivalent to matrix multiplication.

      Example 2: When a and b are matrices (order 2), the case axes = [[1], [0]] is equivalent to matrix multiplication.

      Example 3: When a and b are matrices (order 2), the case axes=0 gives the outer product, a tensor of order 4.

      Example 4: Suppose that aijk and blmn represent two tensors of order 3. Then, contract(a, b, [[0], [2]]) is the order 4 tensor cjklm whose entry corresponding to the indices (j,k,l,m) is given by:

      cjklm = Σi aijk blmi .

      In general, order(c) = order(a) + order(b) - 2*len(axes[0]).

      Type Parameters:
      T - the datatype of the Operands, must be either TFloat32 or TFloat64
      Parameters:
      a - Operand of type TFloat32 or TFloat64.
      b - Operand with the same type as a.
      axes - If axes is a scalar, sum over the last N axes of a and the first N axes of b in order. If axes is a list, the first and second row contain the set of unique integers specifying axes along which the contraction is computed, for a and b, respectively. The number of axes for a and b must be equal. If axis=0, computes the outer product between a and b.
      Returns:
      A Operand with the same type as a.
      Throws:
      IllegalArgumentException - if a is not a float32 or float64 data type and if a and b are not the same data type
    • tensordot

      public <T extends TFloating> Operand<T> tensordot(Operand<T> a, Operand<T> b, int[] axes)
      Tensor contraction of a and b along specified axes and outer product.

      Tensordot (also known as tensor contraction) sums the product of elements from a and b over the indices specified by a_axes and b_axes. The lists a_axes and b_axes specify those pairs of axes along which to contract the tensors. The axis a_axes[i] of a must have the same dimension as axis b_axes[i] of b for all i in range(0, len(a_axes)). The lists a_axes and b_axes must have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passing axes=0.

      This operation corresponds to numpy.tensordot(a, b, axes).

      Example 1: When a and b are matrices (order 2), the case axes = 1 is equivalent to matrix multiplication.

      Example 2: When a andb are matrices (order 2), the case axes = [[1], [0]] is equivalent to matrix multiplication.

      Example 3: When a and b are matrices (order 2), the case axes=0 gives the outer product, a tensor of order 4.

      Example 4: Suppose that aijk and blmn represent two tensors of order 3. Then, contract(a, b, [[0], [2]]) is the order 4 tensor cjklm whose entry corresponding to the indices (j,k,l,m) is given by:

      cjklm = Σi aijk blmi .

      In general, order(c) = order(a) + order(b) - 2*len(axes[0]).

      Type Parameters:
      T - the datatype of the Operands, must be either TFloat32 or TFloat64
      Parameters:
      a - Operand of type TFloat32 or TFloat64.
      b - Operand with the same type as a.
      axes - the first and second row contain the set of unique integers specifying axes along which the contraction is computed, for a and b, respectively. The number of axes for a and b must be equal. I
      Returns:
      A Operand with the same type as a.
      Throws:
      IllegalArgumentException - if a is not a float32 or float64 data type and if a and b are not the same data type
    • tensordot

      public <T extends TFloating> Operand<T> tensordot(Operand<T> a, Operand<T> b, int[][] axes)
      Tensor contraction of a and b along specified axes and outer product.

      Tensordot (also known as tensor contraction) sums the product of elements from a and b over the indices specified by a_axes and b_axes. The lists a_axes and b_axes specify those pairs of axes along which to contract the tensors. The axis a_axes[i] of a must have the same dimension as axis b_axes[i] of b for all i in range(0, len(a_axes)). The lists a_axes and b_axes must have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passing axes=0.

      This operation corresponds to numpy.tensordot(a, b, axes).

      Example 1: When a and b are matrices (order 2), the case axes = 1 is equivalent to matrix multiplication.

      Example 2: When a andb are matrices (order 2), the case axes = [[1], [0]] is equivalent to matrix multiplication.

      Example 3: When a and b are matrices (order 2), the case axes=0 gives the outer product, a tensor of order 4.

      Example 4: Suppose that aijk and blmn represent two tensors of order 3. Then, contract(a, b, [[0], [2]]) is the order 4 tensor cjklm whose entry corresponding to the indices (j,k,l,m) is given by:

      cjklm = Σi aijk blmi .

      In general, order(c) = order(a) + order(b) - 2*len(axes[0]).

      Type Parameters:
      T - the datatype of the Operands, must be either TFloat32 or TFloat64
      Parameters:
      a - Operand of type TFloat32 or TFloat64.
      b - Operand with the same type as a.
      axes - the first and second row contain the set of unique integers specifying axes along which the contraction is computed, for a and b, respectively. The number of axes for a and b must be equal. I
      Returns:
      A Operand with the same type as a.
      Throws:
      IllegalArgumentException - if a is not a float32 or float64 data type and if a and b are not the same data type
    • tensordot

      public <T extends TFloating> Operand<T> tensordot(Operand<T> a, Operand<T> b, Operand<TInt32> aAxis, Operand<TInt32> bAxis)
      Tensor contraction of a and b along specified axes and outer product.

      Tensordot (also known as tensor contraction) sums the product of elements from a and b over the indices specified by a_axes and b_axes. The lists a_axes and b_axes specify those pairs of axes along which to contract the tensors. The axis a_axes[i] of a must have the same dimension as axis b_axes[i] of b for all i in range(0, len(a_axes)). The lists a_axes and b_axes must have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passing axes=0.

      This operation corresponds to numpy.tensordot(a, b, axes).

      Example 1: When a and b are matrices (order 2), the case axes = 1 is equivalent to matrix multiplication.

      Example 2: When a andb are matrices (order 2), the case axes = [[1], [0]] is equivalent to matrix multiplication.

      Example 3: When a and b are matrices (order 2), the case axes=0 gives the outer product, a tensor of order 4.

      Example 4: Suppose that aijk and blmn represent two tensors of order 3. Then, contract(a, b, [[0], [2]]) is the order 4 tensor cjklm whose entry corresponding to the indices (j,k,l,m) is given by:

      cjklm = Σi aijk blmi .

      In general, order(c) = order(a) + order(b) - 2*len(axes[0]).

      Type Parameters:
      T - the datatype of the Operands, must be either TFloat32 or TFloat64
      Parameters:
      a - Operand of type TFloat32 or TFloat64.
      b - Operand with the same type as a.
      aAxis - axes for the a Operand
      bAxis - axes for the b Operand
      Returns:
      A Operand with the same type as a.
      Throws:
      IllegalArgumentException - if a is not a float32 or float64 data type and if a and b are not the same data type
    • reduceLogSumExp

      public <T extends TFloating> Operand<T> reduceLogSumExp(Operand<T> input, int[] axes, boolean keepDims)
      Computes log(sum(exp(elements across dimensions of a tensor))). Reduces input_tensor along the dimensions given in axes.

      Reduces input along the dimensions given in axes. Unless keepdims is true, the rank of the tensor is reduced by 1 for each of the entries in axes, which must be unique. If keepdims is true, the reduced dimensions are retained with length 1. If axes has no entries, all dimensions are reduced, and a tensor with a single element is returned. This function is more numerically stable than log(sum(exp(input))). It avoids overflows caused by taking the exp of large inputs and underflows caused by taking the log of small inputs.

      Type Parameters:
      T - the data type for the input and the result
      Parameters:
      input - The tensor to reduce.
      axes - The dimensions to reduce. If null, reduces all dimensions. Must be in the range [-rank(input_tensor), rank(input_tensor)].
      keepDims - If true, retains reduced dimensions with length 1.
      Returns:
      The reduced tensor.