user1994648
user1994648

Reputation: 65

Sampling Bernoulli random variables in 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.

Upvotes: 3

Views: 5264

Answers (3)

Tim
Tim

Reputation: 7474

I've seen also the following trick as a way of sampling from the Bernoulli distribution:

tf.nn.relu(tf.sign(means - tf.random_uniform(tf.shape(means))))

Upvotes: 1

Dan Salo
Dan Salo

Reputation: 163

Since TFr1.0, tf.select is deprecated in favor of tf.where. Furthermore, the answer given by @keveman should compare the uniform random sampling with < 0, neither with > 0.5 nor with > 0:

    means = tf.constant([.3,.8])
    sample = tf.where(tf.random_uniform([1, 2]) - means < 0, 
      tf.ones([1,2]), tf.zeros([1,2]))
    with tf.Session(''): sample.eval()

Upvotes: 3

keveman
keveman

Reputation: 8487

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()

Upvotes: 6

Related Questions