Tensorflow:多项式尝试中结果的概率

Tensorflow: Probability of an outcome in multinomial try

我正在进行多项式尝试 - 我有一个概率向量 p=[p1, p2, p3] 和一个观察结果向量 n=[n1, n2, n3]

如何使用 TensorFlow 找到此类事件发生的概率?我也希望有一个适用于矩阵的解决方案(即我有一个张量,每行代表概率,还有一个张量,每行代表结果)。

您可以使用 tf.distributions.Multinomial:

import tensorflow as tf

probs = tf.constant([.2, .4, .6], tf.float32)
counts = tf.constant([1, 2, 3], tf.float32)
total_count = tf.reduce_sum(counts)
multinomial = tf.distributions.Multinomial(total_count, probs=probs)
prob_counts = multinomial.prob(counts)
with tf.Session() as sess:
    print(sess.run(prob_counts))

输出:

0.13888888

您也可以像这样一次操作多个发行版:

import tensorflow as tf

probs = tf.placeholder(tf.float32, [None, 3])
counts = tf.placeholder(tf.float32, [None, 3])
total_counts = tf.reduce_sum(counts, axis=1)
multinomial = tf.distributions.Multinomial(total_counts, probs=probs)
prob_counts = multinomial.prob(counts)

在这种情况下,probs 的每一行是一个概率分布,counts 的每一行是一个分布样本,prob_counts 的每个元素是每个样本的概率。