Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Sampling Bernoulli random variables in TensorFlow

Tags:

tensorflow

Given a 1D tensor containing the means of a Bernoulli distribution, how do I sample a corresponding 1D tensor with the given means?

TensorFlow only seems to have random_normal and random_uniform functions implemented. I could use something complicated like:

tf.ceil(tf.sub(tf.random_uniform((1, means.get_shape()[0])),means))

but the ceil function has no gradient defined in TensorFlow.

like image 421
user1994648 Avatar asked Jan 28 '26 05:01

user1994648


1 Answers

You can use tf.select, which is differentiable.

means = tf.constant([.3,.8])
a = tf.select(tf.random_uniform([1, 2])- means > 0.5, tf.ones([1,2]), tf.zeros([1,2]))
with tf.Session(''): a.eval()
like image 120
keveman Avatar answered Jan 30 '26 17:01

keveman