Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clarify error for uninstantiated model further #178

Merged
merged 5 commits into from Jun 8, 2022

Conversation

rikhuijzer
Copy link
Member

@rikhuijzer rikhuijzer commented Jun 1, 2022

I was trying to do some hyperparameter tuning via

tunedmodel = let
    model = LogisticClassifier
    tuning = Grid()
    range = [
        range(Float64, :lambda; lower=0.01, upper=1, scale=:log),
        range(Float64, :gamma; lower=0.01, upper=1, scale=:log)
    ]
    measure = auc
    TunedModel(; model, tuning, range, measure)
end

and got

ArgumentError: Only `Deterministic` and `Probabilistic` model types supported.

which is very confusing. I had no idea what was the problem here. Fun fact, I got mad at the person who came up with this unclear error message. Turns out it was me #157 馃ぃ

Anyway, this PR allows passing model types so that the code above will "automatically" work. I considered throwing a more clear error, but accepting a model type makes more sense IMO since the tuning strategy will instantiate many models.

Two thoughts:

  • Maybe the instantiate which I wrapped in a try-catch should take the first elements for the range for the first instantiation. Or isn't the instantiated model used anyway?
  • The TunedModel should probably be refactored since it is getting more and more tough to reason about it. We can either do a breaking change and explicitly create TunedModel, TunedModels and other constructors so that we avoid logic like "if explicit is set and models is not set, then do .... Or, we could first figure out what kind of thing the user would like to do and dispatch into the appropriate function at the start of the TunedModels constructor

@ablaom
Copy link
Member

ablaom commented Jun 2, 2022

@rikhuijzer Thanks for this.

I'm not convinced the inconvenience of typing two brackets warrants the introduction of a try-catch block. My vote would be to go with a better error message. How about this one:

https://github.com/JuliaAI/MLJBase.jl/blob/2257471e854a24eba0de7b71e22c8dcb2d3ec9b4/src/machines.jl#L143

@OkonSamuel What do you think?

The TunedModel should probably be refactored since it is getting more and more tough to reason about it.

I don't mind a clean up of the implementation. But you seem to be saying you find it confusing for the user also? Could you say more about that?

I'm not too enthusiastic about splitting this into multiple methods as there are already so many to remember. Perhaps you have a different suggestion?

@OkonSamuel
Copy link
Member

@OkonSamuel What do you think?

Am not really a fan of using try-catch blocks in code. Except as a last resort. Maybe the refactoring of TunedModels is a better option.

@rikhuijzer
Copy link
Member Author

I'm not convinced the inconvenience of typing two brackets warrants the introduction of a try-catch block.

I don't mind a clean up of the implementation. But you seem to be saying you find it confusing for the user also? Could you say more about that?

For me it was confusing that the tuning takes some model and it's parameters, so I assume then that the tuning constructs all my models for me. As an user, it isn't clear why there is a need to manually instantiate the model. Even more, I would normally assume that I should only pass a model type.

@ablaom
Copy link
Member

ablaom commented Jun 6, 2022

For me it was confusing that the tuning takes some model and it's parameters, so I assume then that the tuning constructs all my models for me. As an user, it isn't clear why there is a need to manually instantiate the model. Even more, I would normally assume that I should only pass a model type.

Thanks for pointing out your confusion.

Yes, tuning for non-Explicit strategies constructs of all the models by cloning the provided model and mutating just those hyper parameters specified by the range. So what particular instance you provide makes a difference. I think this is clear from here but I can see this is not spelled out in the MLJ manual. So, happy to have that clarified.

In the case of specifying an explicit list of models, there is no mutation of a fixed model and obviously which instances you provide makes a difference.

Is that clearer?

@rikhuijzer
Copy link
Member Author

Yes, tuning for non-Explicit strategies constructs of all the models by cloning the provided model and mutating just those hyper parameters specified by the range. So what particular instance you provide makes a difference. I think this is clear from here but I can see this is not spelled out in the MLJ manual. So, happy to have that clarified.

Now it all makes sense! 馃槃

How about this (f699f67)?

julia> using MLJBase, MLJTuning

julia model = DeterministicSupervisedDetector;

julia> TunedModel(; model, range=[1])
ERROR: AssertionError: Uninstantiated DeterministicSupervisedDetector passed; pass an instantiated model so that this package can mutate the hyperparameters specified by the range.

In a perfect world, users would have read the manual indeed @ablaom 馃槃 Unfortunately, probably not everyone does. For those people, let's figure out their mistake and give them a clear solution.

@rikhuijzer rikhuijzer changed the title Allow uninstantiated model Clarify error for uninstantiated model further Jun 7, 2022
src/tuned_models.jl Outdated Show resolved Hide resolved
@ablaom
Copy link
Member

ablaom commented Jun 7, 2022

In a perfect world, users would have read the manual indeed @ablaom 馃槃 Unfortunately, probably not everyone does. For those people, let's figure out their mistake and give them a clear solution.

Agreed. A more informative error message very often beats doc-strings. Appreciate the feedback!

rikhuijzer and others added 2 commits June 8, 2022 10:57
Co-authored-by: Anthony Blaom, PhD <anthony.blaom@gmail.com>
@rikhuijzer rikhuijzer requested a review from ablaom June 8, 2022 09:11
Copy link
Member

@ablaom ablaom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @rikhuijzer for this contribution.

@ablaom ablaom merged commit 9436c17 into JuliaAI:dev Jun 8, 2022
@rikhuijzer rikhuijzer deleted the rh/instantiate-models branch June 9, 2022 06:44
@ablaom ablaom mentioned this pull request Jun 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants