njk
njk

Reputation: 655

Gradients of Bernoulli Samples

I'm trying to calculate the gradients of the samples from a Bernoulli distribution w.r.t. the probabilities p (of a sample being 1).

I tried using both the implementation of the Bernoulli distribution provided in tensorflow.contrib.distributions and my own simple implementation based on this discussion. However both methods fail when I try to calculate the gradients.

Using the Bernoulli implementation:

import tensorflow as tf
from tensorflow.contrib.distributions import Bernoulli

p = tf.constant([0.2, 0.6])
b = Bernoulli(p=p)
s = b.sample()
g = tf.gradients(s, p)

with tf.Session() as session:
    print(session.run(g))

The above code gives me the following error:

TypeError: Fetch argument None has invalid type <class 'NoneType'>

Using my implementation:

import tensorflow as tf

p = tf.constant([0.2, 0.6])
shape = [1, 2]
s = tf.select(tf.random_uniform(shape) - p > 0.0, tf.ones(shape), tf.zeros(shape))
g = tf.gradients(s, p)

with tf.Session() as session:
    print(session.run(g))

Same error:

TypeError: Fetch argument None has invalid type <class 'NoneType'>

Is there a way to calculate the gradients of Bernoulli samples?

(My TensorFlow version is 0.12).

Upvotes: 4

Views: 1158

Answers (1)

linello
linello

Reputation: 8704

You cannot backprop through a discrete stochastic node for obvious reasons. As gradients are not defined. However if you approximate the Bernoulli with a continuos distribution controlled by a temperature parameter, yes you can.

This idea is called reparametrization trick and is implemented in the RelaxedBernoulli in Tensorflow Probability (or also in TF.contrib library)

Relaxed bernoulli

You can specify the probability p of your Bernoulli, which is your random variable, et voilà.

Upvotes: 1

Related Questions