pytorch概率函数torch.bernoulli的张量流等效量是多少?

时间:2018-10-31 19:36:36

标签: python tensorflow pytorch

在Pytorch中,您可以执行以下操作:

x = torch.bernoulli(my_data)

张量流中是否有任何类似的功能?输入可以是二维张量,例如(batch,len)吗?

我尝试了tensorflow.contrib.distributions.Bernoulli:

import numpy as np
tmp_x1 = np.random.rand(20,5) 
new_data_2 = tf.convert_to_tensor(tmp_x1)
from tensorflow.contrib.distributions import  Bernoulli
tmp2_x1 = Bernoulli(probs=new_data_2)

得到错误:

return math_ops.log(probs) - math_ops.log1p(-1. * probs), probs
TypeError: unsupported operand type(s) for *: 'float' and 'Tensor'

1 个答案:

答案 0 :(得分:0)

tf.distributions.Bernoulli似乎可以满足您的需求。输入可以是N-D张量,其中包括2D张量。

编辑:示例用法

在您发表评论后,我尝试了以下对我有用的方法(使用tensorflow 1.11):

import numpy as np
import tensorflow
import tensorflow as tf
from tensorflow.distributions import  Bernoulli

tmp_x1 = np.random.rand(20,5)
new_data_2 = tf.convert_to_tensor(tmp_x1)
tmp2_x1 = Bernoulli(probs=new_data_2)
sess = tf.Session()
print sess.run(tmp2_x1.sample())
相关问题