Keras: Keras Model.predict for multiple inputs with different numbers of first dimension

Created on 22 Dec 2019  路  4Comments  路  Source: keras-team/keras

We are able to use Model.predict(x=[input1, input2],...) to have multiple inputs for the model by putting them into a list; however, by entering input1 and input2 with different number of rows, I encountered the following error:

ValueError: Data cardinality is ambiguous:
  x sizes: 10, 200
Please provide data which shares the same first dimension.

My understanding is that the model treats each row as a individual datapoint, and the same row for input1 and input2 belongs to the same datapoint, therefore the first dimension must match; however, here I plan to use aggregate input1 altogether to form one representation, and concatenate this one output with each individual row of input2 as the input the the next network.

In other words, my goal is to make a prediction for each datapoint from input2, and I want to use input1 as a whole.

I'm able to build the computational graph and train the model; as long as the first dimension of input1 and input2 matches there's no issue. But here I plan to have input2 with arbitrary number of datapoints. I'm thus wondering if in Keras we can relax such requirements for multiple inputs?

feature

Most helpful comment

I've found that Model.predict_on_batch() method works for this scenario.

All 4 comments

I've found that Model.predict_on_batch() method works for this scenario.

@XzwHan thank you, it works for me as well,

@XzwHan THANK YOU

https://github.com/tensorflow/tensorflow/commit/56a0ce87911236765633d2a873e706ebc6401ef9
seems to have changed here even with predict_on_batch you can no longer use different inputs with different number of rows...

Was this page helpful?
0 / 5 - 0 ratings