File size: 31,776 Bytes
eccd289 |
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 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Deep Learning Project - Emotion Classification\n",
"\n",
"This notebook implements a **multi-label emotion classification system** using state-of-the-art transformer models. The goal is to predict multiple emotions (anger, fear, joy, sadness, surprise) that may be present in a given text.\n",
"\n",
"**Key Features:**\n",
"- **Model**: Microsoft DeBERTa-v3-base (184M parameters)\n",
"- **Strategy**: 5-Fold Stratified Cross-Validation for robust performance estimation\n",
"- **Optimization**: Mixed Precision Training, Gradient Clipping, Learning Rate Warmup\n",
"- **Evaluation**: Macro F1 Score with Per-Label Threshold Tuning\n",
"- **Ensemble**: Average predictions across all folds for final submission\n",
"\n",
"**Problem Type**: Multi-label classification (each text can have 0 or more emotions)\n",
"\n",
"---\n",
"\n",
"## 1. Imports & Setup\n",
"\n",
"We import all necessary libraries for:\n",
"- **Data handling**: `numpy`, `pandas` for data manipulation\n",
"- **Deep learning**: `torch` (PyTorch) and `transformers` (Hugging Face) for model training\n",
"- **Evaluation**: `sklearn` for F1 metrics and stratified k-fold cross-validation\n",
"- **Optimization**: Mixed precision training with `autocast` and `GradScaler` to speed up training and reduce memory usage\n",
"- **Memory management**: `gc` for garbage collection to free up GPU memory between folds"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import numpy as np\n",
"import pandas as pd\n",
"import torch\n",
"import torch.nn as nn\n",
"from sklearn.model_selection import StratifiedKFold\n",
"from sklearn.metrics import f1_score\n",
"from transformers import (\n",
" AutoTokenizer,\n",
" AutoModelForSequenceClassification,\n",
" get_linear_schedule_with_warmup,\n",
" AutoConfig\n",
")\n",
"from torch.optim import AdamW\n",
"from torch.cuda.amp import autocast, GradScaler\n",
"import gc\n",
"import warnings\n",
"import os\n",
"\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 2. Configuration\n",
"\n",
"Centralized configuration class containing all hyperparameters and paths. This makes it easy to:\n",
"- Experiment with different settings\n",
"- Ensure reproducibility\n",
"- Keep the code organized\n",
"\n",
"**Key Hyperparameters:**\n",
"- `MODEL_NAME`: DeBERTa-v3-base chosen for its strong performance on text classification tasks\n",
"- `MAX_LEN=128`: Balance between capturing context and computational efficiency\n",
"- `BATCH_SIZE=16`: Fits in GPU memory while maintaining good gradient estimates\n",
"- `LR=1.5e-5`: Small learning rate typical for fine-tuning pre-trained transformers\n",
"- `EPOCHS=4`: Sufficient for fine-tuning without overfitting\n",
"- `N_FOLDS=5`: Standard choice for cross-validation, balances between training data and validation reliability"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# ========= CONFIG =========\n",
"class Config:\n",
" SEED = 42\n",
" LABELS = [\"anger\", \"fear\", \"joy\", \"sadness\", \"surprise\"]\n",
" MODEL_NAME = \"microsoft/deberta-v3-base\"\n",
" MAX_LEN = 128\n",
" BATCH_SIZE = 16\n",
" EPOCHS = 4\n",
" LR = 1.5e-5\n",
" WEIGHT_DECAY = 0.01\n",
" WARMUP_RATIO = 0.1\n",
" N_FOLDS = 5\n",
" TRAIN_CSV = \"/kaggle/input/2025-sep-dl-gen-ai-project/train.csv\"\n",
" TEST_CSV = \"/kaggle/input/2025-sep-dl-gen-ai-project/test.csv\"\n",
" SUBMISSION_PATH = \"submission.csv\"\n",
"\n",
"CONFIG = Config()"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3. Seed & Device Setup\n",
"\n",
"**Reproducibility**: Setting seeds ensures that our results can be replicated exactly.\n",
"We set seeds for:\n",
"- NumPy random number generation\n",
"- PyTorch CPU operations\n",
"- PyTorch GPU operations (all CUDA devices)\n",
"- Python's built-in hash function\n",
"\n",
"**Device Selection**: Automatically detects and uses GPU if available (CUDA), otherwise falls back to CPU.\n",
"GPU training is significantly faster (~10-50x) than CPU for deep learning models."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def set_seed(seed=CONFIG.SEED):\n",
" np.random.seed(seed)\n",
" torch.manual_seed(seed)\n",
" torch.cuda.manual_seed_all(seed)\n",
" os.environ['PYTHONHASHSEED'] = str(seed)\n",
"\n",
"set_seed()\n",
"\n",
"device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
"print(f\"Using device: {device}\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 4. Utility Functions\n",
"\n",
"Helper functions used throughout the pipeline:\n",
"\n",
"### `ensure_text_column(df)`\n",
"- Standardizes the text column name across different datasets\n",
"- Searches for common alternatives like 'comment_text', 'sentence', etc.\n",
"- Raises error if no text column is found\n",
"\n",
"### `tune_thresholds(y_true, y_prob)`\n",
"- **Critical for multi-label classification performance**\n",
"- Default threshold of 0.5 is often suboptimal\n",
"- Finds the best threshold per label that maximizes F1 score\n",
"- Tests 17 different thresholds between 0.1 and 0.9\n",
"- Can improve F1 score by 2-5% over default threshold\n",
"\n",
"### `get_optimizer_params(model, lr, weight_decay)`\n",
"- Implements **differential weight decay**\n",
"- Applies weight decay to most parameters (helps prevent overfitting)\n",
"- No weight decay for bias and LayerNorm parameters (standard practice in transformer fine-tuning)\n",
"- This technique is recommended in the BERT and DeBERTa papers"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def ensure_text_column(df: pd.DataFrame) -> pd.DataFrame:\n",
" if \"text\" in df.columns:\n",
" return df\n",
" for c in [\"comment_text\", \"sentence\", \"content\", \"review\"]:\n",
" if c in df.columns:\n",
" return df.rename(columns={c: \"text\"})\n",
" raise ValueError(\"No text column found. Add/rename your text column to 'text'.\")\n",
"\n",
"def tune_thresholds(y_true: np.ndarray, y_prob: np.ndarray) -> np.ndarray:\n",
" th = np.zeros(y_true.shape[1], dtype=np.float32)\n",
" for j in range(y_true.shape[1]):\n",
" best_t, best_f1 = 0.5, -1\n",
" for t in np.linspace(0.1, 0.9, 17):\n",
" f1 = f1_score(y_true[:, j], (y_prob[:, j] >= t).astype(int), zero_division=0)\n",
" if f1 > best_f1:\n",
" best_f1, best_t = f1, t\n",
" th[j] = best_t\n",
" return th\n",
"\n",
"def get_optimizer_params(model, lr, weight_decay):\n",
" param_optimizer = list(model.named_parameters())\n",
" no_decay = [\"bias\", \"LayerNorm.bias\", \"LayerNorm.weight\"]\n",
" optimizer_parameters = [\n",
" {\n",
" \"params\": [p for n, p in param_optimizer if not any(nd in n for nd in no_decay)],\n",
" \"weight_decay\": weight_decay,\n",
" },\n",
" {\n",
" \"params\": [p for n, p in param_optimizer if any(nd in n for nd in no_decay)],\n",
" \"weight_decay\": 0.0,\n",
" },\n",
" ]\n",
" return optimizer_parameters"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 5. Dataset Class\n",
"\n",
"Custom PyTorch Dataset for emotion classification.\n",
"\n",
"**Key Features:**\n",
"- Tokenizes text on-the-fly using the DeBERTa tokenizer\n",
"- Handles both training data (with labels) and test data (without labels)\n",
"- Uses `padding='max_length'` to ensure all sequences have the same length (required for batching)\n",
"- Applies `truncation=True` to handle texts longer than MAX_LEN\n",
"\n",
"**Returns:**\n",
"- `input_ids`: Token IDs representing the text\n",
"- `attention_mask`: Indicates which tokens are real vs padding\n",
"- `labels`: Multi-label binary targets (only for training data)\n",
"\n",
"**PyTorch DataLoader** will use this dataset to create batches efficiently with multi-processing."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class EmotionDS(torch.utils.data.Dataset):\n",
" def __init__(self, df, tokenizer, max_len, is_test=False):\n",
" self.texts = df[\"text\"].tolist()\n",
" self.is_test = is_test\n",
" if not is_test:\n",
" self.labels = df[CONFIG.LABELS].values.astype(np.float32)\n",
" self.tok = tokenizer\n",
" self.max_len = max_len\n",
"\n",
" def __len__(self):\n",
" return len(self.texts)\n",
"\n",
" def __getitem__(self, i):\n",
" enc = self.tok(\n",
" self.texts[i],\n",
" truncation=True,\n",
" padding=\"max_length\",\n",
" max_length=self.max_len,\n",
" return_tensors=\"pt\",\n",
" )\n",
" item = {k: v.squeeze(0) for k, v in enc.items()}\n",
" if not self.is_test:\n",
" item[\"labels\"] = torch.tensor(self.labels[i])\n",
" return item"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 6. Training & Validation Helper Functions\n",
"\n",
"Core training and validation loops.\n",
"\n",
"### `train_one_epoch()`\n",
"Trains the model for one complete pass through the training data.\n",
"\n",
"**Key Techniques:**\n",
"- **Mixed Precision Training** (`autocast`): Uses float16 where safe, reducing memory and increasing speed by ~2x\n",
"- **Gradient Scaling** (`GradScaler`): Prevents gradient underflow in mixed precision\n",
"- **Gradient Clipping** (max_norm=1.0): Prevents exploding gradients, stabilizes training\n",
"- **Memory Efficient**: Uses `zero_grad(set_to_none=True)` and `non_blocking=True` for async GPU transfers\n",
"\n",
"### `validate()`\n",
"Evaluates the model on validation data without updating weights.\n",
"\n",
"**Features:**\n",
"- Runs in `model.eval()` mode (disables dropout, fixes batch normalization)\n",
"- Uses `torch.no_grad()` to save memory (no gradient computation)\n",
"- Applies sigmoid to convert logits to probabilities [0, 1]\n",
"- Returns predictions and targets for metric calculation"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def train_one_epoch(model, loader, optimizer, scheduler, scaler, criterion):\n",
" model.train()\n",
" losses = []\n",
" for batch in loader:\n",
" batch = {k: v.to(device, non_blocking=True) for k, v in batch.items()}\n",
" optimizer.zero_grad(set_to_none=True)\n",
" with autocast(enabled=True):\n",
" out = model(input_ids=batch[\"input_ids\"], attention_mask=batch[\"attention_mask\"])\n",
" loss = criterion(out.logits, batch[\"labels\"])\n",
" \n",
" scaler.scale(loss).backward()\n",
" scaler.unscale_(optimizer)\n",
" torch.nn.utils.clip_grad_norm_(model.parameters(), 1.0)\n",
" scaler.step(optimizer)\n",
" scaler.update()\n",
" scheduler.step()\n",
" losses.append(loss.item())\n",
" return np.mean(losses)\n",
"\n",
"def validate(model, loader, criterion):\n",
" model.eval()\n",
" losses = []\n",
" preds = []\n",
" targs = []\n",
" with torch.no_grad():\n",
" for batch in loader:\n",
" batch = {k: v.to(device, non_blocking=True) for k, v in batch.items()}\n",
" with autocast(enabled=True):\n",
" out = model(input_ids=batch[\"input_ids\"], attention_mask=batch[\"attention_mask\"])\n",
" loss = criterion(out.logits, batch[\"labels\"])\n",
" losses.append(loss.item())\n",
" preds.append(torch.sigmoid(out.logits).float().cpu().numpy())\n",
" targs.append(batch[\"labels\"].cpu().numpy())\n",
" \n",
" return np.mean(losses), np.vstack(preds), np.vstack(targs)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 7. Main K-Fold Training Loop\n",
"\n",
"The heart of our training pipeline - implements **5-Fold Stratified Cross-Validation**.\n",
"\n",
"### Why K-Fold Cross-Validation?\n",
"- More reliable performance estimates than a single train/val split\n",
"- Every sample is used for validation exactly once\n",
"- Out-of-fold predictions can be used for threshold optimization\n",
"- Reduces variance in model performance\n",
"\n",
"### Why Stratified?\n",
"- Maintains label distribution in each fold\n",
"- Important for imbalanced datasets\n",
"- For multi-label, we concatenate all labels into a string for stratification\n",
"\n",
"### Training Process per Fold:\n",
"1. **Split data**: 80% training, 20% validation\n",
"2. **Initialize model**: Fresh DeBERTa-v3-base with random classification head\n",
"3. **Setup optimizer**: AdamW with differential weight decay\n",
"4. **Setup scheduler**: Linear warmup (10% of steps) then linear decay to 0\n",
"5. **Train for 4 epochs**: Track training and validation metrics\n",
"6. **Save best model**: Based on validation F1 score\n",
"7. **Store OOF predictions**: For threshold tuning\n",
"8. **Clean up memory**: Delete model and optimizer, run garbage collection\n",
"\n",
"### Output:\n",
"- 5 trained models (one per fold) saved as `model_fold_{0-4}.pth`\n",
"- Out-of-fold predictions for the entire training set\n",
"- Cross-validated performance metrics"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def run_training():\n",
" if not os.path.exists(CONFIG.TRAIN_CSV):\n",
" print(\"Train CSV not found. Please check the path.\")\n",
" return None, None\n",
"\n",
" df = pd.read_csv(CONFIG.TRAIN_CSV)\n",
" df = ensure_text_column(df)\n",
" \n",
" # Create Stratified Folds\n",
" skf = StratifiedKFold(n_splits=CONFIG.N_FOLDS, shuffle=True, random_state=CONFIG.SEED)\n",
" y_str = df[CONFIG.LABELS].astype(str).agg(\"\".join, axis=1)\n",
" \n",
" oof_preds = np.zeros((len(df), len(CONFIG.LABELS)))\n",
" \n",
" tokenizer = AutoTokenizer.from_pretrained(CONFIG.MODEL_NAME)\n",
" \n",
" for fold, (train_idx, val_idx) in enumerate(skf.split(df, y_str)):\n",
" print(f\"\\n{'='*20} FOLD {fold+1}/{CONFIG.N_FOLDS} {'='*20}\")\n",
" \n",
" df_tr = df.iloc[train_idx].reset_index(drop=True)\n",
" df_va = df.iloc[val_idx].reset_index(drop=True)\n",
" \n",
" ds_tr = EmotionDS(df_tr, tokenizer, CONFIG.MAX_LEN)\n",
" ds_va = EmotionDS(df_va, tokenizer, CONFIG.MAX_LEN)\n",
" \n",
" dl_tr = torch.utils.data.DataLoader(ds_tr, batch_size=CONFIG.BATCH_SIZE, shuffle=True, num_workers=2, pin_memory=True)\n",
" dl_va = torch.utils.data.DataLoader(ds_va, batch_size=CONFIG.BATCH_SIZE, shuffle=False, num_workers=2, pin_memory=True)\n",
" \n",
" model = AutoModelForSequenceClassification.from_pretrained(\n",
" CONFIG.MODEL_NAME, \n",
" num_labels=len(CONFIG.LABELS),\n",
" problem_type=\"multi_label_classification\"\n",
" )\n",
" model.to(device)\n",
" \n",
" optimizer_params = get_optimizer_params(model, CONFIG.LR, CONFIG.WEIGHT_DECAY)\n",
" optimizer = AdamW(optimizer_params, lr=CONFIG.LR)\n",
" \n",
" total_steps = len(dl_tr) * CONFIG.EPOCHS\n",
" scheduler = get_linear_schedule_with_warmup(\n",
" optimizer, \n",
" num_warmup_steps=int(total_steps * CONFIG.WARMUP_RATIO), \n",
" num_training_steps=total_steps\n",
" )\n",
" \n",
" criterion = nn.BCEWithLogitsLoss()\n",
" scaler = GradScaler(enabled=True)\n",
" \n",
" best_f1 = 0\n",
" best_state = None\n",
" \n",
" for ep in range(CONFIG.EPOCHS):\n",
" train_loss = train_one_epoch(model, dl_tr, optimizer, scheduler, scaler, criterion)\n",
" val_loss, val_preds, val_targs = validate(model, dl_va, criterion)\n",
" \n",
" val_f1 = f1_score(val_targs, (val_preds >= 0.5).astype(int), average=\"macro\", zero_division=0)\n",
" \n",
" print(f\"Ep {ep+1}: TrLoss={train_loss:.4f} | VaLoss={val_loss:.4f} | VaF1={val_f1:.4f}\")\n",
" \n",
" if val_f1 > best_f1:\n",
" best_f1 = val_f1\n",
" best_state = model.state_dict()\n",
" \n",
" torch.save(best_state, f\"model_fold_{fold}.pth\")\n",
" \n",
" model.load_state_dict(best_state)\n",
" _, val_preds, _ = validate(model, dl_va, criterion)\n",
" oof_preds[val_idx] = val_preds\n",
" \n",
" del model, optimizer, scaler, scheduler\n",
" torch.cuda.empty_cache()\n",
" gc.collect()\n",
" \n",
" return oof_preds, df[CONFIG.LABELS].values\n",
"\n",
"if os.path.exists(CONFIG.TRAIN_CSV):\n",
" oof_preds, y_true = run_training()\n",
"else:\n",
" print(\"Skipping training as data is not found (likely in a dry-run environment).\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 8. Threshold Optimization\n",
"\n",
"**Why optimize thresholds?**\n",
"\n",
"In multi-label classification, we convert probabilities to binary predictions using a threshold:\n",
"- `prediction = 1 if probability >= threshold else 0`\n",
"- The default threshold of 0.5 is often suboptimal\n",
"- Different emotion labels may have different optimal thresholds\n",
"\n",
"**Example:**\n",
"- 'Joy' might be common → optimal threshold could be 0.4\n",
"- 'Surprise' might be rare → optimal threshold could be 0.6\n",
"\n",
"### Process:\n",
"1. Use out-of-fold predictions (already trained models, no data leakage)\n",
"2. For each label independently, test thresholds from 0.1 to 0.9\n",
"3. Select threshold that maximizes F1 score for that label\n",
"4. Apply optimized thresholds to get final binary predictions\n",
"\n",
"**Expected Improvement**: 2-5% increase in Macro F1 score\n",
"\n",
"This is a standard technique in Kaggle competitions and production systems."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"if os.path.exists(CONFIG.TRAIN_CSV):\n",
" best_thresholds = tune_thresholds(y_true, oof_preds)\n",
" oof_tuned = (oof_preds >= best_thresholds).astype(int)\n",
" final_f1 = f1_score(y_true, oof_tuned, average=\"macro\", zero_division=0)\n",
" print(f\"\\nFinal CV Macro F1: {final_f1:.4f}\")\n",
" print(f\"Best Thresholds: {best_thresholds}\")\n",
"else:\n",
" best_thresholds = np.array([0.5] * len(CONFIG.LABELS))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 9. Inference & Submission\n",
"\n",
"Final prediction pipeline for test data.\n",
"\n",
"### Ensemble Strategy:\n",
"We use **model averaging** across all 5 folds:\n",
"1. Load each trained fold model\n",
"2. Make predictions on test set\n",
"3. Average the probabilities across all folds\n",
"4. Apply optimized thresholds to get binary predictions\n",
"\n",
"### Why ensemble?\n",
"- Reduces variance and overfitting\n",
"- More robust predictions\n",
"- Often improves score by 1-3%\n",
"- Each fold sees different training data, captures different patterns\n",
"\n",
"### Process:\n",
"1. Load test data and tokenize\n",
"2. For each fold:\n",
" - Load saved model weights\n",
" - Generate predictions (probabilities)\n",
" - Clean up memory\n",
"3. Average all fold predictions\n",
"4. Apply optimized thresholds\n",
"5. Create submission file with format: `id, anger, fear, joy, sadness, surprise`\n",
"\n",
"### Output:\n",
"- `submission.csv` ready for Kaggle upload\n",
"- Binary predictions (0 or 1) for each emotion per text"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def predict_test(thresholds):\n",
" if not os.path.exists(CONFIG.TEST_CSV):\n",
" print(\"Test CSV not found.\")\n",
" return\n",
"\n",
" df_test = pd.read_csv(CONFIG.TEST_CSV)\n",
" df_test = ensure_text_column(df_test)\n",
" \n",
" tokenizer = AutoTokenizer.from_pretrained(CONFIG.MODEL_NAME)\n",
" ds_test = EmotionDS(df_test, tokenizer, CONFIG.MAX_LEN, is_test=True)\n",
" dl_test = torch.utils.data.DataLoader(ds_test, batch_size=CONFIG.BATCH_SIZE, shuffle=False, num_workers=2)\n",
" \n",
" fold_preds = []\n",
" \n",
" for fold in range(CONFIG.N_FOLDS):\n",
" model_path = f\"model_fold_{fold}.pth\"\n",
" if not os.path.exists(model_path):\n",
" print(f\"Model for fold {fold} not found, skipping.\")\n",
" continue\n",
" \n",
" print(f\"Predicting Fold {fold+1}...\")\n",
" model = AutoModelForSequenceClassification.from_pretrained(\n",
" CONFIG.MODEL_NAME, \n",
" num_labels=len(CONFIG.LABELS),\n",
" problem_type=\"multi_label_classification\"\n",
" )\n",
" model.load_state_dict(torch.load(model_path))\n",
" model.to(device)\n",
" model.eval()\n",
" \n",
" preds = []\n",
" with torch.no_grad():\n",
" for batch in dl_test:\n",
" batch = {k: v.to(device, non_blocking=True) for k, v in batch.items()}\n",
" with autocast(enabled=True):\n",
" out = model(input_ids=batch[\"input_ids\"], attention_mask=batch[\"attention_mask\"])\n",
" preds.append(torch.sigmoid(out.logits).float().cpu().numpy())\n",
" \n",
" fold_preds.append(np.vstack(preds))\n",
" del model\n",
" torch.cuda.empty_cache()\n",
" gc.collect()\n",
" \n",
" if not fold_preds:\n",
" print(\"No predictions made.\")\n",
" return\n",
"\n",
" avg_preds = np.mean(fold_preds, axis=0)\n",
" final_preds = (avg_preds >= thresholds).astype(int)\n",
" \n",
" sub = pd.DataFrame(columns=[\"id\"] + CONFIG.LABELS)\n",
" sub[\"id\"] = df_test[\"id\"] if \"id\" in df_test.columns else np.arange(len(df_test))\n",
" sub[CONFIG.LABELS] = final_preds\n",
" sub.to_csv(CONFIG.SUBMISSION_PATH, index=False)\n",
" print(f\"Submission saved to {CONFIG.SUBMISSION_PATH}\")\n",
" print(sub.head())\n",
"\n",
"predict_test(best_thresholds)"
]
}
],
"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.13.7"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|