Tensorflow.js tf.gather() Function Last Updated : 26 May, 2021 Comments Improve Suggest changes Like Article Like 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 .gather() function is used to collect the fragments from the stated tensor x's axis as per the stated indices. Syntax: tf.gather(x, indices, axis?, batchDims?) Parameters: x: It is the stated input tensor whose fragments are to be collected, and it can be of type tf.Tensor, TypedArray, or Array.indices: It is the stated indices of the values which are to be pulled out, and it can be of type tf.Tensor, TypedArray, or Array.axis: It is the stated axis above which the values are to be selected. The by default value is zero, and it is of type number. However, this parameter is optional.batchDims: It is the stated number of batch sizes and should be less than or equal to stated rank i.e. indices. It's by default value is zero. Moreover, the output returned must have the shape of x.shape[:axis] + indices.shape[batchDims:] + x.shape[axis + 1:]. It is of type number and is optional. Return Value: It returns tf.Tensor object. Example 1: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining tensor input and indices const y = tf.tensor1d([1, 6, 7, 8]); const ind = tf.tensor1d([1, 6, 2], 'int32'); // Calling tf.gather() method and // Printing output y.gather(ind).print(); Output: Tensor [6, NaN, 7] Example 2: JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining tensor input, indices, axis, // and batchdims const y = tf.tensor2d([7, 8, 12, 13], [4, 1]); const ind = tf.tensor1d([2, 3, 0], 'int32'); const axis = 1; const batchdims = -1; // Calling tf.gather() method var res = tf.gather(y, ind, axis, batchdims); // Printing output res.print(); Output: Tensor [[12 , 13 , 7 ], [13 , NaN, 8 ], [NaN, NaN, 12], [NaN, NaN, 13]] Reference: https://js.tensorflow.org/api/latest/#gather Comment More infoAdvertise with us Next Article Tensorflow.js tf.gatherND() Function N nidhi1352singh Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Tensorflow.js tf.gatherND() 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 also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf. 2 min read Tensorflow tf.sub() Function The tf.sub() function returns the subtraction of two tf.Tensor objects element wise. The tf.Tensor object represents the multidimensional array of numbers. Syntax: tf.sub( a, b ) Parameters: a: It contains the first tf.Tensor object. The value of this parameter can be tf.TensorTypedArray|Array.b: It 2 min read Tensorflow.js tf.data.generator() 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 also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or in Node.js. The tf. 2 min read Tensorflow.js tf.sum() 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.sum() function is used to calculate sum of the elements of a specified Tensor across its dimension. It reduces the given input 2 min read Tensorflow.js tf.all() 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 also helps the developers to develop ML models in JavaScript language and can use ML directly in the browser or Node.js. The tf. al 1 min read Tensorflow.js tf.min() 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.min() function is used to calculate the minimum value from the specified Tensor across its dimension. It reduces the given inpu 2 min read Like