Tensorflow.js tf.unique() Function Last Updated : 30 May, 2021 Comments Improve Suggest changes Like Article Like Report 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?) Parameters : x: It is a first tensor input that can be of type tf.Tensor, or TypedArray, or Array. Parameters can be of these types (int32, string, bool).axis ( number ): It is a second tensor input which is optional. The axis of the tensor is used to find the unique elements. Return value : It returns the values and indices. Example 1: In this example, we are defining an input tensor of integer type and then printing the unique value of it. For creating an input tensor, we are utilizing the .tensor1d() method and in order to print the output we are using the .print() method. JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining tensor input elements const a = tf.tensor1d([1, 1, 2, 2, 4, 4, 7, 7, 8]); // Calling unique() method const {values, indices} = tf.unique(a); // Printing the values and indices values.print(); // [1, 2, 4, 7, 8,] indices.print(); // [0, 0, 1, 1, 2, 2, 3, 3, 4] Output: Tensor [1, 2, 4, 7, 8] Tensor [0, 0, 1, 1, 2, 2, 3, 3, 4] Example 2: In this example axis parameter has been used. For creating an input tensor, we are utilizing the .tensor2d() method and in order to print the output we are using the .print() method. JavaScript // Importing the tensorflow.js library import * as tf from "@tensorflow/tfjs" // Defining tensor input elements const a = tf.tensor2d([[0, 0, 0], [2, 0, 0], [2, 0, 0]]); // Calling the unique() method const {values, indices} = tf.unique(a, 0) // Printing the values and indices values.print(); // [[1, 0, 0], // [2, 0, 0]] indices.print(); // [0, 0, 1] Output: Tensor [[0, 0, 0], [2, 0, 0]] Tensor [0, 1, 1] Reference: https://js.tensorflow.org/api/latest/#unique Comment More infoAdvertise with us Next Article Tensorflow.js tf.unique() Function S sweetyty Follow Improve Article Tags : JavaScript Web Technologies Tensorflow.js Similar Reads Tensorflow.js tf.tile() Function TensorFlow.js is a library for machine learning in JavaScript. It helps developers to develop ML models in JavaScript, and use ML directly in the browser or in Node.js. The tf.tile() function is used to create a Tensor by repeating the number of times given by reps. Syntax: tf.tile(x, reps)Note: Thi 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.unstack() 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. 1 min read Tensorflow.js tf.where() 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.where() function is used to returns the elements, either of first tensor or second tensor depending on the specified condition. 2 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.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.transpose() 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.transpose() function is used to perform a regular matrix transpose operation on the specified 2-D input tensor. Syntax: tf.tran 1 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.eye() Function Tensorflow.js is an open-source library for creating machine learning models in Javascript that allows users to run the models directly in the browser. The tf.eye() is a function defined in the class tf.Tensor. Itâs used to create an identity matrix of specified rows and columns. An identity matrix 3 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 Like