2016-11-22 74 views
0

我尝试实现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]

回答

0

据我所知,你想绘制多项分布的样本。要做到这一点,最简单的是简单地使用tf.multinomial:通过重塑

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

随后可能:

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