Basic Math Functions

TensorFlow provides several operations that you can use to add basic mathematical functions to your graph.

tf.add_n(inputs, name=None)

Adds all input tensors element-wise.

Args:
  • inputs: A list of Tensor objects, each with same shape and type.
  • name: A name for the operation (optional).
Returns:

A Tensor of same shape and type as the elements of inputs.

Raises:
  • ValueError: If inputs don't all have same shape and dtype or the shape cannot be inferred.

tf.abs(x, name=None)

Computes the absolute value of a tensor.

Given a tensor of real numbers x, this operation returns a tensor containing the absolute value of each element in x. For example, if x is an input element and y is an output element, this operation computes \(y = |x|\).

See tf.complex_abs() to compute the absolute value of a complex number.

Args:
  • x: A Tensor or SparseTensor of type float32, float64, int32, or int64.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor the same size and type as x with absolute values.


tf.negative(x, name=None)

Computes numerical negative value element-wise.

I.e., \(y = -x\).

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.sign(x, name=None)

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

y = sign(x) = -1 if x < 0; 0 if x == 0; 1 if x > 0.

For complex numbers, y = sign(x) = x / |x| if x != 0, otherwise y = 0.

Args:
  • x: A Tensor or SparseTensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor, respectively. Has the same type as x.


tf.reciprocal(x, name=None)

Computes the reciprocal of x element-wise.

I.e., \(y = 1 / x\).

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.square(x, name=None)

Computes square of x element-wise.

I.e., (y = x * x = x^2).

Args:
  • x: A Tensor or SparseTensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor. Has the same type as x.


tf.round(x, name=None)

Rounds the values of a tensor to the nearest integer, element-wise.

Rounds half to even. Also known as bankers rounding. If you want to round according to the current system rounding mode use tf::cint. For example:

# 'a' is [0.9, 2.5, 2.3, 1.5, -4.5]
tf.round(a) ==> [ 1.0, 2.0, 2.0, 2.0, -4.0 ]
Args:
  • x: A Tensor of type float32 or float64.
  • name: A name for the operation (optional).
Returns:

A Tensor of same shape and type as x.


tf.sqrt(x, name=None)

Computes square root of x element-wise.

I.e., (y = \sqrt{x} = x^{1/2}).

Args:
  • x: A Tensor or SparseTensor. Must be one of the following types: half, float32, float64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor, respectively. Has the same type as x.


tf.rsqrt(x, name=None)

Computes reciprocal of square root of x element-wise.

I.e., \(y = 1 / \sqrt{x}\).

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.pow(x, y, name=None)

Computes the power of one value to another.

Given a tensor x and a tensor y, this operation computes \(x^y\) for corresponding elements in x and y. For example:

# tensor 'x' is [[2, 2], [3, 3]]
# tensor 'y' is [[8, 16], [2, 3]]
tf.pow(x, y) ==> [[256, 65536], [9, 27]]
Args:
  • x: A Tensor of type float32, float64, int32, int64, complex64, or complex128.
  • y: A Tensor of type float32, float64, int32, int64, complex64, or complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor.


tf.exp(x, name=None)

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

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.log(x, name=None)

Computes natural logarithm of x element-wise.

I.e., \(y = \log_e x\).

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.log1p(x, name=None)

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

I.e., \(y = \log_e (1 + x)\).

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.ceil(x, name=None)

Returns element-wise smallest integer in not less than x.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.floor(x, name=None)

Returns element-wise largest integer not greater than x.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.maximum(x, y, name=None)

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

NOTE: Maximum supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.minimum(x, y, name=None)

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

NOTE: Minimum supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.cos(x, name=None)

Computes cos of x element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.sin(x, name=None)

Computes sin of x element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.lbeta(x, name='lbeta')

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

Given one-dimensional z = [z_0,...,z_{K-1}], we define

Beta(z) = \prod_j Gamma(z_j) / Gamma(\sum_j z_j)

And for n + 1 dimensional x with shape [N1, ..., Nn, K], we define lbeta(x)[i1, ..., in] = Log(|Beta(x[i1, ..., in, :])|). In other words, the last dimension is treated as the z vector.

Note that if z = [u, v], then Beta(z) = int_0^1 t^{u-1} (1 - t)^{v-1} dt, which defines the traditional bivariate beta function.

Args:
  • x: A rank n + 1 Tensor with type float, or double.
  • name: A name for the operation (optional).
Returns:

The logarithm of |Beta(x)| reducing along the last dimension.

Raises:
  • ValueError: If x is empty with rank one or less.

tf.tan(x, name=None)

Computes tan of x element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.acos(x, name=None)

Computes acos of x element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.asin(x, name=None)

Computes asin of x element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.atan(x, name=None)

Computes atan of x element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.lgamma(x, name=None)

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

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.digamma(x, name=None)

Computes Psi, the derivative of Lgamma (the log of the absolute value of

Gamma(x)), element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.erf(x, name=None)

Computes the Gauss error function of x element-wise.

Args:
  • x: A Tensor of SparseTensor. Must be one of the following types: half, float32, float64.
  • name: A name for the operation (optional).
Returns:

A Tensor or SparseTensor, respectively. Has the same type as x.


tf.erfc(x, name=None)

Computes the complementary error function of x element-wise.

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.squared_difference(x, y, name=None)

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

NOTE: SquaredDifference supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, int32, int64, complex64, complex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.igamma(a, x, name=None)

Compute the lower regularized incomplete Gamma function Q(a, x).

The lower regularized incomplete Gamma function is defined as:

P(a, x) = gamma(a, x) / Gamma(a) = 1 - Q(a, x)

where

gamma(a, x) = int_{0}^{x} t^{a-1} exp(-t) dt

is the lower incomplete Gamma function.

Note, above Q(a, x) (Igammac) is the upper regularized complete Gamma function.

Args:
  • a: A Tensor. Must be one of the following types: float32, float64.
  • x: A Tensor. Must have the same type as a.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as a.


tf.igammac(a, x, name=None)

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

The upper regularized incomplete Gamma function is defined as:

Q(a, x) = Gamma(a, x) / Gamma(a) = 1 - P(a, x)

where

Gamma(a, x) = int_{x}^{\infty} t^{a-1} exp(-t) dt

is the upper incomplete Gama function.

Note, above P(a, x) (Igamma) is the lower regularized complete Gamma function.

Args:
  • a: A Tensor. Must be one of the following types: float32, float64.
  • x: A Tensor. Must have the same type as a.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as a.


tf.zeta(x, q, name=None)

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

The Hurwitz zeta function is defined as:

\zeta(x, q) = \sum_{n=0}^{\infty} (q + n)^{-x}
Args:
  • x: A Tensor. Must be one of the following types: float32, float64.
  • q: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.


tf.polygamma(a, x, name=None)

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

The polygamma function is defined as:

\psi^{(n)} (x) = \frac{d^n}{dx^n} \psi(x)

where \(\psi(x)\) is the digamma function.

Args:
  • a: A Tensor. Must be one of the following types: float32, float64.
  • x: A Tensor. Must have the same type as a.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as a.


tf.betainc(a, b, x, name=None)

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

The regularized incomplete beta integral is defined as:

I_x(a, b) = \frac{B(x; a, b)}{B(a, b)}

where

B(x; a, b) = \int_0^x t^{a-1} (1 - t)^{b-1} dt

is the incomplete beta function and \(B(a, b)\) is the complete beta function.

Args:
  • a: A Tensor. Must be one of the following types: float32, float64.
  • b: A Tensor. Must have the same type as a.
  • x: A Tensor. Must have the same type as a.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as a.


tf.rint(x, name=None)

Returns element-wise integer closest to x.

If the result is midway between two representable values, the even representable is chosen. For example:

rint(-1.5) ==> -2.0
rint(0.5000001) ==> 1.0
rint([-1.7, -1.5, -0.2, 0.2, 1.5, 1.7, 2.0]) ==> [-2., -2., -0., 0., 2., 2., 2.]
Args:
  • x: A Tensor. Must be one of the following types: float32, float64.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as x.