{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Reference: https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854","metadata":{}},{"cell_type":"markdown","source":"In the discussion, the division was done by the sum of the weights, but isn't it correct to divide by each row?\n\nver 4: @harshitsheoran pointed out mistake, fixed!","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nimport numpy as np\nimport torch\nimport torch.nn as nn","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2022-08-15T16:59:01.878594Z","iopub.execute_input":"2022-08-15T16:59:01.879382Z","iopub.status.idle":"2022-08-15T16:59:01.884714Z","shell.execute_reply.started":"2022-08-15T16:59:01.879337Z","shell.execute_reply":"2022-08-15T16:59:01.883613Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train = pd.read_csv('../input/rsna-2022-cervical-spine-fracture-detection/train.csv')\ntargets = ['patient_overall', 'C1', 'C2', 'C3', 'C4', 'C5', 'C6', 'C7']\nlabels = torch.tensor(train[targets].values)\nmean_values = train.mean(axis=0).values\nmean_values = torch.tensor(np.vstack([mean_values]*len(train)))","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:59:02.999811Z","iopub.execute_input":"2022-08-15T16:59:03.00052Z","iopub.status.idle":"2022-08-15T16:59:03.031137Z","shell.execute_reply.started":"2022-08-15T16:59:03.000478Z","shell.execute_reply":"2022-08-15T16:59:03.029733Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"This metrics from https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854","metadata":{}},{"cell_type":"code","source":"device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n# change it to nn.BCELoss(reduction='none') if you have sigmoid activation in last layer\nloss_fn = nn.BCELoss(reduction=\"none\") \n\ncompetition_weights = {\n    '-' : torch.tensor([7, 1, 1, 1, 1, 1, 1, 1], dtype=torch.float, device=device),\n    '+' : torch.tensor([14, 2, 2, 2, 2, 2, 2, 2], dtype=torch.float, device=device),\n}\n","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:59:43.160584Z","iopub.execute_input":"2022-08-15T16:59:43.161055Z","iopub.status.idle":"2022-08-15T16:59:43.168362Z","shell.execute_reply.started":"2022-08-15T16:59:43.161014Z","shell.execute_reply":"2022-08-15T16:59:43.167356Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# original metrics from the discussion https://www.kaggle.com/competitions/rsna-2022-cervical-spine-fracture-detection/discussion/341854\ndef competiton_loss(y_hat, y):\n    loss = loss_fn(y_hat, y)\n    weights = y * competition_weights['+'] + (1 - y) * competition_weights['-']\n    loss = (loss * weights).sum(axis=1).mean()\n\n    return loss / weights.sum()\n\nprint(competiton_loss(mean_values, labels.double()))","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:59:44.238536Z","iopub.execute_input":"2022-08-15T16:59:44.239196Z","iopub.status.idle":"2022-08-15T16:59:44.247557Z","shell.execute_reply.started":"2022-08-15T16:59:44.23916Z","shell.execute_reply":"2022-08-15T16:59:44.24647Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Too small value?","metadata":{}},{"cell_type":"code","source":"# without weights normalization\ndef competiton_loss_wo_norm(y_hat, y):\n    loss = loss_fn(y_hat, y)\n    weights = y * competition_weights['+'] + (1 - y) * competition_weights['-']\n    loss = (loss * weights).sum(axis=1).mean()\n\n    return loss #/ weights.sum()\n\nprint(competiton_loss_wo_norm(mean_values, labels.double()))","metadata":{"execution":{"iopub.status.busy":"2022-08-15T16:59:45.53499Z","iopub.execute_input":"2022-08-15T16:59:45.535752Z","iopub.status.idle":"2022-08-15T16:59:45.543753Z","shell.execute_reply.started":"2022-08-15T16:59:45.535707Z","shell.execute_reply":"2022-08-15T16:59:45.542586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Too big value?","metadata":{}},{"cell_type":"code","source":"# with row-wise weights normalization\ndef competiton_loss_row_norm(y_hat, y):\n    loss = loss_fn(y_hat, y)\n    weights = y * competition_weights['+'] + (1 - y) * competition_weights['-']\n    loss = (loss * weights).sum(axis=1)\n    w_sum = weights.sum(axis=1)\n    loss = torch.div(loss, w_sum)\n    return loss.mean()\n\nprint(\"The overall loss is\",competiton_loss_row_norm(mean_values, labels.double()))","metadata":{"execution":{"iopub.status.busy":"2022-08-15T17:04:07.464052Z","iopub.execute_input":"2022-08-15T17:04:07.464481Z","iopub.status.idle":"2022-08-15T17:04:07.474389Z","shell.execute_reply.started":"2022-08-15T17:04:07.464447Z","shell.execute_reply":"2022-08-15T17:04:07.473324Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":" mean preds can get 0.57 on public LB, which is almost identical to this value.","metadata":{}}]}