Open In App
Related Articles

Tensorflow.js tf.multinomial() Function

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
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 .multinomial() function is used to generate a tf.Tensor along with inputs that are dragged out of a multinomial distribution.

Syntax:  

tf.multinomial(logits, numSamples, seed?, normalized?)

Parameters:  

  • logits: It is a stated 1D array along with disorganized log expectancies, or a 2D array that has a shape [batchSize, numOutcomes] and it can be of type tf.Tensor1D, tf.Tensor2D, TypedArray, or Array.
  • numSamples: It is the stated number of samples that are to be dragged for every row section. It is of type number.
  • seed: It is the stated seed number and is an optional parameter of type number.
  • normalized: It checks if the given logits are organized true expectancies or not i.e. (sum to 1). The by default value is false and is an optional parameter of type Boolean.

Return Value: It returns tf.Tensor1D, or tf.Tensor2D.

Example 1:

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Defining logits
const logits = tf.tensor([35, 158]);
  
// Calling tf.multinomial() method and
// Printing output
tf.multinomial(logits, 4).print();

                    

Output:

Tensor
    [1, 1, 1, 1]

Example 2:

Javascript

// Importing the tensorflow.js library
import * as tf from "@tensorflow/tfjs"
  
// Calling tf.multinomial() method and
// Printing output
tf.multinomial(tf.tensor(
  [5.7, 8.7, NaN, 'a', null, 0]), 6).print();

                    

Output:

Tensor
    [5, 5, 5, 5, 5, 5]

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



Last Updated : 31 May, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads