{"cells":[{"metadata":{},"cell_type":"markdown","source":"I implemented continuous approximation of quadratic weighted cohen's kappa for regressor on Keras.  \nThe below function can be used as regressor loss function (but for loss, shoud change 1-k to k for smaller the better).  \nThe performance of this loss is same level as binary_crossentropy in this competiton.  \n\nI don't know the reason, there seems some local optima around 1.0, then when you use this, recommend to use other loss for the warming up (1 or 2 epochs). "},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import keras.backend as K\n# For scoring coding on continuous manner.\ndef QWKloss_score(y_true, y_pred):\n    N = K.cast(K.shape(y_true)[0], 'float32')\n    \n    WC = (y_pred - y_true)**2 / N\n    WE = (y_pred - K.transpose(y_true))**2 / (N**2)\n    \n    k = K.sum(WC) / K.sum(WE)\n    \n    return 1-k","execution_count":null,"outputs":[]},{"metadata":{"_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","trusted":true},"cell_type":"code","source":"import numpy as np\nfrom sklearn.metrics import cohen_kappa_score\n\n# check ~ comparing sklearn\nfor _ in range(10):\n    N = 1000\n    y_true = np.random.randint(5, size=(N))\n    y_pred = np.random.randint(5, size=(N))  # cast to int is necessary for sklearn. but youcan use float in Keras use.\n\n    skl = cohen_kappa_score(y_true, y_pred, weights='quadratic')\n\n    s = QWKloss_score(K.variable(y_true.reshape(-1, 1)), K.variable(y_pred.reshape(-1, 1)))\n    org = K.get_value(s)\n    \n    print(skl, org)","execution_count":null,"outputs":[]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"name":"python","version":"3.6.4","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"}},"nbformat":4,"nbformat_minor":1}