Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
38 changes: 23 additions & 15 deletions lib/OptimizationOptimisers/src/OptimizationOptimisers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,13 @@ SciMLBase.requiresgradient(opt::AbstractRule) = true
SciMLBase.allowsfg(opt::AbstractRule) = true

function SciMLBase.__init(
prob::SciMLBase.OptimizationProblem, opt::AbstractRule; save_best = true,
callback = (args...) -> (false), epochs = nothing,
progress = false, kwargs...)
return OptimizationCache(prob, opt; save_best, callback, progress, epochs,
kwargs...)
prob::SciMLBase.OptimizationProblem, opt::AbstractRule;
callback = (args...) -> (false),
epochs::Union{Number, Nothing} = nothing,
maxiters::Union{Number, Nothing} = nothing,
save_best::Bool = true, progress::Bool = false, kwargs...)
return OptimizationCache(prob, opt; callback, epochs, maxiters,
save_best, progress, kwargs...)
end

function SciMLBase.__solve(cache::OptimizationCache{
Expand Down Expand Up @@ -50,20 +52,26 @@ function SciMLBase.__solve(cache::OptimizationCache{
dataiterate = false
end

epochs = if cache.solver_args.epochs === nothing
if cache.solver_args.maxiters === nothing
throw(ArgumentError("The number of iterations must be specified with either the epochs or maxiters kwarg. Where maxiters = epochs*length(data)."))
epochs, maxiters = if isnothing(cache.solver_args.maxiters) &&
isnothing(cache.solver_args.epochs)
throw(ArgumentError("The number of iterations must be specified with either the epochs or maxiters kwarg. Where maxiters = epochs * length(data)."))
elseif !isnothing(cache.solver_args.maxiters) &&
!isnothing(cache.solver_args.epochs)
if cache.solver_args.maxiters == cache.solver_args.epochs * length(data)
cache.solver_args.epochs, cache.solver_args.maxiters
else
cache.solver_args.maxiters / length(data)
throw(ArgumentError("The number of iterations must be specified with either the epochs or maxiters kwarg. Where maxiters = epochs * length(data)."))
end
else
cache.solver_args.epochs
elseif isnothing(cache.solver_args.maxiters)
cache.solver_args.epochs, cache.solver_args.epochs * length(data)
elseif isnothing(cache.solver_args.epochs)
cache.solver_args.maxiters / length(data), cache.solver_args.maxiters
end

epochs = Optimization._check_and_convert_maxiters(epochs)
if epochs === nothing
throw(ArgumentError("The number of iterations must be specified with either the epochs or maxiters kwarg. Where maxiters = epochs*length(data)."))
end
maxiters = Optimization._check_and_convert_maxiters(maxiters)

# At this point, both of them should be fine; but, let's assert it.
@assert (isnothing(epochs)||isnothing(maxiters) || (maxiters != epochs * length(data))) "The number of iterations must be specified with either the epochs or maxiters kwarg. Where maxiters = epochs * length(data)."

opt = cache.opt
θ = copy(cache.u0)
Expand Down
13 changes: 13 additions & 0 deletions lib/OptimizationOptimisers/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,19 @@ using Lux, MLUtils, Random, ComponentArrays, Printf, MLDataDevices
@test sol.stats.fevals == 1000
@test sol.stats.gevals == 1000

@testset "epochs & maxiters" begin
optprob = SciMLBase.OptimizationFunction(
(u, data) -> sum(u) + sum(data), Optimization.AutoZygote())
prob = SciMLBase.OptimizationProblem(optprob, ones(2), ones(2))
@test_throws ArgumentError solve(prob, Optimisers.Adam())
@test_throws ArgumentError solve(
prob, Optimisers.Adam(), epochs = 2, maxiters = 2)
@test solve(prob, Optimisers.Adam(), epochs = 2)
@test solve(prob, Optimisers.Adam(), maxiters = 2)
@test solve(prob, Optimisers.Adam(), epochs = 2, maxiters = 4)
@test_throws AssertionError solve(prob, Optimisers.Adam(), maxiters = 3)
end

@testset "cache" begin
objective(x, p) = (p[1] - x[1])^2
x0 = zeros(1)
Expand Down
Loading