AI from scratch · MLOps · Stage 7: production
MLOps
A model in production is never finished. The world it learned from keeps changing: new stations open, a holiday changes who rides when, a new kind of outage appears. Meanwhile the model keeps answering, confidently, with no idea anything moved. MLOps is the set of habits that notice and respond: watch the service, its inputs, its outputs and, when the truth arrives, its accuracy; ship new versions without betting the product on them; and keep the whole path from data to deployed model repeatable. The examples use this site's archive of NYC subway elevator and escalator outages.
1. The loop
Every stage of this path appears in it: the toolkit's pipelines and experiment tracking, chapter 11 and 12's honest evaluation, the serving lesson's API. What production adds is that the loop runs again and again, mostly by itself, and that its last step, monitoring, decides when it runs.
Production ML is a loop, from data through a safe deployment to monitoring, and monitoring decides when to go round again.
2. What to watch
| Layer | Watch | Available |
|---|---|---|
| Service | Latency percentiles, errors, timeouts (the serving lesson) | Immediately |
| Inputs | Each feature's distribution compared with training: data drift; missing values; new categories | Immediately |
| Outputs | The distribution of predictions: share flagged, average probability | Immediately |
| Outcomes | Accuracy, AUC, calibration against the truth | Late: an outage's length is known only when it ends; fraud, weeks later |
The truth usually arrives late or never, so the cheap, immediate layers (inputs and outputs) are early warnings for the expensive one. A change in the relationship itself, the same inputs now meaning a different outcome, is concept drift, and only outcomes reveal it.
Watch four layers: the service and its inputs and outputs now, and its accuracy when the truth arrives.
3. Measuring drift
The population stability index compares a feature's distribution in a reference window (the training data) with a live window, bin by bin:
\[ \text{PSI} = \sum_{\text{bins}} (p_{\text{live}} - p_{\text{ref}}) \ln \frac{p_{\text{live}}}{p_{\text{ref}}} \]
Rules of thumb: below 0.1 stable, 0.1 to 0.25 worth a look, above 0.25 a real shift. Compare windows of the outage archive:
reference window live window
Two things stand out. Reasons drift between weekdays and weekends (PSI 0.32): planned work is 15% of weekday outages and 5% at weekends. That's the city's weekly rhythm, not a problem, and a monitor that compares Monday with Saturday will page someone every week; compare like with like (this weekend against last weekend). And between the first and second half of the week reasons drift too (0.22), yet a model trained on the first half scores better on the second (AUC 0.90 against 0.86 on its own training days). Drift is a reason to look, not proof of damage; outcomes decide.
Your fraud model's input drift alarm fires every year in late November. Accuracy, checked weeks later, was fine every time. What should change?
- Retrain the model every November
- The reference window: compare against the same season last year (or include past holiday seasons in the reference), so the normal cycle stops alarming
- Turn off drift monitoring in November
Drift scores like PSI compare live inputs with a reference; choose the reference so normal cycles don't alarm, and let outcomes decide whether drift hurt.
Why PSI is a two-way KL divergence
- Probability 5's KL divergence measures the extra surprise of using \(q\) when the truth is \(p\): \(\operatorname{KL}(p \Vert q) = \sum p \ln (p/q)\). It isn't symmetric.
- Add both directions: \(\operatorname{KL}(p \Vert q) + \operatorname{KL}(q \Vert p) = \sum p \ln (p/q) + \sum q \ln (q/p)\).
- The second sum is \(-\sum q \ln (p/q)\), so together: \(\sum (p - q) \ln (p/q)\). That's PSI, with \(p\) the live share and \(q\) the reference share.
- So PSI is the symmetric KL between the two windows: zero only when they match, never negative, and measured in the same "nats of surprise" as cross-entropy.
- It also explains PSI's weak spot: a bin that's empty in one window has \(\ln (p/0)\) = infinity, so implementations add a small floor to every share, and the score then depends on that choice (below).
4. Shipping a new model safely
A retrained model that wins offline can still lose in production (ML strategy's mismatch, the experiments lesson's lucky seed). So new versions climb a ladder, each rung exposing more traffic only if the last one held:
| Rung | What happens | Risk to users |
|---|---|---|
| Offline evaluation | The champion (current model) and the challenger both scored on the newest held-out data, paired (P4) | None |
| Shadow | The challenger sees real requests and its answers are logged, but users get the champion's | None (costs compute) |
| Canary | 1–5% of traffic gets the challenger; errors, latency and output distribution watched | A few users |
| A/B test | A planned split with a business metric, sample size and guardrails (Probability 4) | Half, briefly |
| Promote, keep rollback | The challenger becomes champion; the old version stays one command away | – |
New models earn traffic step by step: offline, shadow, canary, A/B; and the previous version stays ready for a one-command rollback.
5. Pipelines that check themselves
- Validate data before training. Expected columns and types, value ranges, share of missing values, known categories. A feed that suddenly sends hours as minutes should stop the pipeline, not train a model.
- Gate on evaluation. The pipeline refuses to register a model whose AUC on the newest data is below a floor, or below the champion's by more than noise.
- Register every model with its data fingerprint, code version, metrics and config (the experiments lesson), so "which model answered this?" always has an answer.
- Write a model card: what it's for, what data it learned from, how it was evaluated (including per-group results, chapter 12's slices), and where it shouldn't be used.
A nightly retraining job's newest model scores AUC 0.61 on recent data; the champion scored 0.89 last week. What should the pipeline do?
- Refuse to register it, alert someone, and keep the champion serving
- Deploy it: it's trained on fresher data
- Average the two models
Automated pipelines validate data, gate models on evaluation, and register everything they produce, so a bad batch becomes an alert, not a deployed model.
Where it breaks: when the model changes its own data
- Feedback loops. A fraud model blocks the transactions it flags, so their outcomes are never seen; the next model trains only on what the old one let through, and learns less about exactly the cases that mattered. Recommenders shape the clicks they later learn from. Keep a small random slice of traffic unaffected by the model to see the world it hides.
- Empty bins. A category seen in one window and absent in the other makes PSI depend on the tiny floor you add (the weekday/weekend reasons include two that only appeared at weekends). Merge rare categories into "other" before computing drift.
- Labels that come late, biased. Long outages are labeled last, so accuracy measured "so far this week" is computed on the short ones only. Wait for labels to mature, or measure on cohorts old enough to be complete.
- Retraining on noise. Retraining at every small drift swaps a stable model for a slightly different one trained on less data, with new failure modes. Retrain on a schedule or on evidence (outcomes), not on every alarm.
6. What to carry forward
- The loop: data → validate → train → evaluate → register → deploy safely → monitor.
- Monitor service, inputs, outputs now; outcomes when labels arrive (often late).
- PSI = symmetric KL between windows; pick references that include normal cycles; drift is a reason to look.
- Shadow → canary → A/B → promote, with rollback ready.
- Pipelines validate data, gate on metrics, register models with their story; watch for feedback loops.
All lessons: AI from scratch. Google's "Rules of Machine Learning" and Chip Huyen's "Designing Machine Learning Systems" go much further.
Last updated