Sunday, November 17, 2024
Google search engine
HomeLanguagesJavascriptTensorflow.js tf.scatterND() Function

Tensorflow.js tf.scatterND() Function

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 .scatterND() function is used to form a different tensor by utilizing scattered updates to the individual slices or values inside a zero tensor of the stated shape tensor in accordance with the stated indices. Moreover, this function is negation of the tf.gatherND() function that takes slices or values from a stated tensor.

Syntax:  

tf.scatterND(indices, updates, shape)

Parameters:  

  • indices: It is the stated tensor that holds the indices towards the output tensor, and it can be of type tf.Tensor, TypedArray, or Array.
  • updates: It is the stated tensor that holds the values for the indices, and it can be of type tf.Tensor, TypedArray, or Array.
  • shape: It is the stated shape of the output tensor and 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 indices, updates and shape
const ind = tf.tensor2d([6, 5, 2], [3, 1], 'int32');
const updat = tf.tensor1d([1, 2, 3]);
const shp = [6];
  
// Calling tf.scatterND() method and
// Printing output
tf.scatterND(ind, updat, shp).print();


Output:

Tensor
    [0, 0, 3, 0, 0, 2]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.scatterND() method and
// Printing output
tf.scatterND(tf.tensor2d([5.4, 2.4], [2, 1], 'int32'), 
            tf.tensor1d([1.8, 4.2]), 
            [4]).print();


Output:

Tensor
    [0, 0, 4.1999998, 0]

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

Whether you’re preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, neveropen Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we’ve already empowered, and we’re here to do the same for you. Don’t miss out – check it out now!

RELATED ARTICLES

Most Popular

Recent Comments