TensorFlow Probability中批量混合分布的概率
Probability of batched mixture distribution in TensorFlow Probability
TFP 发行版应具备开箱即用的批处理能力。但是,我面临着批量混合分配的问题。
特此是一个玩具示例(使用急切执行):
tfd = tfp.distributions
mix = np.array([[0.6, 0.4],[0.3, 0.7]] )
bimix_gauss = tfd.Mixture(
cat=tfd.Categorical(probs=mix),
components=[
tfd.Normal(loc=[-1.0, -2.0], scale=[0.1, 0.1]),
tfd.Normal(loc=[+1.0, +2.0], scale=[0.5, 0.5]),
])
print(bimix_gauss.sample())
print(bimix_gauss.prob(0.0))
基本上,它只是默认示例的 baching:https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/Mixture
采样工作正常,但此分布的概率 returns 出错:
InvalidArgumentError: cannot compute Add as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:Add] name: Mixture/prob/add/
任何猜测,我做错了什么?
PS。使用批量高斯分布的相同示例工作正常。
问题是 numpy 默认为 float64,但 TFP 遵循默认为 float32 的 TF 约定。因此,您的正态分布,其参数是裸 python 列表,在 Normal 的构造函数中被重铸为 tf.Tensors 作为 float32 张量,最终导致类型错误。您可以通过强制 np 数组为 float 32 来修复,或者更简单地通过将混合值作为列表而不是 ndarrays 传递。
TFP 发行版应具备开箱即用的批处理能力。但是,我面临着批量混合分配的问题。 特此是一个玩具示例(使用急切执行):
tfd = tfp.distributions
mix = np.array([[0.6, 0.4],[0.3, 0.7]] )
bimix_gauss = tfd.Mixture(
cat=tfd.Categorical(probs=mix),
components=[
tfd.Normal(loc=[-1.0, -2.0], scale=[0.1, 0.1]),
tfd.Normal(loc=[+1.0, +2.0], scale=[0.5, 0.5]),
])
print(bimix_gauss.sample())
print(bimix_gauss.prob(0.0))
基本上,它只是默认示例的 baching:https://www.tensorflow.org/probability/api_docs/python/tfp/distributions/Mixture
采样工作正常,但此分布的概率 returns 出错:
InvalidArgumentError: cannot compute Add as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:Add] name: Mixture/prob/add/
任何猜测,我做错了什么?
PS。使用批量高斯分布的相同示例工作正常。
问题是 numpy 默认为 float64,但 TFP 遵循默认为 float32 的 TF 约定。因此,您的正态分布,其参数是裸 python 列表,在 Normal 的构造函数中被重铸为 tf.Tensors 作为 float32 张量,最终导致类型错误。您可以通过强制 np 数组为 float 32 来修复,或者更简单地通过将混合值作为列表而不是 ndarrays 传递。