Arithmetic Operators

TensorFlow provides several operations that you can use to add basic arithmetic operators to your graph.

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

Returns x + y element-wise.

NOTE: Add supports broadcasting. AddN does not. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, int16, int32, int64, complex64, complex128, string.
  • 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.subtract(x, y, name=None)

Returns x - y element-wise.

NOTE: Sub 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.multiply(x, y, name=None)

Returns x * y element-wise.

NOTE: Mul supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, 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.scalar_mul(scalar, x)

Multiplies a scalar times a Tensor or IndexedSlices object.

Intended for use in gradient code which might deal with IndexedSlices objects, which are easy to multiply by a scalar but more expensive to multiply with arbitrary tensors.

Args:
  • scalar: A 0-D scalar Tensor. Must have known shape.
  • x: A Tensor or IndexedSlices to be scaled.
Returns:

scalar * x of the same type (Tensor or IndexedSlices) as x.

Raises:
  • ValueError: if scalar is not a 0-D scalar.

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

Returns x / y element-wise.

NOTE: Div supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, 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.divide(x, y, name=None)

Computes Python style division of x by y.


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

Divides x / y elementwise, always producing floating point results.

The same as tf.div for floating point arguments, but casts integer arguments to floating point before dividing so that the result is always floating point. This op is generated by normal x / y division in Python 3 and in Python 2.7 with from __future__ import division. If you want integer division that rounds down, use x // y or tf.floordiv.

x and y must have the same numeric type. If the inputs are floating point, the output will have the same type. If the inputs are integral, the inputs are cast to float32 for int8 and int16 and float64 for int32 and int64 (matching the behavior of Numpy).

Args:
  • x: Tensor numerator of numeric type.
  • y: Tensor denominator of numeric type.
  • name: A name for the operation (optional).
Returns:

x / y evaluated in floating point.

Raises:
  • TypeError: If x and y have different dtypes.

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

Divides x / y elementwise, rounding toward the most negative integer.

The same as tf.div(x,y) for integers, but uses tf.floor(tf.div(x,y)) for floating point arguments so that the result is always an integer (though possibly an integer represented as floating point). This op is generated by x // y floor division in Python 3 and in Python 2.7 with from __future__ import division.

Note that for efficiency, floordiv uses C semantics for negative numbers (unlike Python and Numpy).

x and y must have the same type, and the result will have the same type as well.

Args:
  • x: Tensor numerator of real numeric type.
  • y: Tensor denominator of real numeric type.
  • name: A name for the operation (optional).
Returns:

x / y rounded down (except possibly towards zero for negative integers).

Raises:
  • TypeError: If the inputs are complex.

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

Returns x / y element-wise for real types.

If x and y are reals, this will return the floating-point division.

NOTE: Div supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, 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.truncatediv(x, y, name=None)

Returns x / y element-wise for integer types.

Truncation designates that negative numbers will round fractional quantities toward zero. I.e. -7 / 5 = 1. This matches C semantics but it is different than Python semantics. See FloorDiv for a division function that matches Python Semantics.

NOTE: TruncateDiv supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, 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.floor_div(x, y, name=None)

Returns x // y element-wise.

NOTE: FloorDiv supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: half, float32, float64, uint8, int8, uint16, int16, 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.truncatemod(x, y, name=None)

Returns element-wise remainder of division. This emulates C semantics where

true, this follows C semantics in that the result here is consistent with a flooring divide. E.g. floor(x / y) * y + mod(x, y) = x.

NOTE: Mod supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: int32, int64, float32, float64.
  • 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.floormod(x, y, name=None)

Returns element-wise remainder of division. When x < 0 xor y < 0 is

true, this follows Python semantics in that the result here is consistent with a flooring divide. E.g. floor(x / y) * y + mod(x, y) = x.

NOTE: FloorMod supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: int32, int64, float32, float64.
  • 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.mod(x, y, name=None)

Returns element-wise remainder of division.

NOTE: Mod supports broadcasting. More about broadcasting here

Args:
  • x: A Tensor. Must be one of the following types: int32, int64, float32, float64.
  • 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.cross(a, b, name=None)

Compute the pairwise cross product.

a and b must be the same shape; they can either be simple 3-element vectors, or any shape where the innermost dimension is 3. In the latter case, each pair of corresponding 3-element vectors is cross-multiplied independently.

Args:
  • a: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half. A tensor containing 3-element vectors.
  • b: A Tensor. Must have the same type as a. Another tensor, of same type and shape as a.
  • name: A name for the operation (optional).
Returns:

A Tensor. Has the same type as a. Pairwise cross product of the vectors in a and b.