{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"markdown","source":"# Keras Macro F1-Score Implementation\nHi all!\nHere there is my implementation of the Macro-F1-Score in keras/tensorflow that gives the same results of the sklearn implementation on 'macro' mode and has very similar values to the LB scores. Both y_true and y_pred are matrices of size (batch_size, 28)."},{"metadata":{"trusted":true,"_uuid":"5fb545ec9a94c8c4411a81b6e5ff122cfb5a6078"},"cell_type":"code","source":"import tensorflow as tf\nimport keras.backend as K\n\ndef f1(y_true, y_pred):\n    y_pred = K.round(y_pred)\n    tp = K.sum(K.cast(y_true*y_pred, 'float'), axis=0)\n    # tn = K.sum(K.cast((1-y_true)*(1-y_pred), 'float'), axis=0)\n    fp = K.sum(K.cast((1-y_true)*y_pred, 'float'), axis=0)\n    fn = K.sum(K.cast(y_true*(1-y_pred), 'float'), axis=0)\n\n    p = tp / (tp + fp + K.epsilon())\n    r = tp / (tp + fn + K.epsilon())\n\n    f1 = 2*p*r / (p+r+K.epsilon())\n    f1 = tf.where(tf.is_nan(f1), tf.zeros_like(f1), f1)\n    return K.mean(f1)","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"c4267667921b94704de6711667b6097ef996317b"},"cell_type":"markdown","source":"### Testing Metric"},{"metadata":{"trusted":true,"_uuid":"e0a6d9eedc70a34b140ca92da155b1762c65cf5f"},"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import f1_score\n\n# Samples\ny_true = np.array([[1,1,0,0,1], [1,0,1,1,0], [0,1,1,0,0]])\ny_pred = np.array([[0,1,1,1,1], [1,0,0,1,1], [1,0,1,0,0]])\n\nprint('Shape y_true:', y_true.shape)\nprint('Shape y_pred:', y_pred.shape)\n\n# Results\nprint('sklearn Macro-F1-Score:', f1_score(y_true, y_pred, average='macro'))\nprint('Custom Macro-F1-Score:', K.eval(f1(y_true, y_pred)))","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_uuid":"27ca49e17c2479795160bc03275433a124fc754b"},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.6","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}