pythontensorflowmachine-learningtensorflow-datasetstensorflow-probability

TensorFlow Probability inference error input type


I train a model using tensorflow_probability and distributions.

To train it I format my data like this (2 heads model so 2 inputs set):

input_1 = tf.data.Dataset.from_tensor_slices(Xdata)
input_2 = tf.data.Dataset.from_tensor_slices(Xphysio)
output = tf.data.Dataset.from_tensor_slices(yobs)
combined_dataset = tf.data.Dataset.zip(((input_1, input_2), output))
input_dataset = combined_dataset.batch(30)

Trainning work well... but when I try to do inference like in this exemple at cell #15 they call the model like this:

yhats = model(combined_dataset)

I got the error

TypeError: Inputs to a layer should be tensors. Got: <ZipDataset element_spec=((TensorSpec(shape=(120, 9), dtype=tf.float32, name=None), TensorSpec(shape=(24,), dtype=tf.float32, name=None)), TensorSpec(shape=(), dtype=tf.float32, name=None))>

I try:

yhats = model([input_1, input_2])

and got same error:

TypeError: Inputs to a layer should be tensors. Got: <TensorSliceDataset element_spec=TensorSpec(shape=(120, 9), dtype=tf.float32, name=None)>

using yhats = model.predict([Xdata, Xphysio]) run well but seem to not return a valid format for tfd.Distribution:

assert isinstance(yhat, tfd.Distribution):

Traceback (most recent call last):
  File "E:\Windows programs\PyCharm Community Edition 2021.3.1\plugins\python-ce\helpers\pydev\_pydevd_bundle\pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<input>", line 1, in <module>
AssertionError

Solution

  • It is finaly a requirement to use tensorflow 2.13, I was 2.10. I do the upgrade using pip in lieu of conda as usual and all work fine.