如果给定一个包含伯努利分布均值的 1D 张量,如何生成相应的 1D 张量样本?
TensorFlow 只实现了 random_normal
和 random_uniform
函数。您可以使用像下面这样复杂的方法:
tf.ceil(tf.sub(tf.random_uniform((1, means.get_shape()[0])),means))
但是在 TensorFlow 中,ceil
函数没有定义梯度。
如果给定一个包含伯努利分布均值的 1D 张量,如何生成相应的 1D 张量样本?
TensorFlow 只实现了 random_normal
和 random_uniform
函数。您可以使用像下面这样复杂的方法:
tf.ceil(tf.sub(tf.random_uniform((1, means.get_shape()[0])),means))
但是在 TensorFlow 中,ceil
函数没有定义梯度。
tf.select
。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()
tf.select
已被弃用,改用tf.where
。此外,@keveman提供的答案应该将均匀随机抽样与<0进行比较,而不是与>0.5或>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()
我曾经看到以下技巧作为从伯努利分布中抽样的一种方法:
tf.nn.relu(tf.sign(means - tf.random_uniform(tf.shape(means))))
tf.select
和tf.cond
可能是可微分的,但tf.random_uniform
操作表示它不可微分。这是否使得控制操作中的选择或分支不可微分? - muneeb