2016-11-22 52 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])