Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 450
traderbot Avatar asked Jun 15 '20 14:06

traderbot


Video Answer


1 Answers

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)
like image 174
Ufos Avatar answered Sep 17 '22 22:09

Ufos