Text Classification
sentence-transformers
Safetensors
English
bert
cross-encoder
reranker
retrieval
sentence-similarity
Eval Results (legacy)
text-embeddings-inference
Instructions to use matulichpt/radlit-crossencoder with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- sentence-transformers
How to use matulichpt/radlit-crossencoder with sentence-transformers:
from sentence_transformers import CrossEncoder model = CrossEncoder("matulichpt/radlit-crossencoder") query = "Which planet is known as the Red Planet?" passages = [ "Venus is often called Earth's twin because of its similar size and proximity.", "Mars, known for its reddish appearance, is often referred to as the Red Planet.", "Jupiter, the largest planet in our solar system, has a prominent red spot.", "Saturn, famous for its rings, is sometimes mistaken for the Red Planet." ] scores = model.predict([(query, passage) for passage in passages]) print(scores) - Notebooks
- Google Colab
- Kaggle
Update README.md
Browse files
README.md
CHANGED
|
@@ -1,393 +1,387 @@
|
|
| 1 |
-
---
|
| 2 |
-
license: apache-2.0
|
| 3 |
-
language:
|
| 4 |
-
- en
|
| 5 |
-
tags:
|
| 6 |
-
- cross-encoder
|
| 7 |
-
- reranker
|
| 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 |
-
device=device
|
| 169 |
-
)
|
| 170 |
-
|
| 171 |
-
self.
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
self.
|
| 177 |
-
self.corpus_embeddings =
|
| 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 |
-
v
|
| 299 |
-
[
|
| 300 |
-
|
|
| 301 |
-
v
|
| 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 |
-
- [RadLITE-Encoder](https://huggingface.co/matulichpt/radlit-biencoder) - Bi-encoder for first-stage retrieval
|
| 389 |
-
- [RadBERT-RoBERTa-4m](https://huggingface.co/zzxslp/RadBERT-RoBERTa-4m) - Base radiology language model
|
| 390 |
-
|
| 391 |
-
## License
|
| 392 |
-
|
| 393 |
-
Apache 2.0 - Free for commercial and research use.
|
|
|
|
| 1 |
+
---
|
| 2 |
+
license: apache-2.0
|
| 3 |
+
language:
|
| 4 |
+
- en
|
| 5 |
+
tags:
|
| 6 |
+
- cross-encoder
|
| 7 |
+
- reranker
|
| 8 |
+
- retrieval
|
| 9 |
+
- sentence-similarity
|
| 10 |
+
base_model: cross-encoder/ms-marco-MiniLM-L-12-v2
|
| 11 |
+
pipeline_tag: text-classification
|
| 12 |
+
library_name: sentence-transformers
|
| 13 |
+
metrics:
|
| 14 |
+
- mrr
|
| 15 |
+
- ndcg
|
| 16 |
+
model-index:
|
| 17 |
+
- name: RadLITE-Reranker
|
| 18 |
+
results:
|
| 19 |
+
- task:
|
| 20 |
+
type: reranking
|
| 21 |
+
name: Document Reranking
|
| 22 |
+
dataset:
|
| 23 |
+
name: RadLIT-9 (Radiology Retrieval Benchmark)
|
| 24 |
+
type: radiology-retrieval
|
| 25 |
+
metrics:
|
| 26 |
+
- type: mrr
|
| 27 |
+
value: 0.829
|
| 28 |
+
name: MRR (with bi-encoder)
|
| 29 |
+
- type: mrr
|
| 30 |
+
value: 0.533
|
| 31 |
+
name: MRR on ABR Core Exam (Chest)
|
| 32 |
+
---
|
| 33 |
+
|
| 34 |
+
# RadLITE-Reranker
|
| 35 |
+
|
| 36 |
+
**Radiology Late Interaction Transformer Enhanced - Cross-Encoder Reranker**
|
| 37 |
+
|
| 38 |
+
A domain-specialized cross-encoder for reranking radiology search results. This model takes a query-document pair and predicts a relevance score, providing more accurate ranking than bi-encoder similarity alone.
|
| 39 |
+
|
| 40 |
+
> **Recommended:** Use this reranker together with [RadLITE-Encoder](https://huggingface.co/matulichpt/radlit-biencoder) in a two-stage pipeline for optimal performance. The bi-encoder handles fast retrieval over large corpora, then this cross-encoder reranks the top candidates for precision. This combination achieves **MRR 0.829** on radiology retrieval benchmarks.
|
| 41 |
+
|
| 42 |
+
## Model Description
|
| 43 |
+
|
| 44 |
+
| Property | Value |
|
| 45 |
+
|----------|-------|
|
| 46 |
+
| **Model Type** | Cross-Encoder (Reranker) |
|
| 47 |
+
| **Base Model** | [ms-marco-MiniLM-L-12-v2](https://huggingface.co/cross-encoder/ms-marco-MiniLM-L-12-v2) |
|
| 48 |
+
| **Domain** | Radiology / Medical Imaging |
|
| 49 |
+
| **Hidden Size** | 384 |
|
| 50 |
+
| **Max Sequence Length** | 512 tokens |
|
| 51 |
+
| **Output** | Single relevance score |
|
| 52 |
+
| **License** | Apache 2.0 |
|
| 53 |
+
|
| 54 |
+
### Why Use a Reranker?
|
| 55 |
+
|
| 56 |
+
Bi-encoders (like RadLITE-Encoder) are fast but encode query and document independently. Cross-encoders process them together, capturing fine-grained interactions:
|
| 57 |
+
|
| 58 |
+
| Approach | Speed | Accuracy | Use Case |
|
| 59 |
+
|----------|-------|----------|----------|
|
| 60 |
+
| Bi-Encoder | Fast (1000s docs/sec) | Good | First-stage retrieval |
|
| 61 |
+
| Cross-Encoder | Slow (10s docs/sec) | Excellent | Reranking top candidates |
|
| 62 |
+
|
| 63 |
+
**Two-stage pipeline**: Use bi-encoder to get top 50-100 candidates, then rerank with cross-encoder for best results.
|
| 64 |
+
|
| 65 |
+
## Performance
|
| 66 |
+
|
| 67 |
+
### Impact on RadLIT-9 Benchmark
|
| 68 |
+
|
| 69 |
+
| Configuration | MRR | Improvement |
|
| 70 |
+
|---------------|-----|-------------|
|
| 71 |
+
| Bi-Encoder only | 0.78 | baseline |
|
| 72 |
+
| **Bi-Encoder + Reranker** | **0.829** | **+6.3%** |
|
| 73 |
+
|
| 74 |
+
### ABR Core Exam (Board-Style Questions)
|
| 75 |
+
|
| 76 |
+
Comparing two-stage pipeline (bi-encoder + reranker) vs bi-encoder alone:
|
| 77 |
+
|
| 78 |
+
| Dataset | Two-Stage MRR | Bi-Encoder Only | Improvement |
|
| 79 |
+
|---------|---------------|-----------------|-------------|
|
| 80 |
+
| Core Exam Chest | 0.533 | 0.409 | +30.3% |
|
| 81 |
+
| Core Exam Combined | 0.466 | 0.381 | +22.5% |
|
| 82 |
+
|
| 83 |
+
The reranker provides significant gains on complex, multi-part queries typical of board exam questions.
|
| 84 |
+
|
| 85 |
+
### Published Benchmark Results
|
| 86 |
+
|
| 87 |
+
From [Matulich & Mason, 2026](https://huggingface.co/matulichpt/radlit-biencoder):
|
| 88 |
+
|
| 89 |
+
| Benchmark | RadLIT Result | Key Finding |
|
| 90 |
+
|-----------|---------------|-------------|
|
| 91 |
+
| NFCorpus nDCG@10 | 0.268 | **17.9x improvement** over RadBERT bi-encoder (0.015) |
|
| 92 |
+
| VQA-RAD MRR | 0.972 | Near-perfect retrieval on radiology Q&A |
|
| 93 |
+
| RadLIT-9 Thoracic | 0.736 nDCG@10 | **Best-in-class** (beat BGE-large, ColBERTv2) |
|
| 94 |
+
| RadLIT-9 Pediatric | 0.625 nDCG@10 | **Best-in-class** (beat BGE-large, ColBERTv2) |
|
| 95 |
+
| Zebra Test | 92% found rate | 2.1x improvement on rare conditions vs ColBERTv2 |
|
| 96 |
+
|
| 97 |
+
**Vocabulary Alignment Hypothesis**: Domain training provides measurable advantage when queries use radiology-specific terminology that aligns with the training domain.
|
| 98 |
+
|
| 99 |
+
## Quick Start
|
| 100 |
+
|
| 101 |
+
### Installation
|
| 102 |
+
|
| 103 |
+
```bash
|
| 104 |
+
pip install sentence-transformers>=2.2.0
|
| 105 |
+
```
|
| 106 |
+
|
| 107 |
+
### Basic Usage
|
| 108 |
+
|
| 109 |
+
```python
|
| 110 |
+
from sentence_transformers import CrossEncoder
|
| 111 |
+
|
| 112 |
+
# Load the reranker
|
| 113 |
+
reranker = CrossEncoder("matulichpt/radlit-crossencoder", max_length=512)
|
| 114 |
+
|
| 115 |
+
# Query and candidate documents
|
| 116 |
+
query = "What are the imaging features of hepatocellular carcinoma?"
|
| 117 |
+
documents = [
|
| 118 |
+
"HCC typically shows arterial enhancement with portal venous washout on CT.",
|
| 119 |
+
"Fatty liver disease presents as decreased attenuation on non-contrast CT.",
|
| 120 |
+
"Hepatic hemangiomas show peripheral nodular enhancement.",
|
| 121 |
+
]
|
| 122 |
+
|
| 123 |
+
# Create query-document pairs
|
| 124 |
+
pairs = [[query, doc] for doc in documents]
|
| 125 |
+
|
| 126 |
+
# Get relevance scores
|
| 127 |
+
scores = reranker.predict(pairs)
|
| 128 |
+
|
| 129 |
+
# Apply temperature calibration (RECOMMENDED)
|
| 130 |
+
calibrated_scores = scores / 1.5
|
| 131 |
+
|
| 132 |
+
print("Scores:", calibrated_scores)
|
| 133 |
+
# Document about HCC will have highest score
|
| 134 |
+
```
|
| 135 |
+
|
| 136 |
+
### Temperature Calibration
|
| 137 |
+
|
| 138 |
+
**Important**: This model outputs scores with high variance. Apply temperature scaling for better fusion with other signals:
|
| 139 |
+
|
| 140 |
+
```python
|
| 141 |
+
# Raw scores might be: [4.2, -1.5, 0.8]
|
| 142 |
+
# After calibration: [2.8, -1.0, 0.53]
|
| 143 |
+
|
| 144 |
+
TEMPERATURE = 1.5 # Recommended value
|
| 145 |
+
|
| 146 |
+
def calibrated_predict(reranker, pairs):
|
| 147 |
+
raw_scores = reranker.predict(pairs)
|
| 148 |
+
return raw_scores / TEMPERATURE
|
| 149 |
+
```
|
| 150 |
+
|
| 151 |
+
### Full Two-Stage Search Pipeline
|
| 152 |
+
|
| 153 |
+
```python
|
| 154 |
+
from sentence_transformers import SentenceTransformer, CrossEncoder
|
| 155 |
+
import numpy as np
|
| 156 |
+
|
| 157 |
+
class RadLITESearch:
|
| 158 |
+
def __init__(self, device="cuda"):
|
| 159 |
+
# Stage 1: Fast bi-encoder
|
| 160 |
+
self.encoder = SentenceTransformer(
|
| 161 |
+
"matulichpt/radlit-biencoder",
|
| 162 |
+
device=device
|
| 163 |
+
)
|
| 164 |
+
# Stage 2: Precise reranker
|
| 165 |
+
self.reranker = CrossEncoder(
|
| 166 |
+
"matulichpt/radlit-crossencoder",
|
| 167 |
+
max_length=512,
|
| 168 |
+
device=device
|
| 169 |
+
)
|
| 170 |
+
self.temperature = 1.5
|
| 171 |
+
self.corpus_embeddings = None
|
| 172 |
+
self.corpus = None
|
| 173 |
+
|
| 174 |
+
def index_corpus(self, documents: list):
|
| 175 |
+
"""Pre-compute embeddings for your corpus."""
|
| 176 |
+
self.corpus = documents
|
| 177 |
+
self.corpus_embeddings = self.encoder.encode(
|
| 178 |
+
documents,
|
| 179 |
+
normalize_embeddings=True,
|
| 180 |
+
show_progress_bar=True,
|
| 181 |
+
batch_size=32
|
| 182 |
+
)
|
| 183 |
+
|
| 184 |
+
def search(self, query: str, top_k: int = 10, candidates: int = 50):
|
| 185 |
+
"""Two-stage search: retrieve then rerank."""
|
| 186 |
+
|
| 187 |
+
# Stage 1: Bi-encoder retrieval
|
| 188 |
+
query_emb = self.encoder.encode(query, normalize_embeddings=True)
|
| 189 |
+
scores = query_emb @ self.corpus_embeddings.T
|
| 190 |
+
top_indices = np.argsort(scores)[-candidates:][::-1]
|
| 191 |
+
|
| 192 |
+
# Stage 2: Cross-encoder reranking
|
| 193 |
+
candidate_docs = [self.corpus[i] for i in top_indices]
|
| 194 |
+
pairs = [[query, doc] for doc in candidate_docs]
|
| 195 |
+
rerank_scores = self.reranker.predict(pairs) / self.temperature
|
| 196 |
+
|
| 197 |
+
# Sort by reranked scores
|
| 198 |
+
sorted_indices = np.argsort(rerank_scores)[::-1]
|
| 199 |
+
|
| 200 |
+
results = []
|
| 201 |
+
for idx in sorted_indices[:top_k]:
|
| 202 |
+
results.append({
|
| 203 |
+
"document": candidate_docs[idx],
|
| 204 |
+
"corpus_index": int(top_indices[idx]),
|
| 205 |
+
"score": float(rerank_scores[idx]),
|
| 206 |
+
"biencoder_score": float(scores[top_indices[idx]])
|
| 207 |
+
})
|
| 208 |
+
return results
|
| 209 |
+
|
| 210 |
+
|
| 211 |
+
# Usage
|
| 212 |
+
searcher = RadLITESearch()
|
| 213 |
+
searcher.index_corpus(your_radiology_documents)
|
| 214 |
+
results = searcher.search("pneumothorax CT findings")
|
| 215 |
+
```
|
| 216 |
+
|
| 217 |
+
## Integration with Any Corpus
|
| 218 |
+
|
| 219 |
+
### Radiopaedia / Educational Content
|
| 220 |
+
|
| 221 |
+
```python
|
| 222 |
+
import json
|
| 223 |
+
|
| 224 |
+
# Load your content (e.g., Radiopaedia articles)
|
| 225 |
+
with open("radiopaedia_articles.json") as f:
|
| 226 |
+
articles = json.load(f)
|
| 227 |
+
|
| 228 |
+
corpus = [article["content"] for article in articles]
|
| 229 |
+
|
| 230 |
+
# Initialize search
|
| 231 |
+
searcher = RadLITESearch()
|
| 232 |
+
searcher.index_corpus(corpus)
|
| 233 |
+
|
| 234 |
+
# Search
|
| 235 |
+
results = searcher.search("classic findings of pulmonary embolism on CTPA")
|
| 236 |
+
|
| 237 |
+
for r in results[:5]:
|
| 238 |
+
print(f"Score: {r['score']:.3f}")
|
| 239 |
+
print(f"Content: {r['document'][:200]}...")
|
| 240 |
+
print()
|
| 241 |
+
```
|
| 242 |
+
|
| 243 |
+
### Integration with Elasticsearch/OpenSearch
|
| 244 |
+
|
| 245 |
+
```python
|
| 246 |
+
from sentence_transformers import CrossEncoder
|
| 247 |
+
|
| 248 |
+
reranker = CrossEncoder("matulichpt/radlit-crossencoder", max_length=512)
|
| 249 |
+
|
| 250 |
+
def rerank_elasticsearch_results(query: str, es_results: list, top_k: int = 10):
|
| 251 |
+
"""Rerank Elasticsearch BM25 results."""
|
| 252 |
+
documents = [hit["_source"]["content"] for hit in es_results]
|
| 253 |
+
pairs = [[query, doc] for doc in documents]
|
| 254 |
+
|
| 255 |
+
scores = reranker.predict(pairs) / 1.5 # Temperature calibration
|
| 256 |
+
|
| 257 |
+
# Combine with ES scores (optional)
|
| 258 |
+
for i, hit in enumerate(es_results):
|
| 259 |
+
hit["rerank_score"] = float(scores[i])
|
| 260 |
+
hit["combined_score"] = 0.3 * hit["_score"] + 0.7 * scores[i]
|
| 261 |
+
|
| 262 |
+
# Sort by combined score
|
| 263 |
+
reranked = sorted(es_results, key=lambda x: x["combined_score"], reverse=True)
|
| 264 |
+
return reranked[:top_k]
|
| 265 |
+
```
|
| 266 |
+
|
| 267 |
+
## Optimal Fusion Weights
|
| 268 |
+
|
| 269 |
+
When combining multiple signals (bi-encoder, cross-encoder, BM25), use these weights:
|
| 270 |
+
|
| 271 |
+
```python
|
| 272 |
+
# Optimal weights from grid search on RadLIT-9
|
| 273 |
+
FUSION_WEIGHTS = {
|
| 274 |
+
"biencoder": 0.5, # RadLITE-Encoder similarity
|
| 275 |
+
"crossencoder": 0.2, # RadLITE-Reranker (after temp calibration)
|
| 276 |
+
"bm25": 0.3 # Lexical matching (if available)
|
| 277 |
+
}
|
| 278 |
+
|
| 279 |
+
def fused_score(bienc_score, ce_score, bm25_score=0):
|
| 280 |
+
return (
|
| 281 |
+
FUSION_WEIGHTS["biencoder"] * bienc_score +
|
| 282 |
+
FUSION_WEIGHTS["crossencoder"] * ce_score +
|
| 283 |
+
FUSION_WEIGHTS["bm25"] * bm25_score
|
| 284 |
+
)
|
| 285 |
+
```
|
| 286 |
+
|
| 287 |
+
## Architecture
|
| 288 |
+
|
| 289 |
+
```
|
| 290 |
+
[Query] + [SEP] + [Document]
|
| 291 |
+
|
|
| 292 |
+
v
|
| 293 |
+
[BERT Tokenizer]
|
| 294 |
+
|
|
| 295 |
+
v
|
| 296 |
+
[MiniLM Encoder] (12 layers, 384 hidden)
|
| 297 |
+
|
|
| 298 |
+
v
|
| 299 |
+
[Classification Head]
|
| 300 |
+
|
|
| 301 |
+
v
|
| 302 |
+
Relevance Score (float)
|
| 303 |
+
```
|
| 304 |
+
|
| 305 |
+
## Training Details
|
| 306 |
+
|
| 307 |
+
- **Base Model**: ms-marco-MiniLM-L-12-v2 (trained on MS MARCO passage ranking)
|
| 308 |
+
- **Fine-tuning**: Radiology query-document relevance pairs
|
| 309 |
+
- **Training Steps**: 5,626
|
| 310 |
+
- **Best Validation Loss**: 0.691
|
| 311 |
+
- **Learning Rate**: 2e-5
|
| 312 |
+
- **Batch Size**: 32
|
| 313 |
+
- **Category Weighting**: Yes (balanced across radiology subspecialties)
|
| 314 |
+
|
| 315 |
+
## Best Practices
|
| 316 |
+
|
| 317 |
+
### 1. Always Use Temperature Calibration
|
| 318 |
+
|
| 319 |
+
Raw cross-encoder scores can be extreme. Temperature scaling (1.5) produces better fusion:
|
| 320 |
+
|
| 321 |
+
```python
|
| 322 |
+
calibrated = raw_score / 1.5
|
| 323 |
+
```
|
| 324 |
+
|
| 325 |
+
### 2. Limit Candidates for Reranking
|
| 326 |
+
|
| 327 |
+
Cross-encoders are slow. Only rerank top 50-100 candidates from bi-encoder:
|
| 328 |
+
|
| 329 |
+
```python
|
| 330 |
+
# Good: Rerank top 50
|
| 331 |
+
rerank_candidates = 50
|
| 332 |
+
|
| 333 |
+
# Bad: Rerank entire corpus
|
| 334 |
+
rerank_candidates = len(corpus) # Too slow!
|
| 335 |
+
```
|
| 336 |
+
|
| 337 |
+
### 3. Batch Predictions
|
| 338 |
+
|
| 339 |
+
```python
|
| 340 |
+
# Efficient: Single batch call
|
| 341 |
+
pairs = [[query, doc] for doc in candidates]
|
| 342 |
+
scores = reranker.predict(pairs, batch_size=32)
|
| 343 |
+
|
| 344 |
+
# Inefficient: Individual calls
|
| 345 |
+
scores = [reranker.predict([[query, doc]])[0] for doc in candidates]
|
| 346 |
+
```
|
| 347 |
+
|
| 348 |
+
### 4. GPU Acceleration
|
| 349 |
+
|
| 350 |
+
```python
|
| 351 |
+
reranker = CrossEncoder(
|
| 352 |
+
"matulichpt/radlit-crossencoder",
|
| 353 |
+
max_length=512,
|
| 354 |
+
device="cuda" # Use GPU
|
| 355 |
+
)
|
| 356 |
+
```
|
| 357 |
+
|
| 358 |
+
## Limitations
|
| 359 |
+
|
| 360 |
+
- **English only**: Trained on English radiology text
|
| 361 |
+
- **Speed**: ~10-50 pairs/second (use for reranking, not full corpus)
|
| 362 |
+
- **512 token limit**: Long documents are truncated
|
| 363 |
+
- **Domain-specific**: Optimized for radiology, may underperform on general medical content
|
| 364 |
+
|
| 365 |
+
## Citation
|
| 366 |
+
|
| 367 |
+
If you use RadLITE in your work, please cite:
|
| 368 |
+
|
| 369 |
+
```bibtex
|
| 370 |
+
@article{matulich2026radlit,
|
| 371 |
+
title = {Late Interaction Retrieval Unlocks Domain Knowledge in Radiology Language Models},
|
| 372 |
+
author = {Matulich, Patrick and Mason, Dan},
|
| 373 |
+
year = {2026},
|
| 374 |
+
journal = {Radiology: Artificial Intelligence},
|
| 375 |
+
note = {17.9x improvement over RadBERT; best-in-class on Thoracic/Pediatric subspecialties},
|
| 376 |
+
url = {https://huggingface.co/matulichpt/radlit-biencoder}
|
| 377 |
+
}
|
| 378 |
+
```
|
| 379 |
+
|
| 380 |
+
## Related Models
|
| 381 |
+
|
| 382 |
+
- [RadLITE-Encoder](https://huggingface.co/matulichpt/radlit-biencoder) - Bi-encoder for first-stage retrieval
|
| 383 |
+
- [RadBERT-RoBERTa-4m](https://huggingface.co/zzxslp/RadBERT-RoBERTa-4m) - Base radiology language model
|
| 384 |
+
|
| 385 |
+
## License
|
| 386 |
+
|
| 387 |
+
Apache 2.0 - Free for commercial and research use.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|