Class MathOps
-
Method Summary
Modifier and TypeMethodDescriptionCreates an Operand that has all axes contained in the Operand's shape.confusionMatrix(Operand<T> labels, Operand<T> predictions) Computes the confusion matrix from predictions and labels.confusionMatrix(Operand<T> labels, Operand<T> predictions, Operand<T> weights) Computes the confusion matrix from predictions and labels.confusionMatrix(Operand<T> labels, Operand<T> predictions, Operand<T> weights, Operand<TInt64> numClasses) Computes the confusion matrix from predictions and labels.l2Normalize(Operand<T> x, int[] axis) Normalizes along dimension axis using an L2 norm.reduceLogSumExp(Operand<T> input, int[] axes, boolean keepDims) Computes log(sum(exp(elements across dimensions of a tensor))).Tensor contraction of a and b along specified axes and outer product.Tensor contraction of a and b along specified axes and outer product.Tensor contraction of a and b along specified axes and outer product.Tensor contraction of a and b along specified axes and outer product.Tensor contraction of a and b along specified axes and outer product.
-
Method Details
-
l2Normalize
Normalizes along dimension axis using an L2 norm.- Type Parameters:
T- the data type for the input and the result- Parameters:
x- the inputaxis- Dimension along which to normalize.- Returns:
- the normalized values based on L2 norm
-
confusionMatrix
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], wherenis 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
numClassesis null, thennumClasseswill be set to one plus the maximum value in either predictions or labels. Class labels are expected to start at 0. For example, ifnumClassesis 3, then the possible labels would be[0, 1, 2].If
weightsis 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
typewith shape[n, n]representing the confusion matrix, wherenis 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 ifweightsis not null and its shape doesn't matchpredictions.
-
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], wherenis 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
numClassesis null, thennumClasseswill be set to one plus the maximum value in either predictions or labels. Class labels are expected to start at 0. For example, ifnumClassesis 3, then the possible labels would be[0, 1, 2].If
weightsis 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 matchespredictions.- Returns:
- An Operand of type
typewith shape[n, n]representing the confusion matrix, wherenis 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 ifweightsis not null and its shape doesn't matchpredictions.
-
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], wherenis 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
numClassesis null, thennumClasseswill be set to one plus the maximum value in either predictions or labels. Class labels are expected to start at 0. For example, ifnumClassesis 3, then the possible labels would be[0, 1, 2].If
weightsis 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 matchespredictions.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
typewith shape[n, n]representing the confusion matrix, wherenis 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 ifweightsis not null and its shape doesn't matchpredictions.
-
allAxes
-
tensordot
Tensor contraction of a and b along specified axes and outer product.Tensordot (also known as tensor contraction) sums the product of elements from
aandbover the indices specified bya_axesandb_axes. The listsa_axesandb_axesspecify those pairs of axes along which to contract the tensors. The axisa_axes[i]ofamust have the same dimension as axisb_axes[i]ofbfor alliinrange(0, len(a_axes)). The listsa_axesandb_axesmust have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passingaxes=0.This operation corresponds to
numpy.tensordot(a, b, axes).Example 1: When
aandbare matrices (order 2), the caseaxes = 1is equivalent to matrix multiplication.Example 2: When
aandbare matrices (order 2), the caseaxes = [[1], [0]]is equivalent to matrix multiplication.Example 3: When
aandbare matrices (order 2), the caseaxes=0gives 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-Operandof typeTFloat32orTFloat64.b-Operandwith the same type asa.axis- sum over the last N axes of a and the first N axes of b in order. Ifaxis=0, computes the outer product betweenaandb.- Returns:
- A
Operandwith the same type asa. - Throws:
IllegalArgumentException- if a is not a float32 or float64 data type and if a and b are not the same data type
-
tensordot
Tensor contraction of a and b along specified axes and outer product.Tensordot (also known as tensor contraction) sums the product of elements from
aandbover the indices specified bya_axesandb_axes. The listsa_axesandb_axesspecify those pairs of axes along which to contract the tensors. The axisa_axes[i]ofamust have the same dimension as axisb_axes[i]ofbfor alliinrange(0, len(a_axes)). The listsa_axesandb_axesmust have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passingaxes=0.This operation corresponds to
numpy.tensordot(a, b, axes).Example 1: When
aandbare matrices (order 2), the caseaxes = 1is equivalent to matrix multiplication.Example 2: When
aandbare matrices (order 2), the caseaxes = [[1], [0]]is equivalent to matrix multiplication.Example 3: When
aandbare matrices (order 2), the caseaxes=0gives 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-Operandof typeTFloat32orTFloat64.b-Operandwith the same type asa.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, foraandb, respectively. The number of axes foraandbmust be equal. Ifaxis=0, computes the outer product betweenaandb.- Returns:
- A
Operandwith the same type asa. - Throws:
IllegalArgumentException- if a is not a float32 or float64 data type and if a and b are not the same data type
-
tensordot
Tensor contraction of a and b along specified axes and outer product.Tensordot (also known as tensor contraction) sums the product of elements from
aandbover the indices specified bya_axesandb_axes. The listsa_axesandb_axesspecify those pairs of axes along which to contract the tensors. The axisa_axes[i]ofamust have the same dimension as axisb_axes[i]ofbfor alliinrange(0, len(a_axes)). The listsa_axesandb_axesmust have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passingaxes=0.This operation corresponds to
numpy.tensordot(a, b, axes).Example 1: When
aandbare matrices (order 2), the caseaxes = 1is equivalent to matrix multiplication.Example 2: When
aandbare matrices (order 2), the caseaxes = [[1], [0]]is equivalent to matrix multiplication.Example 3: When
aandbare matrices (order 2), the caseaxes=0gives 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-Operandof typeTFloat32orTFloat64.b-Operandwith the same type asa.axes- the first and second row contain the set of unique integers specifying axes along which the contraction is computed, foraandb, respectively. The number of axes foraandbmust be equal. I- Returns:
- A
Operandwith the same type asa. - Throws:
IllegalArgumentException- if a is not a float32 or float64 data type and if a and b are not the same data type
-
tensordot
Tensor contraction of a and b along specified axes and outer product.Tensordot (also known as tensor contraction) sums the product of elements from
aandbover the indices specified bya_axesandb_axes. The listsa_axesandb_axesspecify those pairs of axes along which to contract the tensors. The axisa_axes[i]ofamust have the same dimension as axisb_axes[i]ofbfor alliinrange(0, len(a_axes)). The listsa_axesandb_axesmust have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passingaxes=0.This operation corresponds to
numpy.tensordot(a, b, axes).Example 1: When
aandbare matrices (order 2), the caseaxes = 1is equivalent to matrix multiplication.Example 2: When
aandbare matrices (order 2), the caseaxes = [[1], [0]]is equivalent to matrix multiplication.Example 3: When
aandbare matrices (order 2), the caseaxes=0gives 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-Operandof typeTFloat32orTFloat64.b-Operandwith the same type asa.axes- the first and second row contain the set of unique integers specifying axes along which the contraction is computed, foraandb, respectively. The number of axes foraandbmust be equal. I- Returns:
- A
Operandwith the same type asa. - 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
aandbover the indices specified bya_axesandb_axes. The listsa_axesandb_axesspecify those pairs of axes along which to contract the tensors. The axisa_axes[i]ofamust have the same dimension as axisb_axes[i]ofbfor alliinrange(0, len(a_axes)). The listsa_axesandb_axesmust have identical length and consist of unique integers that specify valid axes for each of the tensors. Additionally outer product is supported by passingaxes=0.This operation corresponds to
numpy.tensordot(a, b, axes).Example 1: When
aandbare matrices (order 2), the caseaxes = 1is equivalent to matrix multiplication.Example 2: When
aandbare matrices (order 2), the caseaxes = [[1], [0]]is equivalent to matrix multiplication.Example 3: When
aandbare matrices (order 2), the caseaxes=0gives 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-Operandof typeTFloat32orTFloat64.b-Operandwith the same type asa.aAxis- axes for the a OperandbAxis- axes for the b Operand- Returns:
- A
Operandwith the same type asa. - 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))). Reducesinput_tensoralong the dimensions given inaxes.Reduces
inputalong the dimensions given inaxes. Unlesskeepdimsis true, the rank of the tensor is reduced by 1 for each of the entries inaxes, which must be unique. Ifkeepdimsis true, the reduced dimensions are retained with length 1. Ifaxeshas no entries, all dimensions are reduced, and a tensor with a single element is returned. This function is more numerically stable thanlog(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.
-