Text Classification
Transformers
Safetensors
deberta
deberta-v3
multiple-choice
question-answering
awp
Instructions to use a-01a/QSolver_Encoder_V2 with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use a-01a/QSolver_Encoder_V2 with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-classification", model="a-01a/QSolver_Encoder_V2")# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("a-01a/QSolver_Encoder_V2", device_map="auto") - Notebooks
- Google Colab
- Kaggle
Create README.md
Browse files
README.md
ADDED
|
@@ -0,0 +1,161 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: mit
|
| 3 |
+
base_model: microsoft/deberta-v3-large
|
| 4 |
+
library_name: transformers
|
| 5 |
+
pipeline_tag: text-classification
|
| 6 |
+
tags:
|
| 7 |
+
- deberta
|
| 8 |
+
- deberta-v3
|
| 9 |
+
- multiple-choice
|
| 10 |
+
- question-answering
|
| 11 |
+
- awp
|
| 12 |
+
metrics:
|
| 13 |
+
- map@3
|
| 14 |
+
---
|
| 15 |
+
|
| 16 |
+
# QSolver_Encoder_V2
|
| 17 |
+
|
| 18 |
+
QSolver_Encoder_V2 is an encoder-based language model fine-tuned for 5-choice scientific multiple-choice question answering. Built on top of `microsoft/deberta-v3-large`, the model utilizes partial layer freezing, dynamic 3D sequence collating, 8-bit AdamW optimization, and custom **Adversarial Weight Perturbation (AWP)** to prevent overfitting and improve generalization across distribution shifts.
|
| 19 |
+
|
| 20 |
+
Evaluation is performed using 5-Fold Stratified Cross-Validation monitored with Mean Average Precision at 3 (MAP@3).
|
| 21 |
+
|
| 22 |
+
---
|
| 23 |
+
|
| 24 |
+
## Model Details
|
| 25 |
+
|
| 26 |
+
- **Model Name:** QSolver_Encoder_V2
|
| 27 |
+
- **Repository ID:** `dahaludba/QSolver_Encoder_V2`
|
| 28 |
+
- **Base Model:** `microsoft/deberta-v3-large`
|
| 29 |
+
- **Architecture:** `AutoModelForMultipleChoice`
|
| 30 |
+
- **Maximum Sequence Length:** 512 tokens
|
| 31 |
+
- **Optimization Precision:** 32-bit Floating Point (FP32)
|
| 32 |
+
- **Optimizer:** 8-bit AdamW (`optim="adamw_8bit"` via `bitsandbytes`)
|
| 33 |
+
- **Total Training Duration:** 13 hours 5 minutes
|
| 34 |
+
- **License:** MIT License
|
| 35 |
+
|
| 36 |
+
---
|
| 37 |
+
|
| 38 |
+
## Training Strategy & Features
|
| 39 |
+
|
| 40 |
+
### 1. Partial Encoder Layer Freezing
|
| 41 |
+
To preserve low-level semantic representation and prevent catastrophic forgetting, the bottom 12 encoder layers (`deberta.encoder.layer.0` through `11`) out of 24 layers were completely frozen. Only the top 12 layers (`12` through `23`) and the multiple-choice classification head were fine-tuned.
|
| 42 |
+
|
| 43 |
+
### 2. Adversarial Weight Perturbation (AWP)
|
| 44 |
+
To increase regularization and combat overfitting on small datasets, training uses a custom `AWPTrainer`.
|
| 45 |
+
- **Activation:** Starts after completing epoch 1.0.
|
| 46 |
+
- **Target Layers:** Active `word_embeddings` parameters.
|
| 47 |
+
- **Step Size ($\alpha$):** `1e-3`
|
| 48 |
+
- **Epsilon Bound ($\epsilon$):** `1e-2` (limits weight perturbation magnitude to within 1% of original parameter weights).
|
| 49 |
+
|
| 50 |
+
During each backward pass, weight embeddings are perturbed in the direction of maximum loss increase, gradients are computed, and original clean weights are restored prior to executing the optimizer step.
|
| 51 |
+
|
| 52 |
+
### 3. Dynamic 3D Sequence Collating
|
| 53 |
+
Question-option pairs are tokenized into 5 choice paths per sample. A `CustomDataCollator` dynamically pads sequences within each batch and reshapes tensors into 3D shapes `(batch_size, num_choices, max_seq_len)` for compute-efficient batch passes through `AutoModelForMultipleChoice`.
|
| 54 |
+
|
| 55 |
+
---
|
| 56 |
+
|
| 57 |
+
## Hyperparameters
|
| 58 |
+
|
| 59 |
+
| Hyperparameter | Value |
|
| 60 |
+
| :--- | :--- |
|
| 61 |
+
| Peak Learning Rate | 8e-6 |
|
| 62 |
+
| LR Scheduler | Cosine Decay |
|
| 63 |
+
| Warmup Steps | 30 |
|
| 64 |
+
| Optimizer | `adamw_8bit` (bitsandbytes) |
|
| 65 |
+
| Weight Decay | 0.01 |
|
| 66 |
+
| Per Device Train Batch Size | 1 |
|
| 67 |
+
| Per Device Eval Batch Size | 1 |
|
| 68 |
+
| Gradient Accumulation Steps | 16 (Effective Batch Size = 16) |
|
| 69 |
+
| Training Epochs | 4 per fold |
|
| 70 |
+
| Floating Point Precision | Full FP32 (`fp16=False`) |
|
| 71 |
+
| Gradient Checkpointing | Enabled (`use_reentrant=False`, `use_cache=False`) |
|
| 72 |
+
| Frozen Layers | Encoder layers 0 to 11 |
|
| 73 |
+
| Metric Monitored | MAP@3 (`eval_map@3`) |
|
| 74 |
+
| Seed | 42 |
|
| 75 |
+
|
| 76 |
+
---
|
| 77 |
+
|
| 78 |
+
## Experiment Tracking & Cross-Validation Results
|
| 79 |
+
|
| 80 |
+
Total training across all 5 folds completed in **13 hours and 5 minutes**. Full run logs, learning rate curves, and loss trajectories are available via Weights & Biases:
|
| 81 |
+
|
| 82 |
+
- **Fold 1:** [W&B Run chh1vxqe](https://wandb.ai/24f2002963-dl-genai-project/24f2002963-t22026/runs/chh1vxqe?nw=nwuser24f2002963)
|
| 83 |
+
- **Fold 2:** [W&B Run eu537lgm](https://wandb.ai/24f2002963-dl-genai-project/24f2002963-t22026/runs/eu537lgm?nw=nwuser24f2002963)
|
| 84 |
+
- **Fold 3:** [W&B Run t3gjczof](https://wandb.ai/24f2002963-dl-genai-project/24f2002963-t22026/runs/t3gjczof?nw=nwuser24f2002963)
|
| 85 |
+
- **Fold 4:** [W&B Run qkukzynk](https://wandb.ai/24f2002963-dl-genai-project/24f2002963-t22026/runs/qkukzynk?nw=nwuser24f2002963)
|
| 86 |
+
- **Fold 5:** [W&B Run 92ksg28n](https://wandb.ai/24f2002963-dl-genai-project/24f2002963-t22026/runs/92ksg28n?nw=nwuser24f2002963)
|
| 87 |
+
|
| 88 |
+
---
|
| 89 |
+
|
| 90 |
+
## Evaluation Metric
|
| 91 |
+
|
| 92 |
+
Performance is measured using Mean Average Precision at 3 (MAP@3):
|
| 93 |
+
|
| 94 |
+
$$\text{MAP@3} = \frac{1}{U} \sum_{i=1}^{U} \sum_{k=1}^{\min(P, 3)} P(k) \times \text{rel}(k)$$
|
| 95 |
+
|
| 96 |
+
Where $P(k)$ is the precision at rank $k$, and $\text{rel}(k)$ indicates whether the option at rank $k$ is the ground-truth label.
|
| 97 |
+
|
| 98 |
+
---
|
| 99 |
+
|
| 100 |
+
## How to Load and Run Inference
|
| 101 |
+
|
| 102 |
+
```python
|
| 103 |
+
import torch
|
| 104 |
+
import numpy as np
|
| 105 |
+
import itertools
|
| 106 |
+
from transformers import AutoTokenizer, AutoModelForMultipleChoice
|
| 107 |
+
|
| 108 |
+
REPO_ID = "dahaludba/QSolver_Encoder_V2"
|
| 109 |
+
SUBFOLDER = "fold_1"
|
| 110 |
+
|
| 111 |
+
# Load Tokenizer & Model
|
| 112 |
+
tokenizer = AutoTokenizer.from_pretrained("microsoft/deberta-v3-large")
|
| 113 |
+
model = AutoModelForMultipleChoice.from_pretrained(REPO_ID, subfolder=SUBFOLDER)
|
| 114 |
+
model.eval()
|
| 115 |
+
|
| 116 |
+
# Sample Question and Options
|
| 117 |
+
question = "Which organelle is responsible for cellular respiration in eukaryotic cells?"
|
| 118 |
+
options = [
|
| 119 |
+
"Lysosome",
|
| 120 |
+
"Mitochondria",
|
| 121 |
+
"Chloroplast",
|
| 122 |
+
"Golgi Apparatus",
|
| 123 |
+
"Peroxisome"
|
| 124 |
+
]
|
| 125 |
+
|
| 126 |
+
# Pair Question with Options
|
| 127 |
+
first_sentences = [question] * 5
|
| 128 |
+
second_sentences = options
|
| 129 |
+
|
| 130 |
+
# Tokenize paired sequences
|
| 131 |
+
inputs = tokenizer(
|
| 132 |
+
first_sentences,
|
| 133 |
+
second_sentences,
|
| 134 |
+
truncation=True,
|
| 135 |
+
max_length=512,
|
| 136 |
+
padding=True,
|
| 137 |
+
return_tensors="pt"
|
| 138 |
+
)
|
| 139 |
+
|
| 140 |
+
# Reshape inputs to 3D tensors (batch_size=1, num_choices=5, seq_len)
|
| 141 |
+
batch_inputs = {
|
| 142 |
+
k: v.unsqueeze(0) for k, v in inputs.items()
|
| 143 |
+
}
|
| 144 |
+
|
| 145 |
+
with torch.no_grad():
|
| 146 |
+
outputs = model(**batch_inputs)
|
| 147 |
+
logits = outputs.logits.cpu().numpy()[0]
|
| 148 |
+
|
| 149 |
+
# Rank top choices
|
| 150 |
+
option_letters = ["A", "B", "C", "D", "E"]
|
| 151 |
+
top3_indices = np.argsort(-logits)[:3]
|
| 152 |
+
top3_predictions = [f"{option_letters[idx]}: {options[idx]}" for idx in top3_indices]
|
| 153 |
+
|
| 154 |
+
print("Top 3 Predictions:", top3_predictions)
|
| 155 |
+
```
|
| 156 |
+
|
| 157 |
+
---
|
| 158 |
+
|
| 159 |
+
## License
|
| 160 |
+
|
| 161 |
+
This model and all repository contents are distributed under the **MIT License**.
|