Open In App

Tensorflow.js tf.unsortedSegmentSum() Function

Last Updated : 30 May, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is an open-source library that is developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment.

The .unsortedSegmentSum() function is used to calculate the sum through the parts of a tf.Tensor.

Syntax:  

tf.unsortedSegmentSum(x, segmentIds, numSegments)

Parameters:  

  • x: It is the stated tensor input that is to be added through its parts and it can be of type tf.Tensor, TypedArray, or Array.
  • segmentIds: It is the stated tf.Tensor1D whose order is equivalent to the order of x’s size through the axis. It maps every item of x to a slice. It can be of type tf.Tensor1D, TypedArray, or Array.
  • numSegments: It is the stated number of separate segmentIds, and it is of type number.

Return Value: It returns tf.Tensor object.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining tensor input, segmentIds 
// and numSegments 
const y = tf.tensor1d([5, 6, 7, 8]);
const segmIds = tf.tensor1d([3, 1, 2, 0], 'int32');
const numSegm = 4;
  
// Calling tf.unsortedSegmentSum() method
// And printing output
y.unsortedSegmentSum(segmIds, numSegm).print();


Output:

Tensor
    [8, 6, 7, 5]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.unsortedSegmentSum() method
var res = tf.unsortedSegmentSum(
    tf.tensor1d([1, 9]), 
    tf.tensor1d([1, 0], 'int32'), 3
);
  
// Printing output
res.print();


Output:

Tensor
    [9, 1, 0]

Reference: https://js.tensorflow.org/api/latest/#unsortedSegmentSum


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads