Open In App

Tensorflow.js tf.denseBincount() Function

Last Updated : 14 May, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Tensorflow.js is a Google-developed open-source toolkit for executing machine learning models and deep learning neural networks in the browser or on the node platform. It also enables developers to create machine learning models in JavaScript and utilize them directly in the browser or with Node.js.

The tf.denseBincount() function is used to construct a tensor of the specified size and data type. The sum of numbers at an index of the given weight tensor that corresponds to the index of the index number in the input tensor would be the tensor’s values.

Syntax:

tf.denseBincount (x, weights, size, binaryOutput?) 

Parameters:

  • x: This is the input tensor. It can be a 1D or a 2D tensor.
  • weights: It is the weights tensor which must be the same form as x, or it must be a length-0 tensor, in which case it acts as if all the weights are equal to 1.
  • size: The size of the output tensor.
  • binaryOutput: This is optional. It is used to specify whether the kernel should count the number of occurrences or the appearance. False is the default value.

Returns: tf.Tensor1D or tf.Tensor2D

Example 1:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const x = tf.tensor([7, 10, 3, 6, 8, 6, 4, 
    10, 5, 9, 2, 5, 9, 6, 1, 4, 10, 5,], 
    [1, 18], 'int32');
      
const weight = [];
const size = 11;
const out = tf.denseBincount(x, weight, size);
console.log(out.print());


Output:

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

Example 2:

Javascript




import * as tf from "@tensorflow/tfjs";
  
const x = tf.tensor([1, 2, 9, 6, 5, 4, 
    7, 4, 7, 4, 3], [1, 11], 'int32');
const weight = [0, 2, 5, 8, 9, 3, 5, 5, 3, 8, 2];
const size = 10;
const out = tf.denseBincount(x, weight, size);
console.log(out.print());


Output:

Tensor
   [[0, 0, 2, 2, 16, 9, 8, 8, 0, 5],]

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


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads