Weighted random tensor select in tensorflow -
i have list of tensors , list representing probability mass function. how can each session run tell tensorflow randomly pick 1 tensor according probability mass function.
i see few possible ways that:
one packing list of tensors in rank 1 higher, , select 1 slice & squeeze based on tensorflow variable i'm going assign correct index. performance penalty approach? tensorflow evaluate other, non-needed tensors?
another using tf.case in similar fashion before me picking 1 tensor out of many. same question -> what's performance penalty since plan on having quite few(~100s) conditional statements per 1 graph run.
is there better way of doing this?
i think should use tf.multinomial(logits, num_samples)
.
say have:
- a batch of tensors of shape
[batch_size, num_features]
- a probability distribution of shape
[batch_size]
you want output:
- 1 example batch of tensors, of shape
[1, num_features]
batch_tensors = tf.constant([[0., 1., 2.], [3., 4., 5.]]) # shape [batch_size, num_features] probabilities = tf.constant([0.7, 0.3]) # shape [batch_size] # need convert probabilities log_probabilities , reshape [1, batch_size] rescaled_probas = tf.expand_dims(tf.log(probabilities), 0) # shape [1, batch_size] # can draw 1 example distribution (we draw more) indice = tf.multinomial(rescaled_probas, num_samples=1) output = tf.gather(batch_tensors, tf.squeeze(indice, [0]))
what's performance penalty since plan on having quite few(~100s) conditional statements per 1 graph run?
if want multiple draws, should in 1 run increasing parameter num_samples
. can gather these num_samples
examples in 1 run tf.gather
.
Comments
Post a Comment