Open In App

Tensorflow.js tf.setdiff1dAsync() Function

Last Updated : 06 Nov, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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

The tf.setdiff1dAsync() function is used to find the difference between two specified lists of numbers.

Let’s assume two Tensor “a” and “b” is given, then this function returns a Tensor out which represents all values present in “a” but not in “b”. The returned Tensor out is sorted in the same order in which “a” is represented. This function also returns a Tensor of indices of the number present in the Tensor out.

Syntax:

tf.setdiff1dAsync (a, b)

Parameters: This function accepts two parameters which are illustrated below:

  • a: It is a 1-D Tensor of values to keep.
  • b: It is a 1-D Tensor of values to exclude in the output. It should have same data type as “a”.

Example 1:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Initializing two Tensors
const a = [10, 20, 30, 40, 50, 60];
const b = [20, 40, 50];
 
// Calling the .setdiff1dAsync() function over
// the above two specified Tensors as parameters
// and returns the Tensor out and indices for
// the same
const [out, indices] = await tf.setdiff1dAsync(a, b);
 
// Getting the Tensor of values present in
// Tensor "a" but not in "b"
out.print();
 
// Getting the Tensor of indices for the
// returned Tensor's values
indices.print();


Output:

Tensor
   [10, 30, 60]
Tensor
   [0, 2, 5]

Example 2:

Javascript




// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
 
// Using two Tensors as the parameters for the
// .setdiff1dAsync() function and
// returns the Tensor out and indices
const [out, indices] = await
  tf.setdiff1dAsync([0.1, 0, 7.0, 7.1], [.1, 7]);
 
// Getting the Tensor of values present in
// Tensor first Tensor parameter
// but not in second one
out.print();
 
// Getting the Tensor of indices for the
// returned Tensor's values
indices.print();


Output:

Tensor
   [0, 7.0999999]
Tensor
   [1, 3]

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



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads