我试图从Bernoulli分布w.r.t计算样本的梯度。(样本为1
的)概率p
。
我尝试使用tensorflow.contrib.distributions
中提供的伯努利分布的实现和基于这个discussion的我自己的简单实现。然而,当我尝试计算梯度时,这两种方法都失败了。
使用Bernoulli
实现:
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))
上面的代码给出了以下错误:
TypeError: Fetch argument None has invalid type <class 'NoneType'>
使用我的实现:
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))
同样的错误:
TypeError: Fetch argument None has invalid type <class 'NoneType'>
有没有办法计算伯努利样本的梯度?
(我的TensorFlow版本是0.12)。
发布于 2019-01-07 22:26:36
由于显而易见的原因,您不能通过离散的随机节点进行支持。因为没有定义渐变。然而,如果你用一个由温度参数控制的连续分布来近似伯努利分布,是的,你可以。
这个想法被称为重新参数化技巧,并在Tensorflow Probability的RelaxedBernoulli中实现(或者也在TF.contrib库中实现)。
你可以指定你的伯努利的概率p
,这是你的随机变量,等等。
https://stackoverflow.com/questions/41961649
复制相似问题