`MeanAbsoluteError` returns "nan" when given empty tensors
Hi @FGRCL ,

I have gone through the issue and replicated the reported behaviour and attached [gist](https://colab.research.google.com/gist/SuryanarayanaY/fff1cc34b602cdc91dcafb5f3a0051ba/61281_nightly.ipynb) for reference. What I observed is even with Numpy for empty arrays Numpy also generating `nan` only which is same as TF behaviour with empty Tensors of shape=(0,) .

This is a different case which needs to be discussed. Tensorflow implemented numpy like behaviour for empty tensors.

Can you check the numpy behaviour and comment. AFAIK it's possible to implement exception of ValueError if the input is empty tensor/array. But we need to take Dev team confirmation on this.

Thanks!
 Hi @FGRCL ,

AFAIK by default `reset_state()` will be called at the end of each epoch/step during training. So in that case this should not be a problem. WDYT ? This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. Hi @SuryanarayanaY 

Sorry for the delay. I think the bug I'm perceiving here is not necessarily how the MAE is calculated by Tensorflow when the metric is called as in the following way: `tf.keras.metrics.mean_absolute_error(y_true, y_pred)`, which is perfectly consistent with numpy, but rather the way that the `MeanAbsoluteError` class accumulates the mae when called with `.update_state()`.  The way I conceptualize it, `MeanAbsoluteError` starts out with 0 data points and returns `0.0`, therefore adding “more” 0 data points, i.e. `mae.update_state([])`, should retain that behaviour of returning `0.0`.

`reset_state()` doesn't necessarily solve the issue. For example, if batch no. 25 in an epoch of 100 batches passes an empty tensor to `update_state()`, then the metric's `result()` will return `nan` until the end of the epoch. If you were then to graph the MAE w.r.t. epochs, that graph would only be `nan` for all epochs. 

The particular use case in which I encountered this bug is the following. I needed to plot the MAE for samples where the target value is above or below a certain threshold. Naturally, some batches contain none of these samples, resulting in an empty tensor.

Let me know if I'm not understanding correctly the way these classes are intended to work. Hi @FGRCL ,

Apologies.This got slipped from my TODO list.This seems change in API design and needs to be addressed in [Keras](https://github.com/keras-team/keras/issues) repo. Would you mind opening this issue at Keras repo? Thanks!

Adding [gist](https://colab.research.google.com/gist/SuryanarayanaY/bd2485edcd0e91fda89654ee9083214b/61281_keras3.ipynb#scrollTo=rzgr0KGAtXCP) with Keras3 for reference. This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you. This issue was closed because it has been inactive for 7 days since being marked as stale. Please reopen if you'd like to work on this further. Are you satisfied with the resolution of your issue?
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=Yes&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61281">Yes</a>
<a href="https://docs.google.com/forms/d/e/1FAIpQLSfaP12TRhd9xSxjXZjcZFNXPGk4kc1-qMdv3gc6bEP90vY1ew/viewform?entry.85265664=No&entry.2137816233=https://github.com/tensorflow/tensorflow/issues/61281">No</a>

### Issue type

Bug

### Have you reproduced the bug with TensorFlow Nightly?

Yes

### Source

binary

### TensorFlow version

v1.12.1-96880-g4499c968316 2.14.0-dev20230714

### Custom code

No

### OS platform and distribution

macOS 13.2.1

### Mobile device

v

### Python version

3.11.4

### Bazel version

n/a

### GCC/compiler version

n/a

### CUDA/cuDNN version

n/a

### GPU model and memory

n/a

### Current behavior?

When using `MeanAbsoluteError.update_state(y_true, y_pred)` if `y_true` and `y_pred` happen to be empty tensors of size 0, all subsequent call to   `MeanAbsoluteError.result()` will yield "nan" until the internal state of the metric is reset. 

Since calling `MeanAbsoluteError.result()` before any result has been passed returns 0.0, I would expect empty tensors to be ignored when passed to `MeanAbsoluteError.update_state(y_true, y_pred)`.

I've only tested this issue with `MeanAbsoluteError`, but it seems likely that it would happen with other metrics.

### Standalone code to reproduce the issue

```shell
from unittest import TestCase

from tensorflow import constant, float32, reduce_mean
from tensorflow import ragged
from tensorflow.python.keras.metrics import MeanAbsoluteError


class TestEmptyTensorMetrics(TestCase):
    def test_no_data(self):
        metric = MeanAbsoluteError()

        result = metric.result()

        expected = constant(0.0)
        self.assertEqual(expected, result)

    def test_empty_array(self):
        metric = MeanAbsoluteError()
        y_true = constant([])
        y_pred = constant([])

        metric.update_state(y_true, y_pred)
        result = metric.result()

        expected = constant(0.0)
        self.assertEqual(expected, result)

    def test_multiple_batches(self):
        metric = MeanAbsoluteError()
        y_true_batches = constant([
            [39, 22, 73],
            [22, 50, 23]
        ], dtype=float32)
        y_pred_batches = constant([
            [80, 59, 52],
            [87, 8, 38],
        ], dtype=float32)

        for y_true, y_pred in zip(y_true_batches, y_pred_batches):
            metric.update_state(y_true, y_pred)
        result = metric.result()

        expected = reduce_mean(abs(y_true_batches - y_pred_batches))
        self.assertAlmostEqual(expected.numpy(), result.numpy(), 5)

    def test_multiple_batches_with_empty_array(self):
        metric = MeanAbsoluteError()
        y_true_batches = ragged.constant([
            [39, 22, 73],
            [],
            [22, 50, 23]
        ], dtype=float32)
        y_pred_batches = ragged.constant([
            [80, 59, 52],
            [],
            [87, 8, 38],
        ], dtype=float32)

        for y_true, y_pred in zip(y_true_batches, y_pred_batches):
            metric.update_state(y_true, y_pred)
        result = metric.result()

        expected = reduce_mean(abs(y_true_batches - y_pred_batches).flat_values)
        self.assertAlmostEqual(expected.numpy(), result.numpy(), 5)
```
```


### Relevant log output

```shell
2023-07-14 14:54:43.650532: I tensorflow/core/platform/cpu_feature_guard.cc:182] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
WARNING:tensorflow:From /Users/francois/.local/share/virtualenvs/sandbox-w3BOTv5B/lib/python3.11/site-packages/tensorflow/python/ops/distributions/distribution.py:259: ReparameterizationType.__init__ (from tensorflow.python.ops.distributions.distribution) is deprecated and will be removed after 2019-01-01.
Instructions for updating:
The TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.
WARNING:tensorflow:From /Users/francois/.local/share/virtualenvs/sandbox-w3BOTv5B/lib/python3.11/site-packages/tensorflow/python/ops/distributions/bernoulli.py:165: RegisterKL.__init__ (from tensorflow.python.ops.distributions.kullback_leibler) is deprecated and will be removed after 2019-01-01.
Instructions for updating:
The TensorFlow Distributions library has moved to TensorFlow Probability (https://github.com/tensorflow/probability). You should update all references to use `tfp.distributions` instead of `tf.distributions`.
test_empty_array (nanbug.TestEmptyTensorMetrics.test_empty_array) ... FAIL
test_multiple_batches (nanbug.TestEmptyTensorMetrics.test_multiple_batches) ... ok
test_multiple_batches_with_empty_array (nanbug.TestEmptyTensorMetrics.test_multiple_batches_with_empty_array) ... FAIL
test_no_data (nanbug.TestEmptyTensorMetrics.test_no_data) ... ok

======================================================================
FAIL: test_empty_array (nanbug.TestEmptyTensorMetrics.test_empty_array)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/francois/Documents/sandbox/nanbug.py", line 26, in test_empty_array
    self.assertEqual(expected, result)
AssertionError: <tf.Tensor: shape=(), dtype=float32, numpy=0.0> != <tf.Tensor: shape=(), dtype=float32, numpy=nan>

======================================================================
FAIL: test_multiple_batches_with_empty_array (nanbug.TestEmptyTensorMetrics.test_multiple_batches_with_empty_array)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/Users/francois/Documents/sandbox/nanbug.py", line 64, in test_multiple_batches_with_empty_array
    self.assertAlmostEqual(expected.numpy(), result.numpy(), 5)
AssertionError: 36.833332 != nan within 5 places (nan difference)

----------------------------------------------------------------------
Ran 4 tests in 0.104s

FAILED (failures=2)
```
