TensorFlow "TypeError: Target data is missing" though dataset with 2 dimension tuple was supplied

I’m trying to use a generator- based data set:

def gen():
    return zip(samples,feature)
ds = tf.data.Dataset.from_generator(gen,output_types=tf.dtypes.float32)


model.fit(ds,
          epochs=150,
          #callbacks=[tensorboard_callback]
          )
model.save("/sda/anyone/imagenet-in-np/transformer")

whereas feature is numpy.ndarray (2D array)
whereas feature is numpy.ndarray (4D array)
And I get the following error:

TypeError: Target data is missing. Your model has `loss`: BinaryCrossentropy, and therefore expects target data to be passed in `fit()`.

which is strange, as the target data is actually present.
Whenever I separate the dataset to two

def gen():
    return samples
ds = tf.data.Dataset.from_generator(gen,output_types=tf.dtypes.float32)

def gen2():
    return feature
ds2= tf.data.Dataset.from_generator(gen2,output_types=tf.dtypes.float32)

model.fit(ds,ds2,
          epochs=150,
          #callbacks=[tensorboard_callback]
          )
model.save("/sda/anyone/imagenet-in-np/transformer")

I get:

    raise ValueError("`y` argument is not supported when using "
ValueError: `y` argument is not supported when using dataset as input.

Which means that TF doesn’t accept this split.

I tried

def gen():
    for element in zip(samples,feature):
        yield element
ds = tf.data.Dataset.from_generator(gen(),output_types=tf.dtypes.float32)

I get
TypeError: generator must be a Python callable.

So I tried to swap it to :

def gen():
    for element in zip(samples,feature):
        yield element
ds = tf.data.Dataset.from_generator(gen,output_types=tf.dtypes.float32)

I get again:

    TypeError: Target data is missing. Your model has `loss`: BinaryCrossentropy, and therefore expects target data to be passed in `fit()`.
python-BaseException

So how should I use the generator API?

1 Like

I actually got the same error and my mistake was that the model was expecting the input and labels in a different format while I was passing them in an incorrect format.

Additionally can you provide a Minimum working example (MWE)? It would be easier to find out the problem that way.