Issue
I am trying to batch predict a number of inputs to my model which expects an input containing 2 1D lists of a fixed size like this:
[<tf.Tensor: shape=(1, 150), dtype=int64, numpy=array([[2, 924, ...]])>, <tf.Tensor: shape=(1, 150), dtype=int64, numpy=array([[2, 130, ...]])>]
Assuming this input is called input_in, I can predict a sample by writing:
transformer.predict(input_in)
Where input_in thus has the following shape:
(2, 1, 150)
And this works. However, I can't understand the input necessary to pass to predict() to predict multiple predictions at once. Intuitively, the shape has to be something like this:
(X, 2, 1, 150)
Where X is the number of predictions to pass.
I tried this, but I receive an error:
transformer.predict([input_in, input_in, input_in])
ValueError: in user code:
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1801, in predict_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1790, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1783, in run_step **
outputs = model.predict_step(data)
File "/usr/local/lib/python3.8/dist-packages/keras/engine/training.py", line 1751, in predict_step
return self(x, training=False)
File "/usr/local/lib/python3.8/dist-packages/keras/utils/traceback_utils.py", line 67, in error_handler
raise e.with_traceback(filtered_tb) from None
File "/usr/local/lib/python3.8/dist-packages/keras/engine/input_spec.py", line 200, in assert_input_compatibility
raise ValueError(f'Layer "{layer_name}" expects {len(input_spec)} input(s),'
ValueError: Layer "transformer" expects 2 input(s), but it received 6 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 12) dtype=int64>, <tf.Tensor 'IteratorGetNext:1' shape=(None, 1) dtype=int64>, <tf.Tensor 'IteratorGetNext:2' shape=(None, 12) dtype=int64>, <tf.Tensor 'IteratorGetNext:3' shape=(None, 1) dtype=int64>, <tf.Tensor 'IteratorGetNext:4' shape=(None, 12) dtype=int64>, <tf.Tensor 'IteratorGetNext:5' shape=(None, 1) dtype=int64>]
Interestingly, if I try to predict one input but simply change the input to a tensor, I also get a similar error:
transformer_test.predict(tf.convert_to_tensor([input_tensor,output_tensor]))
-> ValueError: Layer "transformer" expects 2 input(s), but it received 1 input tensors. Inputs received: [<tf.Tensor 'IteratorGetNext:0' shape=(None, 1, 150) dtype=int64>]
Any suggestions?
Edit: My Transformer model looks like this:
I also reproduced a minimal version of the issue: https://colab.research.google.com/drive/1xYNjLURQcGPIXeWV-xo4_iA09KDJvNfi?usp=sharing
If you run all, the last 2 cells will give errors. I have been trying to play around with the input to batch predict but nothing seems to have worked...
Solution
Or try using the names of your input layers as references:
ds = tf.data.Dataset.from_tensor_slices((tf.stack([batch_inputs[0], batch_inputs[0], batch_inputs[0]]))).map(lambda x: {'inputs': x[0], "dec_inputs": x[1]})
predictions = transformer_test.predict(ds)
And without tf.stack:
ds = tf.data.Dataset.from_tensor_slices((batch_inputs)).map(lambda x: {'inputs': x[0], "dec_inputs": x[1]})
Answered By - AloneTogether Answer Checked By - Mildred Charles (Python.Easybugfix Admin)

Post a Comment
Post a Comment