This repository contains the Qwen 3 Reranker implementation used in Step 4 of the automated food classification pipeline described in "A harmonised FoodEx2-to-FABIO dataset for country-specific, multi-indicator environmental assessment of food consumption".
The model evaluates a food or agricultural product descriptor against candidate ontology categories and assigns each candidate a probability of being a valid match.
The reranker is used as a cross-encoder. The product descriptor and candidate category are provided together to the model, which evaluates whether the category correctly describes the product.
The instruction used was:
INSTRUCTION = (
"Given a food or agricultural product term, "
"judge whether the category correctly describes it. "
"Answer only yes or no."
)The system prompt constrains the model output to yes or no.
Each candidate is assigned a score based on the model's probability of yes:
where yes and no tokens.
Candidates are then ranked according to
The query consists of the hierarchical food descriptor constructed from the ontology terms:
term4 belonging to term3 belonging to term2 belonging to term1
For example:
<product descriptor>
↓
Qwen 3 Reranker
↓
Candidate categories
↓
P(yes) for each category
↓
Ranked categories
The implementation evaluates candidates in batches to reduce memory requirements.
| Parameter | Value |
|---|---|
| Model | Qwen 3 Reranker |
| Task | Food/agricultural category matching |
| Output | Probability of yes
|
| Maximum sequence length | 512 tokens |
| Batch size | 32 |
| Ranking | Descending |
| Output | Best category + top-k candidates |
The implementation retains the top 5 candidate categories for subsequent analysis.
The core scoring operation is:
@torch.no_grad()
def compute_yes_scores(inputs):
logits = model(**inputs).logits[:, -1, :]
yes_logits = logits[:, token_true_id]
no_logits = logits[:, token_false_id]
scores = torch.stack([no_logits, yes_logits], dim=1)
scores = torch.nn.functional.log_softmax(scores, dim=1)
return scores[:, 1].exp().tolist()Candidates are subsequently ranked by their yes probability:
ranked = sorted(
scores.items(),
key=lambda x: x[1],
reverse=True
)The Qwen 3 Reranker does not generate standalone sentence embeddings in this implementation. It functions as a cross-encoder that jointly processes the descriptor and candidate category and produces a relevance score.
This allows it to consider interactions between the two texts that are not captured by independently generated embeddings, making it suitable for fine-grained reranking of semantically similar food and agricultural categories.