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.sub(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.mul(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, 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.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, 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.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 down for floating point.

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.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.