简体   繁体   中英

Tensorflow cannot compute Addv2 as input #1(zero-based) was expected to be a double tensor but it is a float tensor [Op:Addv]

Error message:

tensorflow.python.framework.errors_impl.InvalidArgumentError: cannot compute AddV2 as input #1(zero-based) was expected to be a double tensor but is a float tensor [Op:AddV2]

In my code I create a tensorflow distribution MixtureSameFamily object and use the output of my network as parameters. However when I try to calculate the probability across a range of values in order to generate the probability density function, I receive this error.

My code:

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)

The result of print(x.dtype) is "dtype: 'float'"

As far as I know tensorflow does not have support for float datatypes as per the documentation.

For this reason I am especially confused. Any help would be greatly appreciated.

Seems to be a bug in the latest tensorflow-probability module. It only properly works with float32 .

WORKAROUND

explicitly cast your parameters into 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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM