keras
keras copied to clipboard
model.fit fails with error "Target data is missing. Your model was compiled with loss..."
System information.
- Have I written custom code (as opposed to using a stock example script provided in Keras): Yes
- OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Mac, Google colab
- TensorFlow installed from (source or binary): binary
- TensorFlow version (use command below): 2.8.2 and 2.9.1
- Python version: 3.9 and 3.7
- Bazel version (if compiling from source):
- GPU model and memory: CPU or GPU
- Exact command to reproduce:
import tensorflow as tf
import numpy as np
a = tf.keras.Input(name='a', shape=[None])
b = tf.keras.Input(name='b', shape=[None])
tf_model = tf.keras.Model(inputs=[a, b], outputs={'a': a, 'b': b})
tf_model.compile(loss={'a': tf.keras.losses.MeanSquaredError(),
'b': tf.keras.losses.MeanSquaredError()},
optimizer=tf.keras.optimizers.Adam())
tf_model.fit({'a':np.array([[1,2]]),'b': np.array([[3,4]])})
# Error occurs
You can collect some of this information using our environment capture script:
https://github.com/tensorflow/tensorflow/tree/master/tools/tf_env_collect.sh
You can obtain the TensorFlow version with: python -c "import tensorflow as tf; print(tf.version.GIT_VERSION, tf.version.VERSION)"
Describe the problem.
The above coding works fine on older tensorflow versions (e.g. I tested it on 2.4.1 where it ran w/o error)
Describe the current behavior.
On newer tensorflow version an exception is thrown:
ValueError: in user code:
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1021, in train_function *
return step_function(self, iterator)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1010, in step_function **
outputs = model.distribute_strategy.run(run_step, args=(data,))
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 1000, in run_step **
outputs = model.train_step(data)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 861, in train_step
self._validate_target_and_loss(y, loss)
File "/usr/local/lib/python3.7/dist-packages/keras/engine/training.py", line 819, in _validate_target_and_loss
'Target data is missing. Your model was compiled with '
ValueError: Target data is missing. Your model was compiled with loss={'a': <keras.losses.MeanSquaredError object at 0x7fc15fd785d0>, 'b': <keras.losses.MeanSquaredError object at 0x7fc15fd78e90>}, and therefore expects target data to be provided in `fit()`.
Describe the expected behavior.
According to the documentation for https://www.tensorflow.org/api_docs/python/tf/keras/Model#compile , if the model has multiple outputs
If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses.
therefore, we should be able to pass a dictionary mapping outputs to losses.
- Do you want to contribute a PR? (yes/no): no
- If yes, please read this page for instructions
- Briefly describe your candidate solution(if contributing):
Standalone code to reproduce the issue.
Provide a reproducible test case that is the bare minimum necessary to generate the problem. If possible, please share a link to Colab/Jupyter/any notebook.
See above coding for minimal example, or see here:
https://colab.research.google.com/drive/1shEpzITgBYLfya7FRP6DGM458vEOfHvi?usp=sharing
Source code / logs.
Include any logs or source code that would be helpful to diagnose the problem. If including tracebacks, please include the full traceback. Large logs and files should be attached. Try to provide a reproducible test case that is the bare minimum necessary to generate the problem.
Hey, can i work on this issue?
@gadagashwini, I was able to reproduce the issue on tensorflow v2.9 and nightly. As mentioned, on tf v2.4 the code was executing with any error. Kindly find the gist of it here.
Hi @JuanIMartinezB, You are welcome to raise PR to fix this issue. Thank you!
Hello @skbaur, in the code example, Model.fit() isn't provided with a label (y argument), and thus the library isn't able to compute the loss for the training. The dict is used as the x. As far as I see it this is an expected error message.
Hello @skbaur, in the code example,
Model.fit()isn't provided with a label (yargument), and thus the library isn't able to compute the loss for the training. The dict is used as thex. As far as I see it this is an expected error message.
But for e.g. dataset input, there would never be a 'y' parameter, right? (see e.g. https://www.tensorflow.org/api_docs/python/tf/keras/Model#fit "Target data. 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). If x is a dataset, generator, or keras.utils.Sequence instance, y should not be specified (since targets will be obtained from x).")
The input in the example is a dict while fit expects a tuple of (x, y) or (x, y, sample_weight) from the dataset, and this is the reason why it can't find the label. Can you try using an explicit y argument for the label and see if it helps?
Also this may be helpful: https://keras.io/examples/keras_recipes/endpoint_layer_pattern/
@skbaur As mentioned above, explicitly y argument solves this issue. I am going ahead and closing this issue as it has been inactive for a while. Please add additional comments to open this issue again. Thanks!!