javascriptnode.jstensorflowmultinomial

TensorFlow.js tf.multinomial unexpected result [62083, 37917] instead of [75000, 25000]


I am using the tf.multinomial function from the TensorFlow.js library and I expect to get the result of [75000, 25000] but instead I am getting [62083, 37917]. Can someone explain why this is happening and how I can get the expected result?

I am a beginner in using TensorFlow.js and I am unable to find the proper information in the API documentation regarding the behavior of the tf.multinomial function

const tf = require('@tensorflow/tfjs-node');

const sample = []
const p = tf.tensor([0.75,0.25], [2], 'float32')
// or just p = [0.75,0.25]

for (let i = 0; i < 100000; i++) {
  const a = tf.multinomial(p, 1).arraySync()[0]
  sample[a] = (sample[a] || 0) + 1
}
console.log(sample) // should be something like [75000, 25000], but it is something like [62000, 38000]

Solution

  • Using the Math.log() function to calculate the probabilities and then passing them to the tf.multinomial() function can avoid this issue.

    For example, you can define the probabilities like this:

    const p = tf.tensor([Math.log(0.75), Math.log(0.25)], [2], 'float32')

    And the result will be correct (eg. [ 75043, 24957 ] or something like that).