Batched `keras.utils.data_utils.Sequence` with dictionary inputs

Given a Sequence with a __getitem__ that returns x, y where x, y are of type list containing dictionaries that specify the input, output. For example:

x = [
 { 'input_1': 4123, .... },
 { 'input_1': 1123, .... }
]
y = [
 { 'output_1': 4123, .... },
 { 'output_1': 4123, .... }
]

model.fit cannot infer the shape of the generator. The error that I am getting:

  File "..../site-packages/keras/utils/traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File ".../site-packages/keras/engine/data_adapter.py", line 890, in __init__
    self._first_batch_size = int(tf.nest.flatten(peek)[0].shape[0])
AttributeError: 'float' object has no attribute 'shape'

So I investigated the GeneratorDataAdapter class that the error was raised from.
It’s peeking at the first batch and doing the following on it:

self._first_batch_size = int(tf.nest.flatten(peek)[0].shape[0])

It’s making the assumption that the batch you get is of type numpy.array since tf.nest.flatten(peek)[0] is looking at x and x is of type list which doesn’t change to numpy.array when tensorflow.nest.flatten is called on it afaik (?)

I can’t manually convert x, y to a numpy.array either within the generator because they contain named inputs, outputs in the form of dictionaries as shown in the example in the beginning.

Is there a workaround for this?

Hi @darrenrahnemoon

Welcome to the TensorFlow Forum!

Please let us know if this issue still persists. If so, Could you please provide the minimal reproducible code to replicate the error and to understand the issue? Thank you.