cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Machine Learning
Dive into the world of machine learning on the Databricks platform. Explore discussions on algorithms, model training, deployment, and more. Connect with ML enthusiasts and experts.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

Why I started evaluating the pipeline instead of choosing the best model at each stage

FrankAzzollini
New Contributor III

I recently worked on a multi-label classification pipeline on Databricks, using embeddings, scikit-learn classifiers, Delta Lake, and MLflow to track experiments.

One result caught my attention: the embedding model that ranked fourth in my initial screening ended up in the best-performing combination when evaluated with the actual classifier.

The experiments also surfaced a few less obvious issues:

  • The best decision threshold varied significantly across model combinations.

  • Rebuilding the label vocabulary at inference time could silently associate predictions with the wrong labels.

  • Differences between training and inference inputs could go unnoticed if you only evaluate individual stages.

I documented the experiments, results, code, and lessons learned here:

My Medium Article

I'm curious how others approach this in Databricks: when you have multiple interacting components, do you run a joint search over the pipeline, or narrow down candidates using stage-level metrics first?

Frank
2 REPLIES 2

Isaac_18
Contributor

Your fourth-place embedding winning the full combo is a great example of why picking the best model at each stage can mislead you. Stage metrics measure each piece alone, but the classifier and threshold only see what the embedding hands them.

What tends to work for me is a middle path: use stage-level metrics only to prune the clearly bad options (keep the top few, not the top one), then do a joint search over what's left. With MLflow nested runs that stays readable:

import itertools, mlflow, numpy as np
from sklearn.metrics import f1_score

with mlflow.start_run(run_name="pipeline_search"):
    for emb, clf_name in itertools.product(top_embeddings, classifiers):
        with mlflow.start_run(run_name=f"{emb}+{clf_name}", nested=True):
            X_tr, X_val = embed(emb, train_texts), embed(emb, val_texts)
            clf = make_clf(clf_name).fit(X_tr, Y_tr)   # e.g. OneVsRest, returns (n, n_labels) probs
            probs = clf.predict_proba(X_val)
            t, f1 = max(((t, f1_score(Y_val, probs >= t, average="micro"))
                         for t in np.arange(0.1, 0.9, 0.05)), key=lambda x: x[1])
            mlflow.log_params({"embedding": emb, "classifier": clf_name, "threshold": round(t, 2)})
            mlflow.log_metric("val_micro_f1", f1)

Two things I'd add to your list:

  • Tune the threshold per combination on validation data, then report the final number on a separate test split. Otherwise the threshold quietly overfits.
  • For the label vocabulary problem, save the exact label order with the model, e.g. mlflow.log_dict({"labels": list(mlb.classes_)}, "labels.json"), or wrap binarizer, classifier and threshold into one pyfunc so inference can't rebuild it differently.

slowder
Contributor

Frank, hereโ€™s a third answer to your joint-search vs. prune-first question, building on @Isaac_18โ€™s.

Instead of running a new search every time, I build an offline evaluation suite from historical run data. I replay candidate pipelines against those captured snapshots, try different combinations, and score each on the final outcome. Because the suite is built once and reused, every later change is scored against the same baseline, whether itโ€™s a new embedding model, a different classifier or a retuned threshold. It also works as a regression check.

I also change what counts as winning. Instead of taking the combination with the top score, I look for the smallest, fastest, or most deterministic pipeline that still clears an acceptable bar on the suite. That keeps the pipeline cheaper to run and easier to debug, which matters for batches that have to run reliably every day.

On the label vocabulary, to build on Isaac_18โ€™s mlflow.log_dict tip: load the pinned labels at inference and fail loudly if they donโ€™t match the modelโ€™s output width. Pinning prevents the drift, and the check catches it if something upstream changes anyway.

labels = mlflow.artifacts.load_dict(f"runs:/{run_id}/labels.json")["labels"]
probs = model.predict_proba(X)
if probs.shape[1] != len(labels):
    raise ValueError(f"{len(labels)} labels but model emits {probs.shape[1]} outputs")
Patterns travel. Practices depend. Build it governed, ship it boring.
โ€” Shannon Lowder, Blueprint Technologies | https://bpcs.com/blog | https://shannonlowder.com