Tensorflow:''轮盘赌轮'选择

时间:2016-11-22 16:51:42

标签: tensorflow

我尝试在Tensorflow中实现轮盘赌选择。所以我从这开始:

x = tf.random_uniform([tf.shape(probabilities)[0]])
cumsum = tf.cumsum(probabilities, axis=1) # cumulative sum
b = tf.greater_equal(x, cumsum) # Boolean values now 
...
indices = tf.where(b) # this given indices for all the True values, I need only the first one per row
indices = indices[:,1] # we only need column index

对此有何建议?或者更好的程序来选择轮盘赌?

这是一个让它更清晰的小例子

probabilities = [[0.2 0.3 0.5],
                 [0.1 0.6 0.3],
                 [0.5 0.4 0.1]]

x = [0.27, 0.86, 0.73] # drawn randomly

然后我想要输出[1,2,1]

1 个答案:

答案 0 :(得分:0)

据我了解,您希望从多项分布中提取样本。要做到这一点,最简单的方法是使用tf.multinomial

samples = tf.multinomial(tf.log(probabilities), 1)

可能接着重塑:

samples_vector = tf.reshape(samples, [-1])