# Segmentation

TensorFlow provides several operations that you can use to perform common math computations on tensor segments. Here a segmentation is a partitioning of a tensor along the first dimension, i.e. it defines a mapping from the first dimension onto segment_ids. The segment_ids tensor should be the size of the first dimension, d0, with consecutive IDs in the range 0 to k, where k<d0. In particular, a segmentation of a matrix tensor is a mapping of rows to segments.

For example:

c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])
tf.segment_sum(c, tf.constant([0, 0, 1]))
==>  [[0 0 0 0]
[5 6 7 8]]


### tf.segment_sum(data, segment_ids, name=None)

Computes the sum along segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Computes a tensor such that $$output_i = \sum_j data_j$$ where sum is over j such that segment_ids[j] == i.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
• segment_ids: A Tensor. Must be one of the following types: int32, int64. A 1-D tensor whose rank is equal to the rank of data's first dimension. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.

### tf.segment_prod(data, segment_ids, name=None)

Computes the product along segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Computes a tensor such that $$output_i = \prod_j data_j$$ where the product is over j such that segment_ids[j] == i.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
• segment_ids: A Tensor. Must be one of the following types: int32, int64. A 1-D tensor whose rank is equal to the rank of data's first dimension. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.

### tf.segment_min(data, segment_ids, name=None)

Computes the minimum along segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Computes a tensor such that $$output_i = \min_j(data_j)$$ where min is over j such that segment_ids[j] == i.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• segment_ids: A Tensor. Must be one of the following types: int32, int64. A 1-D tensor whose rank is equal to the rank of data's first dimension. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.

### tf.segment_max(data, segment_ids, name=None)

Computes the maximum along segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Computes a tensor such that $$output_i = \max_j(data_j)$$ where max is over j such that segment_ids[j] == i.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• segment_ids: A Tensor. Must be one of the following types: int32, int64. A 1-D tensor whose rank is equal to the rank of data's first dimension. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.

### tf.segment_mean(data, segment_ids, name=None)

Computes the mean along segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Computes a tensor such that $$output_i = \frac{\sum_j data_j}{N}$$ where mean is over j such that segment_ids[j] == i and N is the total number of values summed.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• segment_ids: A Tensor. Must be one of the following types: int32, int64. A 1-D tensor whose rank is equal to the rank of data's first dimension. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.

### tf.unsorted_segment_sum(data, segment_ids, num_segments, name=None)

Computes the sum along segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Computes a tensor such that (output[i] = sum_{j...} data[j...] where the sum is over tuples j... such that segment_ids[j...] == i. Unlike SegmentSum, segment_ids need not be sorted and need not cover all values in the full range of valid values.

If the sum is empty for a given segment ID i, output[i] = 0.

num_segments should equal the number of distinct segment IDs.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64, int64, int32, uint8, uint16, int16, int8, complex64, complex128, qint8, quint8, qint32, half.
• segment_ids: A Tensor. Must be one of the following types: int32, int64. A tensor whose shape is a prefix of data.shape.
• num_segments: A Tensor of type int32.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for the first segment_ids.rank dimensions, which are replaced with a single dimension which has size num_segments.

### tf.sparse_segment_sum(data, indices, segment_ids, name=None)

Computes the sum along sparse segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Like SegmentSum, but segment_ids can have rank less than data's first dimension, selecting a subset of dimension 0, specified by indices.

For example:

c = tf.constant([[1,2,3,4], [-1,-2,-3,-4], [5,6,7,8]])

# Select two rows, one segment.
tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 0]))
==> [[0 0 0 0]]

# Select two rows, two segment.
tf.sparse_segment_sum(c, tf.constant([0, 1]), tf.constant([0, 1]))
==> [[ 1  2  3  4]
[-1 -2 -3 -4]]

# Select all rows, two segments.
tf.sparse_segment_sum(c, tf.constant([0, 1, 2]), tf.constant([0, 0, 1]))
==> [[0 0 0 0]
[5 6 7 8]]

# Which is equivalent to:
tf.segment_sum(c, tf.constant([0, 0, 1]))

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64, int32, int64, uint8, int16, int8, uint16, half.
• indices: A Tensor of type int32. A 1-D tensor. Has same rank as segment_ids.
• segment_ids: A Tensor of type int32. A 1-D tensor. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.

### tf.sparse_segment_mean(data, indices, segment_ids, name=None)

Computes the mean along sparse segments of a tensor.

Read the section on Segmentation for an explanation of segments.

Like SegmentMean, but segment_ids can have rank less than data's first dimension, selecting a subset of dimension 0, specified by indices.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64.
• indices: A Tensor of type int32. A 1-D tensor. Has same rank as segment_ids.
• segment_ids: A Tensor of type int32. A 1-D tensor. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.

### tf.sparse_segment_sqrt_n(data, indices, segment_ids, name=None)

Computes the sum along sparse segments of a tensor divided by the sqrt of N.

N is the size of the segment being reduced.

Read the section on Segmentation for an explanation of segments.

##### Args:
• data: A Tensor. Must be one of the following types: float32, float64.
• indices: A Tensor of type int32. A 1-D tensor. Has same rank as segment_ids.
• segment_ids: A Tensor of type int32. A 1-D tensor. Values should be sorted and can be repeated.
• name: A name for the operation (optional).
##### Returns:

A Tensor. Has the same type as data. Has same shape as data, except for dimension 0 which has size k, the number of segments.