One Encoder, Three Jobs: Multi-Task NLP for E-commerce Search
March 2026 · 10 min read
When someone types "red summer dress" into an e-commerce search bar, they want a product page. When they type "modern home decor ideas", they want to browse. When they type "how to clean leather jacket", they don't want to buy anything at all, and showing them a wall of leather jackets is exactly the wrong move.
That distinction is buying intent, and it was the problem I worked on for a large Dutch e-commerce platform. Search behaves differently depending on what kind of query it's looking at. So upstream of ranking, something needs to answer: is this person hunting a specific product, exploring a category, looking for inspiration, or not shopping at all?
It sounds like a single classification task. It turned into three models, and then, in the most satisfying part of the project, back into one.
First, teach BERT what queries look like
We started from multilingual BERT, because the queries are mostly Dutch with a heavy sprinkling of English and brand names from everywhere. But a base BERT has never seen search queries, and queries aren't really sentences: two to five words, no grammar, brand names glued to model numbers, typos that a human parses without noticing.
So before any classification, we ran masked-language-model fine-tuning on a large corpus of real, anonymized queries from the domain. Nothing exotic here, just the standard HuggingFace Trainer, masking tokens and asking the model to fill them in. The result was a domain-adapted encoder that had internalized how people actually type when they shop. Every downstream metric moved up when we swapped it in. If you take one practical thing from this post: MLM fine-tuning on in-domain text is cheap and boring, and it works.
There's a second, less glamorous layer of domain knowledge that runs before the encoder ever sees a token: lexical preprocessing. A LexicalPreprocessor enriches each raw query with markers the model can use. It runs flashtext for exact matching against known book titles (books are a notorious intent trap, because a title looks like a natural-language sentence), rapidfuzz for fuzzy hits against category names, and adds some NLTK-derived token features. Feature engineering feeding a transformer felt slightly heretical in 2026, like installing a carburetor on an EV. It also measurably helped, so I stopped feeling bad about it. Short queries are information-starved, so anything that injects domain knowledge before 32 tokens of budget runs out is worth its complexity.
Try it
The training loop underneath BERT fine-tuning lives in two playgrounds. Neural Trainer builds a 2→8→8→1 network by hand and trains it live, so you can watch the loss curve fall and the decision boundary form on XOR, circles, or spirals. Gradient Descent Playground isolates the optimizer: place a ball on a loss surface, pick a learning rate, and see how fast it converges, or whether it oscillates or overshoots.
Then it became three tasks
Intent classification alone wasn't enough for the search team. Two more questions kept coming up. Does this query contain a brand or creator name? That's entity recognition, and knowing "stan smith" is a shoe and not a person changes everything. And which product category does this query most likely belong to? That one is a prediction over roughly fifteen thousand categories.
So the system grew three heads, each with its own personality. The intent classifier got the full treatment: complete backpropagation through the encoder, masked mean pooling over the token embeddings, dropout, one linear layer. The NER model froze most of the encoder, unfroze the top two layers, and put a CRF on top for BIO tagging, because entity boundaries benefit from the CRF's ability to reason about label transitions. "B-BRAND cannot follow I-CREATOR" is the kind of constraint I'd rather enforce in the structure than hope the model picks up statistically. The category model also kept the encoder mostly frozen and used an interaction-style head, inspired by the EXAM line of work, to cope with an output space of fifteen thousand labels without the final layer becoming most of the model.
Compute was only part of the reason for the freezing pattern. Full fine-tuning on the two auxiliary tasks would have dragged the shared representation toward their objectives. Freezing all but the top layers let each task specialize where specialization is cheap, near the output, while leaving the domain-adapted foundation intact. We didn't know it yet, but this decision is also what made the next part possible.
The part I'd do again anywhere: the system model
Three BERT-based models means paying for three BERT forward passes on every keystroke-adjacent query. At search-traffic volumes, that's real money and real latency.
All three models, though, were fine-tuned from the same domain-adapted encoder, and only the top layers meaningfully diverged. The bottom of the network, layers 0 through 9, was doing near-identical work three times.
So we built what we called the system model: a single composed network that runs the shared layers once, then branches at layer 10 into the three task-specific paths. You get all three answers from one tokenization and one pass through ten transformer layers.
flowchart TD
Q["query: 'stan smith 43'"] --> P["lexical preprocessing<br/>flashtext · rapidfuzz"]
P --> T["tokenizer (max_length 32)"]
T --> S["shared encoder<br/>BERT layers 0–9<br/>(runs once)"]
S --> B1["layers 10–11 (intent)<br/>mean pool → linear"]
S --> B2["layers 10–11 (NER)<br/>CRF over BIO tags"]
S --> B3["layers 10–11 (category)<br/>interaction head"]
B1 --> O1["buying intent"]
B2 --> O2["brand/creator spans"]
B3 --> O3["top product categories"]
The system model is roughly 2.5× faster than running the models separately, with each task's accuracy preserved, because each branch kept exactly the layers it had specialized. The merged model exports to TorchScript for serving and to ONNX with int8 quantization where CPU inference needs to be extra cheap. We wanted two artifacts from one training run because the serving fleet and the batch backfill jobs run on different hardware. I also just like that the finished model looks like an org chart of the problem, with the shared understanding at the bottom and the specialized judgment at the top.
The synthetic data tax
Labeled queries are expensive, so like everyone in 2025, we generated synthetic training data. And the model looked great on synthetic held-out sets.
Then we evaluated on a smaller set of real labeled queries and watched about eight F1 points evaporate.
Eight points is not a rounding error. Real queries are messier than any generator makes them: the misspellings are weirder, the intent is more ambiguous, and people paste half a product title with the color in the wrong place. The synthetic data had taught the model a tidier world than the one it would actually serve. We never fully closed that gap, but we managed it by making sure real data dominated evaluation even when synthetic data dominated training, and by treating synthetic-set metrics as an upper bound rather than a number we'd report.
If your eval set is synthetic, read its numbers as an optimistic simulation, not as an evaluation.
The pipeline around the model
Training runs on Vertex AI custom jobs, orchestrated by Airflow on Cloud Composer. A g2-standard-4 with an NVIDIA L4 is plenty for models this size. PyTorch Lightning owns the training loop, with the data side wrapped in Lightning DataModules so train/val/test splits and tokenization live in one place instead of leaking across scripts. Optuna handles hyperparameter search with 5-fold cross-validation, which you really want on small labeled sets. With limited real data, a single lucky split can sell you a learning rate that doesn't generalize, and five folds is a cheap way to catch that.
The DAG itself is refreshingly literal:
flowchart LR
A["log training start"] --> B["train intent classifier<br/>(full backprop)"]
B --> C["train NER<br/>(frozen + CRF)"]
B --> D["train category<br/>(frozen + interaction head)"]
C --> E["compose system model<br/>merge shared layers"]
D --> E
E --> F["system inference<br/>evaluate on real + synthetic sets"]
F --> G["log metrics to BigQuery"]
The ordering encodes a dependency that's easy to miss. The intent task trains first because it's the one that updates the whole encoder, so it produces the foundation. NER and category then train in parallel against that foundation, touching only their top layers, and the last steps compose the system model, evaluate it, and log the results. The DAG ends up with the same shape as the model architecture, just written out as a schedule.
For experiment tracking we used BigQuery instead of MLflow or Weights & Biases: a plain table with a timestamp, a run name, a dataset reference, a JSON configuration column, and a nested metrics record. Every training run appends a row, handled by a couple of CLI flags (--bq_table_id, --bq_create_table) in the trainer. I expected to miss a proper tracking UI and mostly didn't. The query language you already know turns out to be a fine way to ask "show me all runs on this dataset ordered by F1". The JSON config column means you don't need a schema migration when the hyperparameters grow, and there's no tracking server to babysit. For a small team, a table you can SQL is 90% of the value at 10% of the ceremony. When we wanted dashboards, a connected sheet was enough.
The final artifact ships to a GKE cluster behind a FastAPI service with request batching. But the serving side, the Terraform behind it, and the load tests we pointed at it are their own story, in the next post.
Play it
Backprop Runner: Final Manifold: A 3D endless runner where you dodge gradient collapse. The loss function is your score and the speed is your epoch counter. It's easier than tuning a learning rate, and harder than it looks.
References
- HuggingFace Transformers: The library behind the pretrained multilingual BERT and the training utilities around it. Its masked-language-model
Trainerlet us domain-adapt the encoder on search queries with almost no custom code. At this data scale, a pretrained encoder plus fine-tuning beats training from scratch by a lot. - PyTorch Lightning: A structure layer over raw PyTorch: it owns the training loop, checkpointing and device handling, while DataModules keep the train/val/test splits and tokenization in one place. It stops every training script from becoming a unique snowflake, which matters when three different task models have to stay comparable.
- Optuna: Hyperparameter search. It samples learning rates, dropouts and batch sizes, prunes bad trials early, and here ran with 5-fold cross-validation so a lucky data split couldn't sell us a configuration that doesn't generalize. On small labeled datasets you can't really skip that check.
- Conditional random fields (CRF): The layer on top of the NER head. Instead of predicting each token's tag independently, a CRF scores whole tag sequences, so impossible transitions like an entity that starts in the middle of another are ruled out structurally. That fixes the boundary errors per-token classifiers make constantly on short queries.
- Vertex AI custom training: Google Cloud's managed training jobs: you hand it a container and a machine spec (here a g2-standard-4 with an L4 GPU), it runs the job and tears the hardware down after. Instead of owning a training GPU that sits idle 95% of the time, you rent one only for the hours a run actually takes.
- flashtext: Exact keyword matching that stays fast with huge dictionaries, unlike chained regexes. Used in lexical preprocessing to mark known book titles in queries, because a title like 'how to kill a mockingbird' looks like a question to a model unless something tells it otherwise.
- RapidFuzz: Fast fuzzy string matching. It catches near-miss category names in queries (typos, partial words) that exact matching misses, so the encoder gets a hint about probable product categories before classification starts.
- ONNX Runtime quantization: Converts model weights from 32-bit floats to 8-bit integers. That shrinks the artifact ~4x and speeds up CPU inference substantially, at a small accuracy cost. We used it for the export path where inference has to be cheap and GPUs aren't available.
- BigQuery: Google's serverless data warehouse, used here as the experiment tracker: every training run appends a row with config as JSON and metrics as a nested record. You get experiment tracking without another service to run, since 'show me all runs ordered by F1' is just SQL.