【问题标题】:How to draw samples from a categorical distribution in TensorFlow.js如何在 TensorFlow.js 中从分类分布中抽取样本
【发布时间】:2020-04-08 07:05:22
【问题描述】:

简而言之问题

Tensorflow 的Python 版本中,有一个tf.random.categorical() 方法可以从分类分布中抽取样本。但我在TensorFlow.js API 中找不到类似的方法。 那么,在 TensorFlow.js 中从分类分布中抽取样本的正确方法是什么?

问题详情

Text generation with an RNN 教程中,tf.random.categorical() 方法在generate_text() 函数中用于决定应该在 RNN 输入旁边传递什么字符以生成序列。

predicted_id = tf.random.categorical(predictions, num_samples=1)[-1,0].numpy()

我正在试验 TensorFlow.js 并尝试在浏览器中生成类似莎士比亚的“随机”写作。除了使用tf.random.categorical() 方法的步骤之外,本教程的所有部分似乎都可以很好地协同工作。

我想手动编写 tf.random.categorical() 函数的替代方案应该不会那么难,而且已经有几个 3rd-party JavaScript 库已经实现了这个功能,但是将它作为 TensorFlow 的一部分看起来很合乎逻辑.js API。

【问题讨论】:

    标签: tensorflow machine-learning tensorflow2.0 tensorflow.js tfjs-node


    【解决方案1】:

    我认为你可以改用tf.multinomial

    我查看了source code,并将nameseed 参数设置为None,我猜它与tf.multinomial 基本相同,只是进行了一些随机播种。

    【讨论】:

      猜你喜欢
      • 2020-11-01
      • 1970-01-01
      • 2022-11-23
      • 2021-01-20
      • 2021-03-10
      • 1970-01-01
      • 2015-08-28
      • 2020-02-13
      • 1970-01-01
      相关资源
      最近更新 更多