# Sequence Comparison and Indexing

TensorFlow provides several operations that you can use to add sequence comparison and index extraction to your graph. You can use these operations to determine sequence differences and determine the indexes of specific values in a tensor.

### tf.argmin(input, dimension, name=None)

Returns the index with the smallest value across dimensions of a tensor.

##### Args:
• input: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
• dimension: A Tensor. Must be one of the following types: int32, int64. int32, 0 <= dimension < rank(input). Describes which dimension of the input Tensor to reduce across. For vectors, use dimension = 0.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type int64.

### tf.argmax(input, dimension, name=None)

Returns the index with the largest value across dimensions of a tensor.

##### Args:
• input: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
• dimension: A Tensor. Must be one of the following types: int32, int64. int32, 0 <= dimension < rank(input). Describes which dimension of the input Tensor to reduce across. For vectors, use dimension = 0.
• name: A name for the operation (optional).
##### Returns:

A Tensor of type int64.

### tf.listdiff(x, y, out_idx=None, name=None)

Computes the difference between two lists of numbers or strings.

Given a list x and a list y, this operation returns a list out that represents all values that are in x but not in y. The returned list out is sorted in the same order that the numbers appear in x (duplicates are preserved). This operation also returns a list idx that represents the position of each out element in x. In other words:

out[i] = x[idx[i]] for i in [0, 1, ..., len(out) - 1]

For example, given this input:

x = [1, 2, 3, 4, 5, 6]
y = [1, 3, 5]


This operation would return:

out ==> [2, 4, 6]
idx ==> [1, 3, 5]

##### Args:
• x: A Tensor. 1-D. Values to keep.
• y: A Tensor. Must have the same type as x. 1-D. Values to remove.
• out_idx: An optional tf.DType from: tf.int32, tf.int64. Defaults to tf.int32.
• name: A name for the operation (optional).
##### Returns:

A tuple of Tensor objects (out, idx).

• out: A Tensor. Has the same type as x. 1-D. Values present in x but not in y.
• idx: A Tensor of type out_idx. 1-D. Positions of x values preserved in out.

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

### tf.unique(x, out_idx=None, name=None)

Finds unique elements in a 1-D tensor.

This operation returns a tensor y containing all of the unique elements of x sorted in the same order that they occur in x. This operation also returns a tensor idx the same size as x that contains the index of each value of x in the unique output y. In other words:

y[idx[i]] = x[i] for i in [0, 1,...,rank(x) - 1]

For example:

# tensor 'x' is [1, 1, 2, 4, 4, 4, 7, 8, 8]
y, idx = unique(x)
y ==> [1, 2, 4, 7, 8]
idx ==> [0, 0, 1, 2, 2, 2, 3, 4, 4]

##### Args:
• x: A Tensor. 1-D.
• out_idx: An optional tf.DType from: tf.int32, tf.int64. Defaults to tf.int32.
• name: A name for the operation (optional).
##### Returns:

A tuple of Tensor objects (y, idx).

• y: A Tensor. Has the same type as x. 1-D.
• idx: A Tensor of type out_idx. 1-D.

### tf.edit_distance(hypothesis, truth, normalize=True, name='edit_distance')

Computes the Levenshtein distance between sequences.

This operation takes variable-length sequences (hypothesis and truth), each provided as a SparseTensor, and computes the Levenshtein distance. You can normalize the edit distance by length of truth by setting normalize to true.

For example, given the following input:

# 'hypothesis' is a tensor of shape [2, 1] with variable-length values:
#   (0,0) = ["a"]
#   (1,0) = ["b"]
hypothesis = tf.SparseTensor(
[[0, 0, 0],
[1, 0, 0]],
["a", "b"]
(2, 1, 1))

# 'truth' is a tensor of shape [2, 2] with variable-length values:
#   (0,0) = []
#   (0,1) = ["a"]
#   (1,0) = ["b", "c"]
#   (1,1) = ["a"]
truth = tf.SparseTensor(
[[0, 1, 0],
[1, 0, 0],
[1, 0, 1],
[1, 1, 0]]
["a", "b", "c", "a"],
(2, 2, 2))

normalize = True


This operation would return the following:

# 'output' is a tensor of shape [2, 2] with edit distances normalized
# by 'truth' lengths.
output ==> [[inf, 1.0],  # (0,0): no truth, (0,1): no hypothesis
[0.5, 1.0]]  # (1,0): addition, (1,1): no hypothesis

##### Args:
• hypothesis: A SparseTensor containing hypothesis sequences.
• truth: A SparseTensor containing truth sequences.
• normalize: A bool. If True, normalizes the Levenshtein distance by length of truth.
• name: A name for the operation (optional).
##### Returns:

A dense Tensor with rank R - 1, where R is the rank of the SparseTensor inputs hypothesis and truth.

##### Raises:
• TypeError: If either hypothesis or truth are not a SparseTensor.

### tf.invert_permutation(x, name=None)

Computes the inverse permutation of a tensor.

This operation computes the inverse of an index permutation. It takes a 1-D integer tensor x, which represents the indices of a zero-based array, and swaps each value with its index position. In other words, for an output tensor y and an input tensor x, this operation computes the following:

y[x[i]] = i for i in [0, 1, ..., len(x) - 1]

The values must include 0. There can be no duplicate values or negative values.

For example:

# tensor x is [3, 4, 0, 2, 1]
invert_permutation(x) ==> [2, 4, 3, 0, 1]

##### Args:
• x: A Tensor. Must be one of the following types: int32, int64. 1-D.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as x. 1-D.