Tensorflow 无法计算 Addv2,因为输入的第1个张量(从零开始)应该是双精度张量,但它是一个浮点数张量 [Op: Addv]。

7
错误信息:

tensorflow.python.framework.errors_impl.InvalidArgumentError: 无法计算AddV2,因为输入#1(从零开始)应为双精度张量,但是为浮点数张量[Op:AddV2]

我的代码中我创建了一个tensorflow分布MixtureSameFamily对象,并使用网络输出作为参数。然而当我尝试在一系列值上计算概率以生成概率密度函数时,会收到此错误。

我的代码:

gm = tfd.MixtureSameFamily(
    mixture_distribution=tfd.Categorical(probs=alphas),
    components_distribution=tfd.Normal(
        loc=mus,
        scale=sigmas
    )
)

x = np.linspace(-2,2,int(1000), dtype=np.double)
print(x.dtype)
pyx = gm.prob(x)

print(x.dtype)的结果是"dtype: 'float'"

据我所知,根据文档,tensorflow不支持浮点数数据类型。

因此我非常困惑。 如有帮助,将不胜感激。

1个回答

6
似乎最新的tensorflow-probability模块存在一个bug。它只能与 float32 正常工作。
解决方法:
将参数显式地转换为float32
gm = tfd.MixtureSameFamily(
    mixture_distribution=tfd.Categorical(probs=alphas.astype('float32')),
    components_distribution=tfd.Normal(
        loc=mus.astype('float32'),
        scale=sigmas.astype('float32')
    )
)

x = np.linspace(-2,2,int(1000), dtype='float32')
pyx = gm.prob(x)

可重现的Colab:https://colab.research.google.com/drive/1g37RztAguZHIPQqWOqvhe16XJDH0mPvz?usp=sharing - Ufos
TensorFlow Probability 的问题跟踪链接:https://github.com/tensorflow/probability/issues/1010 - Ufos

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接