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

Created a minibatch iterator selection function #57

Merged
merged 23 commits into from
Nov 4, 2022
Merged
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
64 changes: 60 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,20 +44,76 @@ vcat_arrays_vector(arrays_vector::AbstractVector{Param}) = vcat(Knet.cat1d.(arra
"""
reset_minibatch_train!(nlp::AbstractKnetNLPModel)

Select a new training minibatch for `nlp`.
Typically used before a new evaluation of the loss function/gradient.
Select a the first training minibatch for `nlp`.
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved
"""
reset_minibatch_train!(nlp::AbstractKnetNLPModel) =
nlp.current_training_minibatch = first(nlp.training_minibatch_iterator)
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved

"""
rand_minibatch_train!(nlp::AbstractKnetNLPModel)

Select a training minibatch for `nlp` randomly.
"""
rand_minibatch_train!(nlp::AbstractKnetNLPModel) =
nlp.current_training_minibatch = rand(nlp.training_minibatch_iterator)

"""
minibatch_next_train!(nlp::AbstractKnetNLPModel)
Selects the next mini-batch from training_minibatch_iterator
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved
nlp:: KnetNLPModel
i:: current location in the iterator
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved
Returns a new current location of the iterator i, if 0 is returned when all mini-batches are visited
can be used in a loop or method call - refere to KnetNLPModelProblems for more use cases
"""
function minibatch_next_train!(nlp::AbstractKnetNLPModel, i::int)
i += nlp.size_minibatch # update the i by mini_batch size
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved
if (i >= nlp.training_minibatch_iterator.imax)
# reset to the begining and return zero
nlp.current_training_minibatch = first(nlp.training_minibatch_iterator) # reset to the first one
return 0
else
dpo marked this conversation as resolved.
Show resolved Hide resolved
next = iterate(nlp.training_minibatch_iterator, i)
nlp.current_training_minibatch = next[1]
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved
return i
end
end

"""
reset_minibatch_test!(nlp::AbstractKnetNLPModel)

Select a new test minibatch for `nlp`.
Select a new test minibatch for `nlp` at random.
"""
reset_minibatch_test!(nlp::AbstractKnetNLPModel) =
rand_minibatch_test!(nlp::AbstractKnetNLPModel) =
nlp.current_test_minibatch = rand(nlp.test_minibatch_iterator)

"""
reset_minibatch_train!(nlp::AbstractKnetNLPModel)
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved

Select a the first test minibatch for `nlp`.
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved
"""
reset_minibatch_test!(nlp::AbstractKnetNLPModel) =
nlp.current_test_minibatch = first(nlp.test_minibatch_iterator)

"""
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved
minibatch_next_test!(nlp::AbstractKnetNLPModel)
Selects the next mini-batch from test_minibatch_iterator
nlp:: KnetNLPModel
i:: current location in the iterator
Returns a new current location of the iterator i, if 0 is returned when all mini-batches are visited
can be used in a loop or method call - refere to KnetNLPModelProblems for more use cases
"""
function minibatch_next_test!(nlp::AbstractKnetNLPModel, i::int)
i += nlp.size_minibatch #TODO in the futue we might want to have different size for minbatch test vs train
if (i >= nlp.test_minibatch_iterator.imax)
nlp.current_test_minibatch = first(nlp.test_minibatch_iterator)
return 0
else
next = iterate(nlp.test_minibatch_iterator, i)
nlp.current_test_minibatch = next[1]
return i
end
end
farhadrclass marked this conversation as resolved.
Show resolved Hide resolved

"""
accuracy(nlp::AbstractKnetNLPModel)

Expand Down