我正在尝试将Beta分布适合具有离散分数(1, 2, 3, 4, 5)
的调查结果。为此,我需要TensorFlow概率中Beta的有效log_prob。但是,在Beta中如何处理批处理存在问题。这是给我一个错误的最小示例:
InvalidArgumentError:a和x的形状不一致:[3]与[1000,1] [Op:Betainc]
相同的代码似乎可以在正态分布下正常工作...
我在这里做错了什么?
import numpy as np
import tensorflow_probability as tfp
tfd = tfp.distributions
#Generate fake data
np.random.seed(2)
data = np.random.beta(2.,2.,1000)*5.0
data = np.ceil(data)
data = data[:,None]
# Create a batch of three Beta distributions.
alpha = np.array([1., 2., 3.]).astype(np.float32)
beta = np.array([1., 2., 3.]).astype(np.float32)
bt = tfd.Beta(alpha, beta)
#bt = tfd.Normal(loc=alpha, scale=beta)
#Scale beta to 0-5
scbt = tfd.TransformedDistribution(
distribution=bt,
bijector=tfp.bijectors.AffineScalar(
shift=0.,
scale=5.))
# quantize beta to (1,2,3,4,5)
qdist = tfd.QuantizedDistribution(distribution=scbt,low=1,high=5)
#calc log_prob for 3 distributions
print(np.sum(qdist.log_prob(data),axis=0))
print(qdist.log_prob(data).shape)
TensorFlow 2.0.0tensorflow_probability 0.8.0
可悲的是,tf.math.betainc目前不支持广播,这导致QuantizedDistribution称为cdf计算失败。如果必须使用Beta,我能想到的唯一解决方法是通过汇总数据和Beta参数来“手动”广播。
或者,您也许可以摆脱Kumaraswamy分布,它与Beta类似,但具有更好的分析特性。