Module: tf.math

TensorFlow 2 version

Math Operations.

TensorFlow provides a variety of math functions including:

See: tf.linalg for matrix and tensor functions.

About Segmentation

TensorFlow provides several operations that you can use to perform common math computations on tensor segments. Here a segmentation is a partitioning of a tensor along the first dimension, i.e. it defines a mapping from the first dimension onto segment_ids. The segment_ids tensor should be the size of the first dimension, d0, with consecutive IDs in the range 0 to k, where k<d0. In particular, a segmentation of a matrix tensor is a mapping of rows to segments.

For example:

c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
tf.math.segment_sum(c, tf.constant([0, 0, 1]))
#  ==>  [[0 0 0 0]
#        [5 6 7 8]]

The standard segment_* functions assert that the segment indices are sorted. If you have unsorted indices use the equivalent unsorted_segment_ function. Thses functions take an additional argument num_segments so that the output tensor can be efficiently allocated.

c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
tf.math.unsorted_segment_sum(c, tf.constant([0, 1, 0]), num_segments=2)
# ==> [[ 6,  8, 10, 12],
#       [-1, -2, -3, -4]]

Functions

abs(...): Computes the absolute value of a tensor.

accumulate_n(...): Returns the element-wise sum of a list of tensors.

acos(...): Computes acos of x element-wise.

acosh(...): Computes inverse hyperbolic cosine of x element-wise.

add(...): Returns x + y element-wise.

add_n(...): Adds all input tensors element-wise.

angle(...): Returns the element-wise argument of a complex (or real) tensor.

argmax(...): Returns the index with the largest value across axes of a tensor. (deprecated arguments)

argmin(...): Returns the index with the smallest value across axes of a tensor. (deprecated arguments)

asin(...): Computes the trignometric inverse sine of x element-wise.

asinh(...): Computes inverse hyperbolic sine of x element-wise.

atan(...): Computes the trignometric inverse tangent of x element-wise.

atan2(...): Computes arctangent of y/x element-wise, respecting signs of the arguments.

atanh(...): Computes inverse hyperbolic tangent of x element-wise.

bessel_i0(...): Computes the Bessel i0 function of x element-wise.

bessel_i0e(...): Computes the Bessel i0e function of x element-wise.

bessel_i1(...): Computes the Bessel i1 function of x element-wise.

bessel_i1e(...): Computes the Bessel i1e function of x element-wise.

betainc(...): Compute the regularized incomplete beta integral \(I_x(a, b)\).

bincount(...): Counts the number of occurrences of each value in an integer array.

ceil(...): Returns element-wise smallest integer not less than x.

confusion_matrix(...): Computes the confusion matrix from predictions and labels.

conj(...): Returns the complex conjugate of a complex number.

cos(...): Computes cos of x element-wise.

cosh(...): Computes hyperbolic cosine of x element-wise.

count_nonzero(...): Computes number of nonzero elements across dimensions of a tensor. (deprecated arguments) (deprecated arguments)

cumprod(...): Compute the cumulative product of the tensor x along axis.

cumsum(...): Compute the cumulative sum of the tensor x along axis.

cumulative_logsumexp(...): Compute the cumulative log-sum-exp of the tensor x along axis.

digamma(...): Computes Psi, the derivative of Lgamma (the log of the absolute value of

divide(...): Computes Python style division of x by y.

divide_no_nan(...): Computes an unsafe divide which returns 0 if the y is zero.

equal(...): Returns the truth value of (x == y) element-wise.

erf(...): Computes the Gauss error function of x element-wise.

erfc(...): Computes the complementary error function of x element-wise.

exp(...): Computes exponential of x element-wise. \(y = e^x\).

expm1(...): Computes exp(x) - 1 element-wise.

floor(...): Returns element-wise largest integer not greater than x.

floordiv(...): Divides x / y elementwise, rounding toward the most negative integer.

floormod(...): Returns element-wise remainder of division. When x < 0 xor y < 0 is

greater(...): Returns the truth value of (x > y) element-wise.

greater_equal(...): Returns the truth value of (x >= y) element-wise.

igamma(...): Compute the lower regularized incomplete Gamma function P(a, x).

igammac(...): Compute the upper regularized incomplete Gamma function Q(a, x).

imag(...): Returns the imaginary part of a complex (or real) tensor.

in_top_k(...): Says whether the targets are in the top K predictions.

invert_permutation(...): Computes the inverse permutation of a tensor.

is_finite(...): Returns which elements of x are finite.

is_inf(...): Returns which elements of x are Inf.

is_nan(...): Returns which elements of x are NaN.

is_non_decreasing(...): Returns True if x is non-decreasing.

is_strictly_increasing(...): Returns True if x is strictly increasing.

l2_normalize(...): Normalizes along dimension axis using an L2 norm. (deprecated arguments)

lbeta(...): Computes \(ln(|Beta(x)|)\), reducing along the last dimension.

less(...): Returns the truth value of (x < y) element-wise.

less_equal(...): Returns the truth value of (x <= y) element-wise.

lgamma(...): Computes the log of the absolute value of Gamma(x) element-wise.

log(...): Computes natural logarithm of x element-wise.

log1p(...): Computes natural logarithm of (1 + x) element-wise.

log_sigmoid(...): Computes log sigmoid of x element-wise.

log_softmax(...): Computes log softmax activations. (deprecated arguments)

logical_and(...): Returns the truth value of x AND y element-wise.

logical_not(...): Returns the truth value of NOT x element-wise.

logical_or(...): Returns the truth value of x OR y element-wise.

logical_xor(...): Logical XOR function.

maximum(...): Returns the max of x and y (i.e. x > y ? x : y) element-wise.

minimum(...): Returns the min of x and y (i.e. x < y ? x : y) element-wise.

mod(...): Returns element-wise remainder of division. When x < 0 xor y < 0 is

multiply(...): Returns x * y element-wise.

multiply_no_nan(...): Computes the product of x and y and returns 0 if the y is zero, even if x is NaN or infinite.

negative(...): Computes numerical negative value element-wise.

nextafter(...): Returns the next representable value of x1 in the direction of x2, element-wise.

not_equal(...): Returns the truth value of (x != y) element-wise.

polygamma(...): Compute the polygamma function \(\psi^{(n)}(x)\).

polyval(...): Computes the elementwise value of a polynomial.

pow(...): Computes the power of one value to another.

real(...): Returns the real part of a complex (or real) tensor.

reciprocal(...): Computes the reciprocal of x element-wise.

reciprocal_no_nan(...): Performs a safe reciprocal operation, element wise.

reduce_all(...): Computes the "logical and" of elements across dimensions of a tensor. (deprecated arguments)

reduce_any(...): Computes the "logical or" of elements across dimensions of a tensor. (deprecated arguments)

reduce_euclidean_norm(...): Computes the Euclidean norm of elements across dimensions of a tensor.

reduce_logsumexp(...): Computes log(sum(exp(elements across dimensions of a tensor))). (deprecated arguments)

reduce_max(...): Computes the maximum of elements across dimensions of a tensor. (deprecated arguments)

reduce_mean(...): Computes the mean of elements across dimensions of a tensor.

reduce_min(...): Computes the minimum of elements across dimensions of a tensor. (deprecated arguments)

reduce_prod(...): Computes the product of elements across dimensions of a tensor. (deprecated arguments)

reduce_std(...): Computes the standard deviation of elements across dimensions of a tensor.

reduce_sum(...): Computes the sum of elements across dimensions of a tensor. (deprecated arguments)

reduce_variance(...): Computes the variance of elements across dimensions of a tensor.

rint(...): Returns element-wise integer closest to x.

round(...): Rounds the values of a tensor to the nearest integer, element-wise.

rsqrt(...): Computes reciprocal of square root of x element-wise.

scalar_mul(...): Multiplies a scalar times a Tensor or IndexedSlices object.

segment_max(...): Computes the maximum along segments of a tensor.

segment_mean(...): Computes the mean along segments of a tensor.

segment_min(...): Computes the minimum along segments of a tensor.

segment_prod(...): Computes the product along segments of a tensor.

segment_sum(...): Computes the sum along segments of a tensor.

sigmoid(...): Computes sigmoid of x element-wise.

sign(...): Returns an element-wise indication of the sign of a number.

sin(...): Computes sine of x element-wise.

sinh(...): Computes hyperbolic sine of x element-wise.

softmax(...): Computes softmax activations. (deprecated arguments)

softplus(...): Computes softplus: log(exp(features) + 1).

softsign(...): Computes softsign: features / (abs(features) + 1).

sqrt(...): Computes square root of x element-wise.

square(...): Computes square of x element-wise.

squared_difference(...): Returns (x - y)(x - y) element-wise.

subtract(...): Returns x - y element-wise.

tan(...): Computes tan of x element-wise.

tanh(...): Computes hyperbolic tangent of x element-wise.

top_k(...): Finds values and indices of the k largest entries for the last dimension.

truediv(...): Divides x / y elementwise (using Python 3 division operator semantics).

unsorted_segment_max(...): Computes the maximum along segments of a tensor.

unsorted_segment_mean(...): Computes the mean along segments of a tensor.

unsorted_segment_min(...): Computes the minimum along segments of a tensor.

unsorted_segment_prod(...): Computes the product along segments of a tensor.

unsorted_segment_sqrt_n(...): Computes the sum along segments of a tensor divided by the sqrt(N).

unsorted_segment_sum(...): Computes the sum along segments of a tensor.

xdivy(...): Returns 0 if x == 0, and x / y otherwise, elementwise.

xlogy(...): Returns 0 if x == 0, and x * log(y) otherwise, elementwise.

zero_fraction(...): Returns the fraction of zeros in value.

zeta(...): Compute the Hurwitz zeta function \(\zeta(x, q)\).