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’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Learning graph in mach #759

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions src/composition/learning_networks/machines.jl
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ method exists for the composite model. This can be removed in the future.
function fit_(mach::Machine{<:Composite}, resampled_data...; verbosity=0, kwargs...)
if applicable(learning_network, mach, resampled_data...)
signature = learning_network(mach, resampled_data...; verbosity=verbosity, kwargs...)
return finalize(mach, signature; verbosity=verbosity, kwargs...)
return finalize!(mach, signature; verbosity=verbosity, kwargs...)
else
return fit(mach.model, verbosity, resampled_data...)
end
Expand Down Expand Up @@ -490,15 +490,17 @@ function update_(mach::Machine{<:Composite}, resampled_data...; verbosity=0, kwa

end

function finalize(mach::Machine{<:Composite}, signature; kwargs...)
function finalize!(mach::Machine{T}, signature; acceleration=CPU1(), kwargs...) where T <:Composite
check_signature(signature)
# Build composite Fitresult
fitresult = CompositeFitresult(signature)
fitresult.network_model_names = network_model_names(mach.model, fitresult)

# Fit all machines in the learning network
glb_node = glb(fitresult)
fit!(glb_node; kwargs...)
acceleration = hasfield(T, :acceleration) && getfield(mach.model, :acceleration) !== nothing ?
Copy link
Member

Choose a reason for hiding this comment

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

instead use hasproperty and getproperty. In general model struct fields and properties can be different but it is the properties that are "public".

getfield(mach.model, :acceleration) : acceleration
fit!(glb_node; acceleration=acceleration, kwargs...)

# Build report
report_additions_ = _call(_report_part(MLJBase.signature(fitresult)))
Expand Down
16 changes: 9 additions & 7 deletions src/composition/models/stacking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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

Expand Down Expand Up @@ -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...)
Copy link
Member

Choose a reason for hiding this comment

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

Why is the acceleration default nothing instead of CPU1()?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

The goal was to:

  • By default propagate the acceleration specified in fit if the stack's acceleration is nothing
  • Take the stack's acceleration instead if specified.

metalearner === nothing &&
throw(ArgumentError("No metalearner specified. Use Stack(metalearner=...)"))

Expand All @@ -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})"))
Expand Down
2 changes: 1 addition & 1 deletion test/composition/models/stacking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ end
metalearner = DeterministicConstantRegressor()
resampling = CV()

MLJBase.DeterministicStack(modelnames, models, metalearner, resampling, nothing)
MLJBase.DeterministicStack(modelnames, models, metalearner, resampling, nothing, nothing)

# Test input_target_scitypes with non matching target_scitypes
models = [KNNRegressor()]
Expand Down