How to use precision or f1-score metrics in a multiclass classification - shape error

Hello,

I have tried to reproduce the following code for a multiclass classification problem (3 classes) from here:

import tensorflow as tf
from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split

# Load the Iris dataset
iris = load_iris()

# Split the dataset into training and test sets
X_train, X_test, y_train, y_test = train_test_split(
    iris.data, iris.target, test_size=0.2, random_state=42)


# Define the model
model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=(4,)),
    tf.keras.layers.Dense(3, activation='softmax')
])

# Compile the model
model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
# Train the model
history = model.fit(X_train, y_train, epochs=50, validation_split=0.2)

I noticed that when I change the line in model.compile with

metrics=['accuracy']

to

 metrics=[ tf.keras.metrics.Precision()])

to get a precision metric instead of accuracy, the code gives an error about shapes:

ValueError: Shapes (32, 3) and (32, 1) are incompatible

Error also happens if I try to add precision as a metric after accuracy like so:

metrics=['accuracy', tf.keras.metrics.Precision()]

I have also tried tensorflow addons and again get an error:

import tensorflow_addons as tfa
metrics= [tfa.metrics.F1Score(average="macro",num_classes = 3,threshold=None,name='f1_score', dtype=None)]

ValueError: Dimension 0 in both shapes must be equal, but are 3 and 1. Shapes are [3] and [1]. 

How can I optimize on precision or f1-score from Tensorflow metrics (
Module: tf.keras.metrics  |  TensorFlow v2.16.1)?

Hi @Maria_B, You can calculate precision by

y_pred = tf.argmax(model.predict(X_test), axis=1)

# Calculate precision
precision_metric = tf.keras.metrics.Precision()
precision_metric.update_state(y_test, y_pred)

print("Precision:", precision_metric.result().numpy())

for calculating F1_score

from sklearn.metrics import f1_score
print("F1_Score:",f1_score(y_test, y_pred, average='weighted'))

Please refer to this gist for working code example.Thank You.