-
Notifications
You must be signed in to change notification settings - Fork 45
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’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Learning graph in mach #759
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
ad1871b
add test Project file
olivierlabayle ba025fc
extract check_signature function
olivierlabayle 26f549b
add possibility to propagate some option (kwargs...) for learning
olivierlabayle 64de9e3
add new learning_network definition API
olivierlabayle e2ad39e
add caching test
olivierlabayle 11783f6
update location of methods and bacward compatibility
olivierlabayle e967b5f
add update
olivierlabayle 38a576f
add acceleration priority to composite model if implemented
olivierlabayle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,9 +31,10 @@ mutable struct DeterministicStack{modelnames, inp_scitype, tg_scitype} <: Determ | |
metalearner::Deterministic | ||
resampling | ||
measures::Union{Nothing,AbstractVector} | ||
function DeterministicStack(modelnames, models, metalearner, resampling, measures) | ||
acceleration::Union{AbstractResource, Nothing} | ||
function DeterministicStack(modelnames, models, metalearner, resampling, measures, acceleration) | ||
inp_scitype, tg_scitype = input_target_scitypes(models, metalearner) | ||
return new{modelnames, inp_scitype, tg_scitype}(models, metalearner, resampling, measures) | ||
return new{modelnames, inp_scitype, tg_scitype}(models, metalearner, resampling, measures, acceleration) | ||
end | ||
end | ||
|
||
|
@@ -42,9 +43,10 @@ mutable struct ProbabilisticStack{modelnames, inp_scitype, tg_scitype} <: Probab | |
metalearner::Probabilistic | ||
resampling | ||
measures::Union{Nothing,AbstractVector} | ||
function ProbabilisticStack(modelnames, models, metalearner, resampling, measures) | ||
acceleration::Union{AbstractResource, Nothing} | ||
function ProbabilisticStack(modelnames, models, metalearner, resampling, measures, acceleration) | ||
inp_scitype, tg_scitype = input_target_scitypes(models, metalearner) | ||
return new{modelnames, inp_scitype, tg_scitype}(models, metalearner, resampling, measures) | ||
return new{modelnames, inp_scitype, tg_scitype}(models, metalearner, resampling, measures, acceleration) | ||
end | ||
end | ||
|
||
|
@@ -147,7 +149,7 @@ report(mach).cv_report | |
``` | ||
|
||
""" | ||
function Stack(;metalearner=nothing, resampling=CV(), measure=nothing, measures=measure, named_models...) | ||
function Stack(;metalearner=nothing, resampling=CV(), measure=nothing, measures=measure, acceleration=nothing, named_models...) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why is the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The goal was to:
|
||
metalearner === nothing && | ||
throw(ArgumentError("No metalearner specified. Use Stack(metalearner=...)")) | ||
|
||
|
@@ -159,9 +161,9 @@ function Stack(;metalearner=nothing, resampling=CV(), measure=nothing, measures= | |
end | ||
|
||
if metalearner isa Deterministic | ||
stack = DeterministicStack(modelnames, models, metalearner, resampling, measures) | ||
stack = DeterministicStack(modelnames, models, metalearner, resampling, measures, acceleration) | ||
elseif metalearner isa Probabilistic | ||
stack = ProbabilisticStack(modelnames, models, metalearner, resampling, measures) | ||
stack = ProbabilisticStack(modelnames, models, metalearner, resampling, measures, acceleration) | ||
else | ||
throw(ArgumentError("The metalearner should be a subtype | ||
of $(Union{Deterministic, Probabilistic})")) | ||
|
@@ -369,7 +371,7 @@ This function is building the out-of-sample dataset that is later used by the `j | |
for its own training. It also returns the folds_evaluations object if internal | ||
cross-validation results are requested. | ||
""" | ||
function oos_set(m::Stack, Xs::Source, ys::Source, tt_pairs) | ||
function oos_set(m::Stack, Xs::Source, ys::Source, tt_pairs; cache=true) | ||
Zval = [] | ||
yval = [] | ||
folds_evaluations = [] | ||
|
@@ -384,7 +386,7 @@ function oos_set(m::Stack, Xs::Source, ys::Source, tt_pairs) | |
# predictions are subsequently used as an input to the metalearner | ||
Zfold = [] | ||
for model in getfield(m, :models) | ||
mach = machine(model, Xtrain, ytrain) | ||
mach = machine(model, Xtrain, ytrain, cache=cache) | ||
ypred = predict(mach, Xtest) | ||
# Internal evaluation on the fold if required | ||
push!(folds_evaluations, store_for_evaluation(mach, Xtest, ytest, m.measures)) | ||
|
@@ -408,36 +410,36 @@ end | |
####################################### | ||
################# Fit ################# | ||
####################################### | ||
|
||
|
||
""" | ||
fit(m::Stack, verbosity::Int, X, y) | ||
This is the learning network definition for the `Stack` model. | ||
see: `learning_network` | ||
""" | ||
function fit(m::Stack, verbosity::Int, X, y) | ||
check_stack_measures(m, verbosity, m.measures, y) | ||
tt_pairs = train_test_pairs(m.resampling, 1:nrows(y), X, y) | ||
function learning_network(mach::Machine{<:Stack, C}, X, y; verbosity=0::Int, kwargs...) where C | ||
check_stack_measures(mach.model, verbosity, mach.model.measures, y) | ||
tt_pairs = train_test_pairs(mach.model.resampling, 1:nrows(y), X, y) | ||
|
||
Xs = source(X) | ||
ys = source(y) | ||
|
||
Zval, yval, folds_evaluations = oos_set(m, Xs, ys, tt_pairs) | ||
|
||
metamach = machine(m.metalearner, Zval, yval) | ||
Zval, yval, folds_evaluations = oos_set(mach.model, Xs, ys, tt_pairs, cache=C) | ||
|
||
metamach = machine(mach.model.metalearner, Zval, yval, cache=C) | ||
|
||
# Each model is retrained on the original full training set | ||
Zpred = [] | ||
for model in getfield(m, :models) | ||
mach = machine(model, Xs, ys) | ||
ypred = predict(mach, Xs) | ||
for model in getfield(mach.model, :models) | ||
submach = machine(model, Xs, ys, cache=C) | ||
ypred = predict(submach, Xs) | ||
ypred = pre_judge_transform(ypred, typeof(model), target_scitype(model)) | ||
push!(Zpred, ypred) | ||
end | ||
|
||
Zpred = MLJBase.table(hcat(Zpred...)) | ||
ŷ = predict(metamach, Zpred) | ||
|
||
internal_report = internal_stack_report(m, verbosity, tt_pairs, folds_evaluations...) | ||
internal_report = internal_stack_report(mach.model, verbosity, tt_pairs, folds_evaluations...) | ||
|
||
# We can infer the Surrogate by two calls to supertype | ||
mach = machine(supertype(supertype(typeof(m)))(), Xs, ys; predict=ŷ, internal_report...) | ||
|
||
return!(mach, m, verbosity) | ||
return (predict=ŷ, internal_report...) | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
instead use
hasproperty
andgetproperty
. In general model struct fields and properties can be different but it is the properties that are "public".