Tensorflow.js tf.unsortedSegmentSum() Function
Last Updated :
30 May, 2021
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
import * as tf from "@tensorflow/tfjs"
const y = tf.tensor1d([5, 6, 7, 8]);
const segmIds = tf.tensor1d([3, 1, 2, 0], 'int32' );
const numSegm = 4;
y.unsortedSegmentSum(segmIds, numSegm).print();
|
Output:
Tensor
[8, 6, 7, 5]
Example 2:
Javascript
import * as tf from "@tensorflow/tfjs"
var res = tf.unsortedSegmentSum(
tf.tensor1d([1, 9]),
tf.tensor1d([1, 0], 'int32' ), 3
);
res.print();
|
Output:
Tensor
[9, 1, 0]
Reference: https://js.tensorflow.org/api/latest/#unsortedSegmentSum
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...