Reputation: 83
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.
Upvotes: 8
Views: 13562
Reputation: 3305
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)
Upvotes: 6