Comparison Operators

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

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

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

NOTE: Equal 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, quint8, qint8, qint32, string, bool, complex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

A Tensor of type bool.


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

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

NOTE: NotEqual 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, quint8, qint8, qint32, string, bool, complex128.
  • y: A Tensor. Must have the same type as x.
  • name: A name for the operation (optional).
Returns:

A Tensor of type bool.


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

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

NOTE: Less supports broadcasting. More about broadcasting here

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

A Tensor of type bool.


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

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

NOTE: LessEqual supports broadcasting. More about broadcasting here

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

A Tensor of type bool.


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

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

NOTE: Greater supports broadcasting. More about broadcasting here

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

A Tensor of type bool.


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

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

NOTE: GreaterEqual supports broadcasting. More about broadcasting here

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

A Tensor of type bool.


tf.select(condition, t, e, name=None)

Selects elements from t or e, depending on condition.

The t, and e tensors must all have the same shape, and the output will also have that shape. The condition tensor must be a scalar if t and e are scalars. If t and e are vectors or higher rank, then condition must be either a vector with size matching the first dimension of t, or must have the same shape as t.

The condition tensor acts as a mask that chooses, based on the value at each element, whether the corresponding element / row in the output should be taken from t (if true) or e (if false).

If condition is a vector and t and e are higher rank matrices, then it chooses which row (outer dimension) to copy from t and e. If condition has the same shape as t and e, then it chooses which element to copy from t and e.

For example:

# 'condition' tensor is [[True,  False]
#                        [False, True]]
# 't' is [[1, 2],
#         [3, 4]]
# 'e' is [[5, 6],
#         [7, 8]]
select(condition, t, e) ==> [[1, 6],
                             [7, 4]]

# 'condition' tensor is [True, False]
# 't' is [[1, 2],
#         [3, 4]]
# 'e' is [[5, 6],
#         [7, 8]]
select(condition, t, e) ==> [[1, 2],
                             [7, 8]]

Args:
  • condition: A Tensor of type bool.
  • t: A Tensor which may have the same shape as condition. If condition is rank 1, t may have higher rank, but its first dimension must match the size of condition.
  • e: A Tensor with the same type and shape as t.
  • name: A name for the operation (optional).
Returns:

A Tensor with the same type and shape as t and e.


tf.where(input, name=None)

Returns locations of true values in a boolean tensor.

This operation returns the coordinates of true elements in input. The coordinates are returned in a 2-D tensor where the first dimension (rows) represents the number of true elements, and the second dimension (columns) represents the coordinates of the true elements. Keep in mind, the shape of the output tensor can vary depending on how many true values there are in input. Indices are output in row-major order.

For example:

# 'input' tensor is [[True, False]
#                    [True, False]]
# 'input' has two true values, so output has two coordinates.
# 'input' has rank of 2, so coordinates have two indices.
where(input) ==> [[0, 0],
                  [1, 0]]

# `input` tensor is [[[True, False]
#                     [True, False]]
#                    [[False, True]
#                     [False, True]]
#                    [[False, False]
#                     [False, True]]]
# 'input' has 5 true values, so output has 5 coordinates.
# 'input' has rank of 3, so coordinates have three indices.
where(input) ==> [[0, 0, 0],
                  [0, 1, 0],
                  [1, 0, 1],
                  [1, 1, 1],
                  [2, 1, 1]]
Args:
  • input: A Tensor of type bool.
  • name: A name for the operation (optional).
Returns:

A Tensor of type int64.