2

I am training a Keras Tensorflow model with three inputs and two outputs:

mymodel = tf.keras.Model([X1, X2, X3], [y1, y2])

When I fit this model by separately specifying x and y data, it works fine without any hitches:

history = mymodel.fit([X1, X2, X3], [y1, y2], batch_size=128, epochs=5)

However, I would like to provide the training data as a single tuple (x, y) in order to maintain compatibility with a custom data generator. When I do this, it throws an error:

data = ([X1, X2, X3], [y1, y2])
history = mymodel.fit(data, batch_size=128, epochs=5)
No gradients provided for any variable: ['dense/kernel:0', 'dense/bias:0',...

I guess my format for the data tuple is wrong.

How can I correctly specify my training data?

ProteinGuy
  • 1,754
  • 2
  • 17
  • 33

1 Answers1

4

What you need is to build your data pipeline with a generator or tf.data API. According to the documentation of the training API, source:

Model.fit(
    x=None,
    y=None,
    batch_size=None,
    epochs=1,
    ...

Arguments

- x: Input data. It could be:
     A tf.data dataset. Should return a tuple of either (inputs, targets) 
     or (inputs, targets, sample_weights).

     A generator or keras.utils.Sequence returning (inputs, targets) or 
    (inputs, targets, sample_weights).

- y: If x is a dataset, generator, or keras.utils.Sequence instance, 
     y should not be specified (since targets will be obtained from x).

FYI, but if your data is array or tensor (x), then you need to provide the corresponding y. According to the doc

- x: 
    A Numpy array (or array-like), or a list of arrays (in case the model has 
    multiple inputs).
    A TensorFlow tensor, or a list of tensors (in case the model has multiple inputs).

- y:
   Like the input data x, it could be either Numpy array(s) or TensorFlow tensor(s). 
   It should be consistent with x (you cannot have Numpy inputs 
   and tensor targets, or inversely).
Innat
  • 16,113
  • 6
  • 53
  • 101
  • Thank you! In that case, how can I use a dataset generator with numpy arrays (and multiple inputs/outputs). – ProteinGuy May 26 '21 at 21:45
  • 1
    [Here](https://stanford.edu/~shervine/blog/keras-how-to-generate-data-on-the-fly) is one good article regarding the keras generator. And [here](https://www.tensorflow.org/api_docs/python/tf/data/Dataset) is the tutorial of tf.dataset API. – Innat May 26 '21 at 21:49
  • 1
    Those are for dataloader, in case of model definition, refer to this discussion, [one](https://stackoverflow.com/questions/66845924/multi-input-multi-output-model-with-keras-functional-api/66849164#66849164), [two](https://stackoverflow.com/questions/67114215/training-multiple-input-and-output-keras-model-valueerror-failed-to-convert-a/67117135#67117135). – Innat May 26 '21 at 21:51
  • 1
    Some more resources for reference. https://blog.keras.io/building-powerful-image-classification-models-using-very-little-data.html – Innat May 26 '21 at 21:54