首页 > 解决方案 > 具有急切执行的 TensorFlow 多项式分布

问题描述

我来自本教程,它基于来自我们的 RNN 的预测张量,在急切执行中使用多项分布来获得下一个字符的最终预测以生成文本。

# using a multinomial distribution to predict the character returned by the model
temperature = 0.5
predictions = predictions / temperature
predicted_id = tf.multinomial(predictions, num_samples=1)[-1,0].numpy()

我的问题是:

  1. 温度(这里是 0.5)不只是缩放所有预测,为什么它会影响多项式选择呢?

    [0.2, 0.4, 0.3, 0.1]/温度 = [0.4, 0.8, 0.6, 0.2]

    那么多项式不是对概率进行归一化吗?因此,在缩放时,我们只是增加每个字符的概率,限制为 1?

  2. [-1, 0].numpy() 有什么作用?我完全迷失了这个。

任何提示表示赞赏。

标签: pythontensorflowmultinomialeager-execution

解决方案


  1. [i, :] 表示所有类的非标准化对数概率

因此,首先概率越小,温度小于 1 的概率就越小。温度大于 1 的概率越大:

math.exp(0.4)/math.exp(0.8) = 0.670
math.exp(0.3)/ math.exp(0.6) = 0.7408
math.exp(0.2)/ math.exp(0.4) = 0.818
math.exp(0.1)/ math.exp(0.2) = 0.9048
  1. [-1, 0].numpy()只是得到多项式张量的值

如:

tf.multinomial(predictions, num_samples=1)
tf.Tensor([[3]], shape=(1, 1), dtype=int64)
to 3

推荐阅读