Tensorflow.js tf.where() Function Last Updated : 03 Sep, 2021 Comments Improve Suggest changes Like Article Like 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.where() function is used to returns the elements, either of first tensor or second tensor depending on the specified condition. If the given condition is true, it select from the first tensor else select form the second tensor. Syntax: tf.where (condition, a, b) Parameters: This function accepts three parameters which are illustrated below: condition: The input condition which should must have Boolean datatype.a: The first input tensor with dimension same as the size of condition.b: The second input tensor with shape that is compatible with "a". It should must have same data type as "a". Return Value: It returns the tensor of elements, either of first tensor or second tensor depending on the specified condition. Example 1: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Initializing a tensor of conditions const cond = tf.tensor1d([true, false, true, false], 'bool'); // Initializing two tensors const a = tf.tensor1d([-2 , 4, -6, 8]); const b = tf.tensor1d([2, -4, 6, -8]); // Calling the .where() function a.where(cond, b).print(); Output: Tensor [-2, -4, -6, -8] Example 2: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Using a tensor of condition along with two tensors of // same datatype values as the parameters of the .where() function tf.tensor1d([10, 20, 30, 40]). where(tf.tensor1d([true, false, true, false], 'bool'), tf.tensor1d([100, 200, 300, 400])).print(); Output: Tensor [10, 200, 30, 400] Reference:https://js.tensorflow.org/api/1.0.0/#where Comment More infoAdvertise with us Next Article Tensorflow.js tf.where() Function K Kanchan_Ray Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Tensorflow.js tf.whereAsync() Function 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.whereAsync() function is used to return the 2-D tensor of coordinates of true elements for the specified condition. In the retu 2 min read Tensorflow.js tf.zeros() Function 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.zeros() function is used to create a new tensor with all elements set to zero. Syntax: tf.zeros(shape, dataType) Parameters: sh 2 min read Tensorflow.js tf.zerosLike() Function 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.zeroslIke() is used to create a tf.tensor with all elements set to '0' with the same shape as the given tensor by passing the p 3 min read Tensorflow.js tf.variable() Function 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. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js. The tf.variable() function i 2 min read Tensorflow.js tf.unique() Function Tensorflow.js is an open-source library that is being developed by Google for running machine learning models as well as deep learning neural networks in the browser or node environment. The .unique() function is used to find unique elements along an axis of a tensor. Â Syntax: tf.unique (x, axis?) 2 min read Like