Encoder Training Recipes: Why Classification and Retrieval No Longer Have to Be a Trade-Off
Bidirectional encoders remain the cost-effective backbone for retrieval, data filtering, and reward scoring, yet they are widely assumed to force a choice between strong classification and strong retrieval. This article traces that trade-off not to the architecture but to the training recipes built around it: it explains why contrastive and cross-entropy objectives pull the embedding space in opposite directions, then walks through a unified recipe based on multi-stage training, explicit loss weighting, and deliberate pooling and normalization choices. It closes with dual-metric evaluation, anisotropy and collapse checks, and the deployment practices that let one encoder serve both capabilities at low latency and low cost.
1. The Underrated Encoder: Still Irreplaceable in the Age of Generative LLMs
When everyone is talking about generative models, it is easy to fall into a false assumption. If a ten-billion-parameter autoregressive model can answer questions, write prose, and reason, then handing it text for classification or retrieval must be overkill. In real engineering, the opposite is true.
Take the mainstream bidirectional encoder — a model that sees both the left and right context of a span and compresses the whole thing into a single vector, with masked language models such as BERT as the representative architecture. It differs fundamentally from a decoder that generates text token by token from left to right. The encoder is not in the business of producing new text; it is in the business of reading and compressing, mapping a sentence, a paragraph, or an entire document into a fixed-dimension dense vector that can then be used for similarity comparison, category assignment, or quality scoring.
This capability is close to mandatory in three high-frequency scenarios:
- Retrieval recall: providing first-stage ranking for RAG pipelines or search engines, shrinking a huge candidate pool down to a size that a heavier reranker can handle;
- Data quality filtering: batch-scoring cleanliness, domain relevance, or duplication before a large corpus enters training;
- Reward scoring: supplying a differentiable, scalable scoring signal for reinforcement learning or data synthesis loops.
What these scenarios share is enormous call volume, very tight latency budgets, and unit cost that has to be pushed as low as possible. A billion-parameter encoder usually beats spinning up a heavyweight generative model on every request across throughput, latency, and total cost, and on ranking or judging tasks its quality is not necessarily worse. In other words, encoders are not legacy technology — they are a badly underrated foundation for cost-effective performance.
2. Where the Trade-Off Comes From: The Classification-or-Retrieval Trap
That foundation, however, has long suffered from a strange phenomenon: models that are good at classification consistently score lower on retrieval, and models that are good at retrieval fall behind on classification.
It is like training a track athlete. A pure sprinter never builds marathon endurance no matter how hard they train, and a pure distance runner is always half a step slow in the sprint phase. The industry gradually accepted this as some kind of encoder destiny — as if the architecture itself made the two abilities mutually exclusive, so gaining one means sacrificing the other. A de facto either/or.
The result in practice is a lot of compromise: train one vector model for retrieval, train a separate discriminative model for classification, run two systems in parallel and maintain both, with inference paths that never meet. That is tolerable at small and medium scale. But the moment retrieval and classification need to share one feature space, or need to ship both capabilities at very low cost, that split turns into architectural debt fast.
The way out starts from one question: if the either/or were an architectural inevitability, no encoder would have a solution. But if a different training approach can light up both abilities at once, then the problem was never the architecture — it was how we train it.
3. The Real Root Cause: A Mismatched Training Recipe, Not the Architecture
The first step is to move attention from “which model should I swap in” to “how should I train it”. Nothing in the structure of a bidirectional encoder prevents it from being good at both tasks. What is genuinely outdated is the training recipe that grew up around it.
Traditional recipes usually take one of two forms:
- Single-objective pretraining plus single-task fine-tuning: general pretraining with masked language modeling, then fine-tuning for exactly one downstream objective — either retrieval or classification. The model is pulled by a single loss from start to finish, so of course it grows well in that one direction.
- Default pooling and normalization habits: almost reflexively taking the vector from some fixed position, while ignoring the fact that retrieval and classification impose different requirements on the geometry of that vector.
The essence of the problem is this: classification and retrieval both reduce to “one vector representing one piece of text”, but their geometric requirements on that vector are completely different. Trying to chase both with the wrong recipe inevitably sacrifices one for the other.
4. Recipe Breakdown: Why the Two Abilities Pull Against Each Other
Fixing the problem requires seeing what each ability actually wants inside the vector space.
Retrieval wants a semantic-neighborhood structure. Retrieval typically relies on contrastive learning with an InfoNCE-style objective: pull a query and its relevant documents closer in the vector space while pushing irrelevant samples apart. It is extremely sensitive to a few factors:
- Number of effective negatives: the more in-batch negatives you can supply, the better the discrimination, which is why retrieval training strongly prefers large batches;
- Temperature: it controls how soft or hard the similarity distribution is, and directly determines the separation of the embedding space;
- Hard negative mining: random negatives are too easy to tell apart — the real gains come from samples that look similar but are actually irrelevant;
- Anisotropy: raw language model embeddings tend to be squeezed into a narrow cone, so cosine similarity between arbitrary texts is uniformly high. Everything looks alike, and retrieval precision suffers badly.
Classification wants a category-decidable structure. Classification usually uses cross-entropy with a discriminative head on top of the pooled vector, pulling same-class samples together and pushing different-class samples apart. What matters is whether the decision boundary is clean, not whether the similarity between any two arbitrary samples monotonically reflects semantic relatedness.
Here is the contradiction: retrieval wants vectors spread as evenly as possible across the whole space to preserve fine-grained similarity, while classification wants same-class samples packed into one tight cluster. When both share the same parameters, the gradient directions often cancel each other out — that is the mathematical root of “a fast runner cannot build endurance”. On top of this, pooling strategy (a specific token position versus mean pooling), whether the vector is normalized, and whether whitening is applied all amplify or soften the conflict.
5. Putting the Unified Recipe into Practice: Multi-Stage, Weighted, Configurable
Once the cause is clear, the solution becomes straightforward: stop using one objective, one pooling scheme, and one stage to force the model to learn both things at once, and redesign the recipe into a process with stages and proportions. Three ideas carry the design.
First, train in multiple stages instead of hard-mixing in one. Start with retrieval-oriented contrastive learning to open up and align the embedding space — fixing anisotropy and establishing fine-grained similarity — then inject classification supervision on top of that, replaying retrieval data at the same time to prevent catastrophic forgetting of the retrieval ability already learned.
Second, weight the losses to explicitly manage the tug-of-war. Write the retrieval loss and the classification loss as a weighted sum, and treat the weights as the key tunable knob, calibrated repeatedly against validation results rather than fixed at whatever ratio “looks reasonable”.
Third, choose pooling and normalization per use case, and post-process. Retrieval favors comparable similarity, so it generally works better with mean pooling plus L2 normalization, and whitening where necessary; classification favors a decidable boundary and can attach a discriminative head directly to the pooled vector. Both can share one backbone with separate output heads.
The pseudocode below expresses this logic schematically. It describes the training flow only and is independent of any specific framework or library.
# Illustrative pseudocode: expresses recipe logic only, no framework dependency# ---------- Stage 1: retrieval-oriented contrastive learning, align the space first ----------for batch in retrieval_stream:q = encoder(batch.query)d = encoder(batch.doc)# InfoNCE-style: pull positives close, push in-batch negatives apart;# prefer large batches and hard negativeslogits = cosine_sim(q, d) / temperatureloss_ret = infonce_loss(logits, batch.positive_idx)step(loss_ret)# ---------- Stage 2: inject classification supervision, replay retrieval data to avoid forgetting ----------for batch in mixed_stream: # classification data + retrieval data mixedpooled = mean_pooling(encoder(batch.text))emb = l2_normalize(pooled)# Retrieval branch: keep fine-grained similarity alive so the ability does not degradeloss_ret = infonce_loss(build_logits(emb), batch.ret_label)# Classification branch: standard cross-entropy, discriminative head on the pooled vectorloss_cls = cross_entropy(classifier_head(pooled), batch.cls_label)# Explicit weighting to balance the two objectives (weights must be calibrated on validation)loss = w_ret * loss_ret + w_cls * loss_clsstep(loss)
What matters in this recipe is not any single line of magic code. It is turning training from a single-point assault into an engineering process that is staged, proportioned, and replayable.
6. Evaluation: Don’t Let One Metric Hide the Other Ability
The most common trap with a unified recipe is looking at a single metric during evaluation and quietly sacrificing the other capability. The right approach is dual-metric in parallel: on the retrieval side track recall and ranking quality (recall@k, mean reciprocal rank and similar), and on the classification side track accuracy and macro-averaged F1 and similar.
In addition, because contrastive learning is sensitive to large batches and hard negatives, a mismatch between training and validation configurations can also produce results that look good offline and disappoint in production. It is worth monitoring both tasks in production during A/B testing, and checking the embedding space for anisotropy (for example, the mean cosine similarity of random sample pairs) to catch vector collapse early.
7. Deployment: Realizing the Cost and Latency Advantage of a Small Model
Once the model is trained, the deployment approach is what really decides success. Since the core value of an encoder is low cost, low latency, and high throughput, that advantage should be released through appropriate engineering:
- serve the model in a containerized inference service with autoscaling to absorb traffic peaks;
- use a batching interface to merge high-volume scoring and recall tasks into combined requests and amortize unit cost;
- use logging, monitoring, and alerting to continuously track latency, throughput, and quality drift;
- store corpora and vectors in object storage, and use message queues to smooth peaks, fill valleys, and decouple the offline batch pipeline.
On these general-purpose capabilities — container platforms, managed model serving, logging and monitoring, object storage, and similar building blocks offered by cloud platforms such as Baidu AI Cloud — an encoder that trains once and ships both capabilities can move into production more smoothly. Availability and specifics vary by provider and region, so check the official documentation for the services you intend to use. The point worth stressing is that the deployment value here comes from general engineering practice, not from any proprietary interface.
8. Conclusion
The either/or between classification and retrieval was never the destiny of bidirectional encoders. It was legacy baggage left behind by an outdated training recipe. Once the problem is redefined from “change the architecture” to “change the recipe”, the solution falls out naturally: use multi-stage training to align the embedding space, use loss weighting to explicitly manage the tug-of-war between the two objectives, use sensible pooling and normalization to serve both similarity and decidability, and hold the line with dual-metric evaluation.
For developers, this means one model and one feature space can support retrieval recall, data filtering, and reward scoring at the same time — saving the maintenance cost of an entire parallel system while preserving a low-latency, low-cost engineering baseline. That is what it really means to train an encoder to its limit: classification and retrieval finally stop competing for a single slot.
