# How do I tune a pipeline in MLJ?

**URL:** <https://discourse.julialang.org/t/how-do-i-tune-a-pipeline-in-mlj/111155>\
**Category:** Machine Learning\
**Tags:** optimization, mlj\
**Created:** [March 4, 2024, 8:07pm UTC](https://discourse.julialang.org/t/how-do-i-tune-a-pipeline-in-mlj/111155 "2024-03-04T20:07:39Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [March 4, 2024, 8:07pm UTC](https://discourse.julialang.org/t/how-do-i-tune-a-pipeline-in-mlj/111155/1 "2024-03-04T20:07:39Z")

</div>

A slack user has asked the question in the title. More generally, how does one tune hyperparameters that are nested in composed model?

One obtains composed models, for example, when applying `Stack`, `EnsembleModel`, `IteratedModel`, `BinaryThresholdPredictor`, `BalancedModel`, `TunedModel` and other model “wrappers”.

---

<div class="post-metadata">

**Author:** ![ablaom](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ablaom/32/4889_2.png) [@ablaom](https://discourse.julialang.org/u/ablaom)\
**Post date:** [March 4, 2024, 8:08pm UTC](https://discourse.julialang.org/t/how-do-i-tune-a-pipeline-in-mlj/111155/2 "2024-03-04T20:08:00Z")

</div>

Here’s an example addressing the question:

```julia
Pkg.activate(temp=true)
Pkg.add(["MLJ", "MLJXGBoostInterface"])
using MLJ

X, y = @load_reduced_ames;

# notice `X` has mixed feature types:
schema(X)

# vertically split data:
(Xtrain, Xtest), (ytrain, ytest) = partition((X, y), 0.8, multi=true)

XGBoostRegressor = @load XGBoostRegressor
pipe = ContinuousEncoder() |> XGBoostRegressor()

propertynames(pipe)
# (:continuous_encoder, :xg_boost_regressor, :cache)

# range for a nested hyperparameter:
r = range(pipe, :(xg_boost_regressor.max_depth), lower=3, upper=10)

# self-tuning pipeline:
tmodel = TunedModel(
    pipe,
    resampling=CV(nfolds=5),
    tuning=Grid(resolution=10),
    measure = l2,
    range=r,
)

# training:
mach = machine(tmodel, Xtrain, ytrain) |> fit!

# inspect optimal parameter:
best_pipe = report(mach).best_model
best_pipe.xg_boost_regressor.max_depth
# 5

# predict:
predict(mach, Xtest)

```
