Search code examples
tensorflowmachine-learningkerasdeep-learningmetrics

Tensorflow & Keras: Building Custom Metric for Precision


I have Sub-Classed the Metric class to create a custom precision metric. Everything looks fine; I mean there is no run-time error. But I suspect there is something wrong when I see the precision scores logging in the output, it always shows the same score at the end of each epoch, e.g., epoch 0.1000 or 0.9000

Here is my code:


(X_train_10, y_train_10), (X_test_10, y_test_10) = keras.datasets.cifar10.load_data()

X_train_10 = X_train_10 / 255.
X_test_10 = X_test_10 / 255.

class PrecisionMetric(keras.metrics.Metric):
    def __init__(self, name = 'precision', **kwargs):
        super(PrecisionMetric, self).__init__(**kwargs)
        self.tp = self.add_weight('tp', initializer = 'zeros')
        self.fp = self.add_weight('fp', initializer = 'zeros')

    def update_state(self, y_true, y_pred):
        y_true = tf.cast(y_true, tf.bool)
        y_pred = tf.cast(y_pred, tf.bool)        
        true_p = tf.logical_and(tf.equal(y_true, True), tf.equal(y_pred, True))
        false_p = tf.logical_and(tf.equal(y_true, False), tf.equal(y_pred, True))
        self.tp.assign_add(tf.reduce_sum(tf.cast(true_p, self.dtype)))
        self.fp.assign_add(tf.reduce_sum(tf.cast(false_p, self.dtype)))
    def reset_states(self):
        self.tp.assign(0)
        self.fp.assign(0)

    def result(self):
        return self.tp / (self.tp + self.fp)


keras.backend.clear_session()
model = keras.models.Sequential()

model.add(keras.layers.Flatten(input_shape = np.array(X_train_10.shape[1: ])))
for _ in range(2):
    model.add(keras.layers.Dense(50, activation = 'elu', kernel_initializer = 'he_normal'))
model.add(keras.layers.Dense(1, activation = 'sigmoid'))

loss = keras.losses.binary_crossentropy
optimizer = keras.optimizers.SGD()

model.compile(loss = loss, optimizer = optimizer, metrics = [PrecisionMetric()])

# To make it binary classification
y_train_5 = (y_train_10 == 5)
y_test_5 = (y_test_10 == 5)

history = model.fit(X_train_10, y_train_5, epochs = 5)

Output after running 5 epoch:

Epoch 1/5
1563/1563 [==============================] - 5s 4ms/step - loss: 0.2921 - precision_metric: 0.1000
Epoch 2/5
1563/1563 [==============================] - 4s 2ms/step - loss: 0.2744 - precision_metric: 0.1000
Epoch 3/5
1563/1563 [==============================] - 3s 2ms/step - loss: 0.2682 - precision_metric: 0.1000
Epoch 4/5
1563/1563 [==============================] - 3s 2ms/step - loss: 0.2651 - precision_metric: 0.1000
Epoch 5/5
1563/1563 [==============================] - 4s 3ms/step - loss: 0.2629 - precision_metric: 0.1000

Note: even tried with a higher number of the epochs, e.g., 100, and still outputs the exact same precision, while the loss keeps shrinking/getting-smaller.


Solution

  • EDIT

    There is a mistake in your implementation. You should not cast y_pred to bool. These are outputs from sigmoid. You should do

    y_pred = tf.math.greater(y_pred, 0.5)
    

    when your problem is binary (you get output from sigmoid). And adapt it for multi-class classification.

    Please in result check if you are not dividing 0/0 via:

        def result(self):
            res =  self.tp / (self.tp + self.fp)
            if tf.math.is_nan(res):
                return 0.0
            else:
                return res
    

    I apologize for first wrong answer, although I hope it was somewhat educational.