Part V — The Modern Experience Layer · Chapter 35 · Lesson 2 of 2
AutoML random search and ONNX export
Finding a good model configuration by hand is indistinguishable from superstition wearing a hoodie. v2.3.0 gives you a real — if deliberately simple — search primitive inside the language.
Random search for model selection
let result = analysis.random_search(
"random_forest",
X,
y,
"classification",
{
"n_trees": [50, 100, 200, 400],
"max_depth": [3, 5, 8, null],
"min_samples_split": [2, 5, 10]
},
{ "n_iter": 20, "seed": 42 }
)
print result.n_candidates # 20
print result.task # "classification"
print result.best_model # the winning estimator
The PeaLang-facing API is positional: random_search(model_type, X, y, task, param_space, n_iter=10, seed=42), returning {"n_candidates", "task", "best_model"}. The seed makes runs reproducible — same seed, same candidates, same winner. n_iter bounds the budget. Random search consistently beats grid search when only a few hyperparameters matter, which is most real problems; grid search wastes its budget enumerating the irrelevant dimensions at full cost.
Where this fits with Chapter 19
- Chapter 19 taught evaluation honestly: seeded splits, cross-validation, calibration, leakage avoidance. AutoML is not a shortcut around any of that —
best_modelis still overfit to whatever validation you handed the search. - Keep a frozen holdout. Never report the search score as the model score; the search optimizes that metric by construction.
- Feed the winner through your pipeline stack (Ch 19 lesson 6) and registry/promotion gates (Ch 20) exactly like a hand-picked model. AutoML does not waive governance.
- One nested-loop guardrail: the inner loop (search) must never see the outer holdout. Structure your splits before you invoke the search.
ONNX export
let onnx = analysis.export_to_onnx(model, { "input": "x", "output": "y" })
export_to_onnx emits an ONNX-shaped export stub — an interchange boundary for moving fitted models out of PeaLang into runtimes you do not own (Python onnxruntime, browser JS runtimes, mobile). Current status: a deliberate stub. Treat it as the contract point, and verify round-trip predictions before trusting the export for anything user-facing. When it matures, the interface is designed to stay — the stub grows a backend, not a new API.
A realistic AutoML workflow
# 1. Split honestly: train / validation / frozen holdout (Chapter 19).
# 2. Search within each candidate family on the validation set.
# 3. Pick the winning family by validation score.
# 4. Retrain the winner on train+validation with the winning config.
# 5. Report exactly once on the frozen holdout.
# 6. Register through the Chapter 20 lifecycle — bundle, sign, promote.
Failure modes worth knowing
- The seductive leaderboard — a winner by 0.4% on 200 validation rows is noise, not signal. Report intervals, not point estimates.
- Search-budget theater — 20 iterations of random search over three meaningful parameters is fine; over fifteen is a random walk with a budget.
- Silent holdout leakage — preprocessing fit inside the search loop still leaks. Fit transforms on train only, always.
Guided practice
- Run
random_searchover 20 candidates for one of your problems; report validation vs holdout side by side. - Compare the AutoML winner against your hand-picked baseline; if it wins, ask why honestly before celebrating — sometimes it means your baseline was weak, not that the machine is wise.
- Export the winning model with
export_to_onnxand inspect the file; even as a stub, it is the integration point you will need when production moves off-device.