F1 score error on multi class data
Hi @Glfrey ,

Thanks for reporting. Unfortunately the attached drive file can't accessible for me. Could you please provide the access. I request you to provide colab gist for ease of access with a minimal code snippet which can help us to look into the issue.

Thanks! Hi @SuryanarayanaY,

Thanks for your rapid response. I've transferred it over to colab here's the link https://colab.research.google.com/drive/1CoKr7DNBT3VDqSQlUAPfZPH0pYlj1-cq?usp=sharing

 > Note: This API is new and only available via pip install tf-nightly.

https://www.tensorflow.org/api_docs/python/tf/keras/metrics/F1Score @DenisVorotyntsev  Yes I'm aware, my tests were with the nightly builds but thank you for sharing.  @Glfrey, the error you mentioned in the first post doesn't match the error in the notebook, which confused me. 

I looked into F1, and it seems it required input to be one-hot-encoded to make the metric work. I made a conversion to ohe in the [notebook](https://colab.research.google.com/drive/1HRLJBvKhwAUAoKkZxMF7csh_GG3M9w9Y?usp=sharing) and it works. I also added a custom metric class that converts labels to ohe on the fly, which could lead to less memory consumption.  @DenisVorotyntsev huh, that's weird. I'm definitely on the nightly build at the moment I just checked. Either way, you're absolutely right. I completely overlooked the requirement for one-hot-encoding. My conversion to that representation of my labels has enabled it to work, so thank you very much! 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/60920">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/60920">No</a>

<details><summary>Click to expand!</summary> 
 
 ### Issue Type

Bug

### Have you reproduced the bug with TF nightly?

Yes

### Source

binary

### Tensorflow Version

v1.12.1-95639-g08bd7e1a8e5 2.14.0-dev20230618

### Custom Code

Yes

### OS Platform and Distribution

OS Ventura 13.0.1

### Mobile device

_No response_

### Python version

3.8

### Bazel version

_No response_

### GCC/Compiler version

_No response_

### CUDA/cuDNN version

_No response_

### GPU model and memory

_No response_

### Current Behaviour?

Implementing the F1 score available in the nightly builds on multi-class data such as below:

```
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
                metrics= tf.keras.metrics.F1Score())

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))
```

triggers the following error:

```
Epoch 1/10
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[8], line 5
      1 model.compile(optimizer='adam',
      2               loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
      3                 metrics= tf.keras.metrics.F1Score())
----> 5 history = model.fit(train_images, train_labels, epochs=10, 
      6                     validation_data=(test_images, test_labels))

File /opt/homebrew/lib/python3.8/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File /var/folders/f5/mkqkf_0d42qcsqc37hd_y0hm0000gn/T/__autograph_generated_fileb8tcgui2.py:15, in outer_factory.<locals>.inner_factory.<locals>.tf__train_function(iterator)
     13 try:
     14     do_return = True
---> 15     retval_ = ag__.converted_call(ag__.ld(step_function), (ag__.ld(self), ag__.ld(iterator)), None, fscope)
     16 except:
     17     do_return = False

ValueError: in user code:

    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/engine/training.py", line 1338, in train_function  *
        return step_function(self, iterator)
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/engine/training.py", line 1322, in step_function  **
        outputs = model.distribute_strategy.run(run_step, args=(data,))
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/engine/training.py", line 1303, in run_step  **
        outputs = model.train_step(data)
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/engine/training.py", line 1085, in train_step
        return self.compute_metrics(x, y, y_pred, sample_weight)
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/engine/training.py", line 1179, in compute_metrics
        self.compiled_metrics.update_state(y, y_pred, sample_weight)
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/engine/compile_utils.py", line 605, in update_state
        metric_obj.update_state(y_t, y_p, sample_weight=mask)
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/utils/metrics_utils.py", line 77, in decorated
        update_op = update_state_fn(*args, **kwargs)
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/metrics/base_metric.py", line 140, in update_state_fn
        return ag_update_state(*args, **kwargs)
    File "/opt/homebrew/lib/python3.8/site-packages/keras/src/metrics/f_score_metrics.py", line 176, in update_state  **
        y_true = tf.convert_to_tensor(y_true, dtype=self.dtype)

    ValueError: Tensor conversion requested dtype float32 for Tensor with dtype uint8: <tf.Tensor 'IteratorGetNext:1' shape=(None, 1) dtype=uint8>
```

I've tried with multiple multi-class datasets and the same error is returned. The F1 score page says it should work with multi-class data https://www.tensorflow.org/api_docs/python/tf/keras/metrics/F1Score. Is there something I've missed regarding its implementation for multi-class data (such as somewhere to specify the number of classes?) or is this a bug?



### Standalone code to reproduce the issue

```shell
Here is a Jupyter notebook with some example data from https://www.tensorflow.org/tutorials/images/cnn

https://drive.google.com/file/d/1tExJ80AktA87EmsExOPEMWsevoiQz4VX/view?usp=share_link
```


### Relevant log output

_No response_</details>