'How to feed multiple inputs to tf.keras.Model.predict?
Given this simple tensorflow toy model
import tensorflow as tf
inputs = {
"a":tf.keras.Input(shape=(), name="input_a"),
"b":tf.keras.Input(shape=(), name="input_b")
}
outputs = tf.keras.layers.Add()([inputs["a"], inputs["b"]])
model = tf.keras.Model(inputs=inputs, outputs=outputs)
I can get its output by invoking it with inputs as defined, so the following:
model({"a":2,"b":3})
Gives the output:
<tf.Tensor: shape=(), dtype=float32, numpy=5.0>
But invoking the predict function:
model.predict({"a":2,"b":3})
Gives the following error:
ValueError: Failed to find data adapter that can handle input: (<class 'dict'> containing {"<class 'str'>"} keys and {"<class 'int'>"} values), <class 'NoneType'>
So how do I correctly invoke the predict function when my model has more than a single input as in this case?
Solution 1:[1]
It seems that the predict method can handle a dictionary of BATCHES of numpy arrays, this works:
model.predict({'a': np.full((1,), 3), 'b': np.full((1,), 2)})
and outputs <tf.Tensor: shape=(1,), dtype=float32, numpy=array([5.], dtype=float32)>
In this case, the batch size is equal to 1.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | elbe |
