# Question Regarding leveraging MLJ.jl's CV features for my own

**URL:** <https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439>\
**Category:** Machine Learning\
**Created:** [November 24, 2019, 2:45am UTC](https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439 "2019-11-24T02:45:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 24, 2019, 2:45am UTC](https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439/1 "2019-11-24T02:45:50Z")

</div>

I tried to read through the [Adding Models for General Use · MLJ](https://alan-turing-institute.github.io/MLJ.jl/dev/adding_models_for_general_use/) article, but I can’t find any information on how I can expose the hyper-parameters in my model to MLJ.jl so that I can use MLJ’s infrastructure to do CV. Can someone give some info here so it’s easily searchable for future reference?

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [November 24, 2019, 8:21am UTC](https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439/2 "2019-11-24T08:21:40Z")

</div>

Hello, to interface with MLJ you basically need to tick the following boxes:

- import MLJBase in your package
- write a constructor for your model that meets the requirements for MLJ (basically a mutable struct which contains the hyperparameters of your models) that is a subtype of either Probabilistic or Deterministic (in your case if you have a binary classifier without scores, it will go under `Deterministic`),
- write a `clean!` method which checks that hyperparameters passed meet constraints for your model
- write a `fit`, `fitted_params` and `predict` method
- add metadata using the `metadata_pkg` and `metadata_model` functions.

To help you with this I would recommend considering:

- any of the examples in MLJModels.jl (for instance the XGBoost interface)
- the MLJLinearModels package which shows maybe more explicitly how you can write an interface from an external package

you’ll see that these interfaces all follow essentially the same pattern so it should be reasonably easy to adapt to your case.

**Note** : with respect to writing a constructor + `clean!` method, you can also use the `@mlj_model` macro which does some of the work for you, again please consider examples in MLJModels for instance the interface for NearestNeighbors.jl

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 24, 2019, 8:42am UTC](https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439/3 "2019-11-24T08:42:11Z")

</div>

My question is more about CV and hyper-parameter training. Each type of models’s hyper parameters are different, so how do I tell MLJ which are the hyper parameters to tune? That’s the thing I am a bit not sure about at the moment.

---

<div class="post-metadata">

**Author:** ![tlienart](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tlienart/32/7640_2.png) [@tlienart](https://discourse.julialang.org/u/tlienart)\
**Post date:** [November 24, 2019, 9:47am UTC](https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439/4 "2019-11-24T09:47:04Z")

</div>

Once you have a working interface, the HP tuning via CV is done through a `TunedModel`, either see [the docs](https://alan-turing-institute.github.io/MLJ.jl/stable/tuning_models/) or look at [this tutorial using XGB](https://alan-turing-institute.github.io/MLJTutorials/pub/end-to-end/crabs-xgb.html) with XGb for an example.

**Edit** :

- _how do I tell which are the hp to tune_ MLJ considers all fields of the model to be hyperparameters that can be tuned

- _each type are a bit different_ that’s considered automatically, there’s two scenario, either it’s a numeric HP in which case you specify a `lower=` and `upper=` and MLJ works out an appropriate sampling that matches the type of the HP **or** it’s not a numeric in which case you specify a `values=[...]` (e.g. if the hp is a symbol or a string or a metric)

---

<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:** [November 25, 2019, 12:15am UTC](https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439/5 "2019-11-25T00:15:22Z")

</div>

Yes, to emphasise the point already made by @tlienart, in MLJ a “model” struct only contains hyperparameters and not learned parameters. The learned parameters are part of the output of the model `fit` method you must implement (and labeled `fitresult` in the docs), and part of the _input_ of the `predict` (or `transform`, etc) method.

---

<div class="post-metadata">

**Author:** ![xiaodai](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/xiaodai/32/15937_2.png) [@xiaodai](https://discourse.julialang.org/u/xiaodai)\
**Post date:** [November 25, 2019, 12:42am UTC](https://discourse.julialang.org/t/question-regarding-leveraging-mlj-jls-cv-features-for-my-own/31439/6 "2019-11-25T00:42:49Z")

</div>

Yeah. Implemented a first cut here [https://github.com/xiaodaigh/JLBoost.jl#mljjl-integrations](https://github.com/xiaodaigh/JLBoost.jl#mljjl-integrations)
