File size: 7,297 Bytes
da67e9c |
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import json\n",
"import math\n",
"from pathlib import Path\n",
"\n",
"import numpy as np\n",
"import pandas as pd\n",
"from datasets import Dataset\n",
"from sklearn.metrics import f1_score, accuracy_score, log_loss\n",
"from tqdm import tqdm\n",
"\n",
"from models.models import language_to_models"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"en = \"en\"\n",
"ru = \"ru\"\n",
"datasets_dir = Path(\"datasets\")\n",
"test_filename = \"arxiv_test\"\n",
"test_dataset_filename = {\n",
" en: datasets_dir / en / test_filename,\n",
" ru: datasets_dir / ru / test_filename,\n",
"}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_datasets = {}\n",
"for lang in (en, ru):\n",
" csv_file = str(test_dataset_filename[lang]) + \".csv\"\n",
" json_file = str(test_dataset_filename[lang]) + \".json\"\n",
" if Path(csv_file).exists():\n",
" test_datasets[lang] = pd.read_csv(csv_file)\n",
" else:\n",
" test_datasets[lang] = pd.read_json(json_file, lines=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"test_results_filename = Path(\"test_results.json\")\n",
"if test_results_filename.exists():\n",
" with open(test_results_filename, \"r\") as f:\n",
" test_results = json.load(f)\n",
"else:\n",
" test_results = {}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def pred_to_1d(pred):\n",
" return pred.idxmax(axis=1)\n",
"\n",
"\n",
"def true_to_nd(true, columns):\n",
" columns = list(columns)\n",
" true_arr = np.zeros((len(true), len(columns)))\n",
" column_numbers = true.apply(lambda label: columns.index(label)).to_numpy()\n",
" one_inds = np.column_stack((np.arange(len(true)), column_numbers))\n",
" true_arr[one_inds] = 1\n",
" true = pd.DataFrame(true_arr, columns=columns)\n",
" return true\n",
"\n",
"\n",
"def accuracy(pred, true):\n",
" return accuracy_score(true, pred_to_1d(pred))\n",
"\n",
"\n",
"def f1(pred, true):\n",
" return f1_score(true, pred_to_1d(pred), average=\"macro\")\n",
"\n",
"\n",
"def cross_entropy(pred, true):\n",
" pred = pd.DataFrame(\n",
" pred.to_numpy() / pred.sum(axis=1).to_numpy()[:, None], columns=pred.columns\n",
" )\n",
" return log_loss(true_to_nd(true, pred.columns), pred)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"metrics = {\"Macro F1\": f1, \"Accuracy\": accuracy, \"Cross-entropy loss\": cross_entropy}"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"predications_dir = Path(\"pred\")\n",
"predications_dir.mkdir(exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def canonicalize_label(label):\n",
" if \".\" in label:\n",
" return label[: label.index(\".\")]\n",
" return label\n",
"\n",
"\n",
"def predict(model_name, model, dataset: pd.DataFrame, batch_size=32, first: int = 3000):\n",
" label = \"category\"\n",
" all_labels = list(dataset[label].unique())\n",
" if first is not None:\n",
" dataset = dataset[:first]\n",
" true = dataset[label]\n",
" prediction_file_path = predications_dir / (model_name + \".csv\")\n",
" dataset_size = len(dataset)\n",
" if not prediction_file_path.exists():\n",
" preds = []\n",
" for i in tqdm(\n",
" range(0, dataset_size + batch_size, batch_size),\n",
" desc=f\"Predicting using {model_name}\",\n",
" total=math.ceil(dataset_size / batch_size),\n",
" unit=\"batch\",\n",
" ):\n",
" data = dataset.iloc[i : i + batch_size]\n",
" if data.empty:\n",
" break\n",
" data = Dataset.from_pandas(data)\n",
" batch_pred = model(data)\n",
" batch_pred_canonicalised = []\n",
" for paper_pred in batch_pred:\n",
" labels_dict = {}\n",
" for label_score in paper_pred:\n",
" label = canonicalize_label(label_score[\"label\"])\n",
" if label not in all_labels:\n",
" return None, None\n",
" labels_dict[label] = label_score[\"score\"]\n",
" batch_pred_canonicalised.append(labels_dict)\n",
" preds.extend(batch_pred_canonicalised)\n",
" else:\n",
" preds = pd.read_csv(prediction_file_path, index_col=0)\n",
" preds = pd.DataFrame(preds).fillna(0)\n",
" for label in all_labels:\n",
" if label not in preds.columns:\n",
" preds[label] = 0\n",
" preds = preds.reindex(sorted(preds.columns), axis=1)\n",
" if not prediction_file_path.exists():\n",
" preds.to_csv(prediction_file_path)\n",
" return preds, true\n",
"\n",
"\n",
"for lang, name_get_model in language_to_models.items():\n",
" lang_results = test_results.setdefault(lang, {})\n",
" for metric_name, metic in metrics.items():\n",
" metrics_results = lang_results.setdefault(metric_name, {})\n",
" for model_name, get_model in name_get_model.items():\n",
" model_name = model_name.replace(\"/\", \".\")\n",
" if model_name not in metrics_results:\n",
" test_size = 3000 if en == lang else 500\n",
" pred, true = predict(model_name, get_model(), test_datasets[lang], first=test_size)\n",
" if pred is None:\n",
" print(f\"{model_name} does not produce labels that we can estimate\")\n",
" continue\n",
" metrics_results[model_name] = metic(pred, true)\n",
" print(f\"{metric_name} for {model_name} = {metrics_results[model_name]}\")"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"with open(test_results_filename, \"w\") as f:\n",
" json.dump(test_results, f)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|