-
Notifications
You must be signed in to change notification settings - Fork 0
/
tf_custom_metric.py
33 lines (27 loc) · 1.04 KB
/
tf_custom_metric.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Fri May 20 11:30:25 2022
custom metric to supercede @tf.function
@author: jack
"""
import tensorflow as tf
@tf.function
def macro_soft_f1(y, y_hat):
"""Compute the macro soft F1-score as a cost (average 1 - soft-F1 across all labels).
Use probability values instead of binary predictions.
Args:
y (int32 Tensor): targets array of shape (BATCH_SIZE, N_LABELS)
y_hat (float32 Tensor): probability matrix from forward propagation of shape (BATCH_SIZE, N_LABELS)
Returns:
cost (scalar Tensor): value of the cost function for the batch
"""
y = tf.cast(y, tf.float32)
y_hat = tf.cast(y_hat, tf.float32)
tp = tf.reduce_sum(y_hat * y, axis=0)
fp = tf.reduce_sum(y_hat * (1 - y), axis=0)
fn = tf.reduce_sum((1 - y_hat) * y, axis=0)
soft_f1 = 2*tp / (2*tp + fn + fp + 1e-16)
cost = 1 - soft_f1 # reduce 1 - soft-f1 in order to increase soft-f1
macro_cost = tf.reduce_mean(cost) # average on all labels
return macro_cost