什么是pytorch概率函数的张量流等价:torch.bernoulli?

问题描述 投票:0回答:1

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

x = torch.bernoulli(my_data)

张量流中的任何类似功能?输入可以是2-D张量,例如(batch,len)?

I tried 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)

got error:

return math_ops.log(probs) - math_ops.log1p(-1. * probs), probs
TypeError: unsupported operand type(s) for *: 'float' and 'Tensor'
python tensorflow pytorch
1个回答
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())
© www.soinside.com 2019 - 2024. All rights reserved.