I am currently trying to implement a multi-input model in Keras. The input consists of multiple batches, and each includes different samples, but I get a 'different samples'-error. My implementation looks like this:
The model site looks as follows:
for s in range(NUM_STREAMS):
inp.append(Input(shape=(16,8)))
...
The site where the error occurs:
history = model.train_on_batch(
x=[x for x in X_batch],
y=[y for y in y_batch]
)
The error I get is:
ValueError: All input arrays (x) should have the same number of
samples. Got array shapes: [(6, 16, 8), (7, 16, 8), (6, 16, 8), (6, 16, 8)]
The abstract model architecture looks as follow:
If you ever need to specify a fixed batch size for your inputs (this is useful for stateful recurrent networks), you can pass a batch_size argument to a layer. If you pass both batch_size=32 and input_shape=(6, 8) to a layer, it will then expect every batch of inputs to have the batch shape (32, 6, 8) .
Keras is able to handle multiple inputs (and even multiple outputs) via its functional API. Learn more about 3 ways to create a Keras model with TensorFlow 2.0 (Sequential, Functional, and Model Subclassing).
For models like LSTM and CNN, the batch size is critical to learn the common patterns as important features. For a model to be able to figure out what are the common patterns and features across the input train samples we need to provide the samples as batches.
FYI, when faced with a similar problem, I rewrote my model in tensorflow, as their computational graphs are not constrained to keeping a batch size dimension constant.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With