Pythorch概率函数的张量流当量是多少:火炬伯努利?

2024-06-15 23:41:06 发布

您现在位置:Python中文网/ 问答频道 /正文

在Pythorch中,可以执行以下操作:

x = torch.bernoulli(my_data)

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

我试过了tensorflow.contrib.分布伯努利:

^{pr2}$

收到错误:

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

Tags: 功能datalenmytensorflow错误batchmath
1条回答
网友
1楼 · 发布于 2024-06-15 23:41:06

似乎^{}能满足您的需要。输入可以是包括2D张量的nd张量。在

编辑:示例使用

在你的评论之后,我尝试了以下方法,这对我很有效(使用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())

相关问题 更多 >