Friday, September 26, 2025
HomeLanguagesPython – tensorflow.math.unsorted_segment_sum()

Python – tensorflow.math.unsorted_segment_sum()

TensorFlow is open-source Python library designed by Google to develop Machine Learning models and deep learning  neural networks.

unsorted_segment_sum() is used to find the sum of segments.

Syntax: tensorflow.math.unsorted_segment_sum( data, segment_ids, num_segments, name )

Parameter:

  • data: It is a tensor. Allowed dtypes are floating point or complex.
  • segment_ids: It’s 1-D tensor with sorted values. It’s size should be equal to  size of first dimension of data. It represents number of distinct segment IDs. Allowed dtypes are int32 and int64.
  • num_segments: It is a Tensor. Allowed dtypes are int32 and int64.
  • name(optional): It defines the name for the operation.

Return: It returns a tensor of dtype as x.

Example 1:

Python3




# importing the library
import tensorflow as tf
  
# Initializing the input tensor
data = tf.constant([1, 2, 3], dtype = tf.float64)
segment_ids = tf.constant([2, 2, 2])
  
# Printing the input tensor
print('data: ', data)
print('segment_ids: ', segment_ids)
  
# Calculating result
res = tf.math.unsorted_segment_sum(data, segment_ids, tf.constant(3))
  
# Printing the result
print('Result: ', res)


Output:

data:  tf.Tensor([1. 2. 3.], shape=(3, ), dtype=float64)
segment_ids:  tf.Tensor([2 2 2], shape=(3, ), dtype=int32)
Result:  tf.Tensor([0. 0. 6.], shape=(3, ), dtype=float64)




Example 2:

Python3




# importing the library
import tensorflow as tf
  
# Initializing the input tensor
data = tf.constant([[1, 2, 3], [4, 5, 6], [7, 8, 9]], dtype = tf.float64)
segment_ids = tf.constant([0, 0, 2])
  
# Printing the input tensor
print('data: ', data)
print('segment_ids: ', segment_ids)
  
# Calculating result
res = tf.math.unsorted_segment_sum(data, segment_ids, tf.constant(3))
  
# Printing the result
print('Result: ', res)


Output:

data:  tf.Tensor(
[[1. 2. 3.]
 [4. 5. 6.]
 [7. 8. 9.]], shape=(3, 3), dtype=float64)
segment_ids:  tf.Tensor([0 0 2], shape=(3, ), dtype=int32)
Result:  tf.Tensor(
[[5. 7. 9.]
 [0. 0. 0.]
 [7. 8. 9.]], shape=(3, 3), dtype=float64)
Dominic
Dominichttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Dominic
32321 POSTS0 COMMENTS
Milvus
84 POSTS0 COMMENTS
Nango Kala
6683 POSTS0 COMMENTS
Nicole Veronica
11854 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11910 POSTS0 COMMENTS
Shaida Kate Naidoo
6795 POSTS0 COMMENTS
Ted Musemwa
7071 POSTS0 COMMENTS
Thapelo Manthata
6757 POSTS0 COMMENTS
Umr Jansen
6762 POSTS0 COMMENTS