keras-team / tf-keras

The TensorFlow-specific implementation of the Keras API, which was the default Keras from 2019 to 2023.
Apache License 2.0
60 stars 28 forks source link

tf.keras.metrics.mean_absolute_error exception information is misleading. #343

Open triumph-wangyuyang opened 1 year ago

triumph-wangyuyang commented 1 year ago

System information.

Describe the problem.

The exceptions thrown by tf.keras.metrics.mean_absolute_error(y_true, y_pred) are different when running the following two test codes. In code1, I set y_pred to bool, and the exception message prompts me that bool is not in the legal type (bfloat16, half, float, double, uint8, int8, uint16, int16, int32, int64, complex64, complex128, uint32, uint64) (Remember that it includes uint16, which will be useful later). In code2, I set y_pred to uint16, and the exception information reminded me that uint16 is not in the legal type (bfloat16, half, float, double, int8, int16, int32, int64). It is obvious that the exception information before and after is contradictory. In addition, tf.keras.metrics.mean_absolute_percentage_error, tf.keras.metrics.mape, tf.keras.metrics.MAPE, tf.keras.metrics.mae, tf.keras.metrics.MAE, tf.keras.losses.mean_absolute_error, tf.keras.losses.mean_absolute_percentage_error, tf.keras.losses.mape, tf.keras.losses.MAPE, tf.keras.losses.mae, tf.keras.losses.MAE all have the same doubt.

Standalone code to reproduce the issue.

code 1:

import tensorflow as tf
y_true = tf.random.uniform([2, 3], minval=-5, maxval=5, dtype=tf.int32)
y_pred = tf.cast(tf.random.uniform([2, 3], minval=-5, maxval=5, dtype=tf.int32), dtype=tf.bool)
out = tf.keras.metrics.mean_absolute_error(y_true, y_pred)

code1 result : tensorflow.python.framework.errors_impl.InvalidArgumentError: Value for attr 'T' of bool is not in the list of allowed values: bfloat16, half, float, double, uint8, int8, uint16, int16, int32, int64, complex64, complex128, uint32, uint64; NodeDef: {{node Sub}}; Op<name=Sub; signature=x:T, y:T -> z:T; attr=T:type,allowed=[DT_BFLOAT16, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_UINT8, DT_INT8, DT_UINT16, DT_INT16, DT_INT32, DT_INT64, DT_COMPLEX64, DT_COMPLEX128, DT_UINT32, DT_UINT64]> [Op:Sub]

code 2:

import tensorflow as tf
y_true = tf.random.uniform([2, 3], minval=-5, maxval=5, dtype=tf.int32)
y_pred = tf.cast(tf.random.uniform([2, 3], minval=-5, maxval=5, dtype=tf.int32), dtype=tf.uint16)
out = tf.keras.metrics.mean_absolute_error(y_true, y_pred)

code2 result : tensorflow.python.framework.errors_impl.InvalidArgumentError: Value for attr 'T' of uint16 is not in the list of allowed values: bfloat16, half, float, double, int8, int16, int32, int64; NodeDef: {{node Abs}}; Op<name=Abs; signature=x:T -> y:T; attr=T:type,allowed=[DT_BFLOAT16, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_INT8, DT_INT16, DT_INT32, DT_INT64]> [Op:Abs]

tilakrayal commented 1 year ago

@triumph-wangyuyang, The exception information is auto-generated based on kernel registrations. You will need to update the python docstrings. Thank you!

triumph-wangyuyang commented 1 year ago

update the python docstrings

Thank you for your reply, what is the specific operation, I did not understand the meaning of "update the python docstrings".

google-ml-butler[bot] commented 1 year ago

Are you satisfied with the resolution of your issue? Yes No

triumph-wangyuyang commented 1 year ago

The exception information is auto-generated based on kernel registrations. You will need to update the python docstrings. Thank you!

Hello, I have changed here, but the problem still exists, can you answer it?

triumph-wangyuyang commented 1 year ago

@triumph-wangyuyang, The exception information is auto-generated based on kernel registrations. You will need to update the python docstrings. Thank you!

Hi, can you explain to me why these problems occur?

tilakrayal commented 1 year ago

@gowthamkpr, I was able to reproduce the issue on tensorflow v2.9, v2.11 and nightly. Kindly find the gist of it here.