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

Add check that Flux optimiser is not being used #260

Merged
merged 1 commit into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
10 changes: 9 additions & 1 deletion src/mlj_model_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@ MLJModelInterface.deep_properties(::Type{<:MLJFluxModel}) =

# # CLEAN METHOD

const ERR_BAD_OPTIMISER = ArgumentError(
"Flux.jl optimiser detected. Only optimisers from Optimisers.jl are supported. "*
"For example, use `optimiser=Optimisers.Momentum()` after `import Optimisers`. "
)

function MLJModelInterface.clean!(model::MLJFluxModel)
warning = ""
if model.lambda < 0
Expand Down Expand Up @@ -40,6 +45,9 @@ function MLJModelInterface.clean!(model::MLJFluxModel)
"on an RNG during training, such as `Dropout`. Consider using "*
" `Random.default_rng()` instead. `"
end
# TODO: This could be removed in next breaking release (0.6.0):
model.optimiser isa Flux.Optimise.AbstractOptimiser && throw(ERR_BAD_OPTIMISER)

return warning
end

Expand Down Expand Up @@ -79,7 +87,7 @@ function regularized_optimiser(model, nbatches)
Optimisers.WeightDecay(λ_weight),
model.optimiser,
)
end
end
end

function MLJModelInterface.fit(model::MLJFluxModel,
Expand Down
4 changes: 4 additions & 0 deletions test/mlj_model_interface.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ end
end
@test model.acceleration == CUDALibs()
end

@test_throws MLJFlux.ERR_BAD_OPTIMISER NeuralNetworkClassifier(
optimiser=Flux.Optimise.Adam(),
)
end

@testset "regularization: logic" begin
Expand Down
Loading