advanced patterns when using tf.keras.metrics.Precision with multi-class classification in TensorFlow 2.12
After trying multiple solutions online, I still can't figure this out. I tried several approaches but none seem to work. I'm working on a multi-class classification question using TensorFlow 2.12, and I'm trying to track precision during training using `tf.keras.metrics.Precision`. However, I'm experiencing unexpected behavior where the precision metric is consistently reporting a value of 0.0 after several training epochs, even though I can see the model making correct predictions. Here's the relevant part of my code: ```python import tensorflow as tf # Sample dataset (x_train, y_train), (x_test, y_test) = tf.keras.datasets.cifar10.load_data() # Normalize pixel values x_train = x_train.astype('float32') / 255.0 x_test = x_test.astype('float32') / 255.0 # Convert labels to one-hot encoding num_classes = 10 y_train = tf.keras.utils.to_categorical(y_train, num_classes) y_test = tf.keras.utils.to_categorical(y_test, num_classes) model = tf.keras.Sequential([ tf.keras.layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3)), tf.keras.layers.MaxPooling2D(), tf.keras.layers.Flatten(), tf.keras.layers.Dense(64, activation='relu'), tf.keras.layers.Dense(num_classes, activation='softmax') ]) model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', tf.keras.metrics.Precision()]) # Train the model model.fit(x_train, y_train, epochs=10, validation_data=(x_test, y_test)) ``` Despite the model achieving a decent accuracy (around 75%), the precision metric remains at 0.0. I suspect that it might be related to how I'm initializing `tf.keras.metrics.Precision`. I've tried different configurations, including setting `class_id`, but it didn't seem to help. Hereβs how I attempted to set it up: ```python precision = tf.keras.metrics.Precision(class_id=1) # Trying for one class model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy', precision]) ``` However, it still only reports 0.0. I'm confused about how to correctly use `tf.keras.metrics.Precision` in this context. Any insights on what might be going wrong or how to properly integrate multi-class precision tracking would be greatly appreciated!