# How to define a custom performance metric in Keras?

I tried to define a custom metric fuction (F1-Score) in Keras (Tensorflow backend) according to the following:

def f1_score(tags, predicted):

tags = set(tags)
predicted = set(predicted)

tp = len(tags & predicted)
fp = len(predicted) - tp
fn = len(tags) - tp

if tp>0:
precision=float(tp)/(tp+fp)
recall=float(tp)/(tp+fn)
return 2*((precision*recall)/(precision+recall))
else:
return 0


So far, so good, but when I try to apply it in model compilation:

model1.compile(loss="binary_crossentropy", optimizer=Adam(), metrics=[f1_score])


it gives error:

TypeError                                 Traceback (most recent call last)
<ipython-input-85-4eca4def003f> in <module>()
6
----> 7 model1.compile(loss="binary_crossentropy", optimizer=Adam(), metrics=[f1_score])
8
9 h=model1.fit(X_train, Y_train, batch_size=500, nb_epoch=5, verbose=True, validation_split=0.1)

/home/buda/anaconda2/lib/python2.7/site-packages/keras/models.pyc in compile(self, optimizer, loss, metrics, sample_weight_mode, **kwargs)
522                            metrics=metrics,
523                            sample_weight_mode=sample_weight_mode,
--> 524                            **kwargs)
525         self.optimizer = self.model.optimizer
526         self.loss = self.model.loss

/home/buda/anaconda2/lib/python2.7/site-packages/keras/engine/training.pyc in compile(self, optimizer, loss, metrics, loss_weights, sample_weight_mode, **kwargs)
664                 else:
665                     metric_fn = metrics_module.get(metric)
--> 666                     self.metrics_tensors.append(metric_fn(y_true, y_pred))
667                     if len(self.output_names) == 1:
668                         self.metrics_names.append(metric_fn.__name__)

<ipython-input-84-b8a5752b6d55> in f1_score(tags, predicted)
4     #tf.convert_to_tensor(img.eval())
5
----> 6     tags = set(tags)
7     predicted = set(predicted)
8

/home/buda/anaconda2/lib/python2.7/site-packages/tensorflow/python/framework/ops.pyc in __iter__(self)
493       TypeError: when invoked.
494     """
--> 495     raise TypeError("'Tensor' object is not iterable.")
496
497   def __bool__(self):

TypeError: 'Tensor' object is not iterable.


What is the problem here? The fact that my f1_score function inputs are not Tensorflow arrays? If so, where/how can I convert them correctly?

• Hmm, the error message does imply you are getting tensor objects. Perhaps you need the eval after all! If so, your mistake is likely to be using eval when you mean eval() – Neil Slater Aug 30 '16 at 9:08

You have to use Keras backend functions. Unfortunately they do not support the &-operator, so that you have to build a workaround: We generate matrices of the dimension batch_size x 3, where (e.g. for true positive) the first column is the ground truth vector, the second the actual prediction and the third is kind of a label-helper column, that contains in the case of true positive only ones. Then we check which instances are positive instances, are predicted as positive and the label-helper is also positive. Those are the true positives.

We can make this analog with false positives, false negatives and true negatives with some reverse-calculations of the labels.

Your f1-metric may look as follows:

def f1_score(y_true, y_pred):
"""
f1 score

:param y_true:
:param y_pred:
:return:
"""
tp_3d = K.concatenate(
[
K.cast(y_true, 'bool'),
K.cast(K.round(y_pred), 'bool'),
K.cast(K.ones_like(y_pred), 'bool')
], axis=1
)

fp_3d = K.concatenate(
[
K.cast(K.abs(y_true - K.ones_like(y_true)), 'bool'),
K.cast(K.round(y_pred), 'bool'),
K.cast(K.ones_like(y_pred), 'bool')
], axis=1
)

fn_3d = K.concatenate(
[
K.cast(y_true, 'bool'),
K.cast(K.abs(K.round(y_pred) - K.ones_like(y_pred)), 'bool'),
K.cast(K.ones_like(y_pred), 'bool')
], axis=1
)

tp = K.sum(K.cast(K.all(tp_3d, axis=1), 'int32'))
fp = K.sum(K.cast(K.all(fp_3d, axis=1), 'int32'))
fn = K.sum(K.cast(K.all(fn_3d, axis=1), 'int32'))

precision = tp / (tp + fp)
recall = tp / (tp + fn)
return 2 * ((precision * recall) / (precision + recall))


Since the Keras-backend calculator returns nan for division by zero, we do not need the if-else-statement for the return statement.

Edit: I have found a pretty good idea for a exact implementation. The problem with our first approach is, that it is only "approximated", since it is computed batchwise and subsequently averaged. One could also calculate this after each epoch with the keras.callbacks. Please find the idea here.

An example implementation would be:

import keras
import numpy as np
import sklearn.metrics as sklm

class Metrics(keras.callbacks.Callback):
def on_train_begin(self, logs={}):
self.confusion = []
self.precision = []
self.recall = []
self.f1s = []
self.kappa = []
self.auc = []

def on_epoch_end(self, epoch, logs={}):
score = np.asarray(self.model.predict(self.validation_data[0]))
predict = np.round(np.asarray(self.model.predict(self.validation_data[0])))
targ = self.validation_data[1]

self.auc.append(sklm.roc_auc_score(targ, score))
self.confusion.append(sklm.confusion_matrix(targ, predict))
self.precision.append(sklm.precision_score(targ, predict))
self.recall.append(sklm.recall_score(targ, predict))
self.f1s.append(sklm.f1_score(targ, predict))
self.kappa.append(sklm.cohen_kappa_score(targ, predict))

return


To make the network to call this function you simply add it to you callbacks like

metrics = Metrics()
model.fit(
train_instances.x,
train_instances.y,
batch_size,
epochs,
verbose=2,
callbacks=[metrics],
validation_data=(valid_instances.x, valid_instances.y),
)


Then you can simply access the members of the metrics variable.

• Thank you, this has already been really useful. Do you know how to incorporate the custom metrics into a tensorboard callback so they can be monitored during training? – N.Kaiser Jun 6 '18 at 15:49