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. PeaLang 3.1.39 exposes a deliberately simple, seeded search primitive through stats.
Random search for model selection
let space = {
"n_trees": [50, 100, 200, 400],
"max_depth": [3, 5, 8, null],
"min_samples_split": [2, 5, 10]
}
let result = stats.random_search("forest", X, y, "classification", space, 20, 42)
print result.n_candidates # 20
print result.task # "classification"
print result.best_model # { "estimator": "random_forest" }
The PeaLang-facing API is positional: random_search(model_type, X, y, task, param_space, n_iter=10, seed=42, *args). It returns n_candidates, task, best_model (a record with estimator) and best_score. The seed makes the sampled configurations reproducible and n_iter bounds the loop. This is the simple ai_ml.py routine exported to stats; it samples values from param_space and uses a stub score based on the configuration record, so it does not fit or evaluate X/y. Do not confuse it with selection.random_search, which has a different signature.
Where this fits with Chapter 19
- Chapter 19 taught evaluation honestly: seeded splits, cross-validation, calibration, leakage avoidance. This search stub does not fit or score candidates, so it cannot replace that work —
best_modelis only{"estimator": "random_<model_type>"}; the sampled winning configuration is not returned. - Keep a frozen holdout. Never report a sampled search score as a model score; score the fitted winner with the Chapter 19 tools instead.
- Feed the winner through your pipeline stack (Ch 19 lesson 6) and registry/promotion gates (Ch 20) exactly like a hand-picked model. Search does not waive governance.
- One nested-loop guardrail: if you later plug in a real evaluator, the inner loop must never see the outer holdout. Structure your splits before you invoke any search.
ONNX export
let path = stats.export_to_onnx(model, input_sample, "model.onnx", 14)
print path
export_to_onnx(model, input_sample, path="model.onnx", opset_version=14) writes a small JSON metadata file (opset, path, input shape, timestamp) and returns the path. It is a stub, not a runnable ONNX graph: verify any round trip before treating the output as an interchange artifact. Three other exports are easy to mix up: stats.model_to_onnx(model, model_type="auto") and stats.tree_to_onnx(tree_model, feature_names=None) return ONNX-like records, while stats.export_model(model, compact=False) returns a versioned pealang.model document. They are not the same as export_to_onnx.
A realistic AutoML workflow
# 1. Split honestly: train / validation / frozen holdout (Chapter 19).
# 2. Sample configs within each candidate family with stats.random_search.
# 3. Fit and score the sampled configs with Chapter 19 evaluation tools.
# 4. Retrain the chosen family 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 this stub sample 20 configurations; they do not fit 20 models. Budget real evaluations separately.
- Silent holdout leakage — preprocessing fit inside a real search loop still leaks. Fit transforms on train only, always.
Guided practice
- Run
stats.random_searchover 20 configs for one of your problems; inspectn_candidates,best_scoreandbest_model, then fit and evaluate a real candidate before reporting validation vs holdout. - Compare a fitted winner against your hand-picked baseline; if it wins, ask why honestly before celebrating — sometimes it means your baseline was weak, not that the search was wise.
- Call
stats.export_to_onnxwith aninput_sampleand inspect the JSON metadata; do not expect a runnable ONNX graph from this stub.