Skip to content
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ EarlyStopping = "792122b4-ca99-40de-a6bc-6742525f08b6"
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"

[compat]
EarlyStopping = "0.1.8"
EarlyStopping = "0.2"
julia = "1"

[extras]
Expand Down
2 changes: 1 addition & 1 deletion src/train.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ function train!(model, controls...; verbosity::Int=1)
training_losses = IterationControl.training_losses(model)
if verbosity > 0
loss isa Nothing || @info "final loss: $loss"
training_losses isa Nothing ||
training_losses isa Nothing || isempty(training_losses) ||
@info "final training loss: $(training_losses[end])"
verbosity > 1 && @info "total control cycles: $n"
end
Expand Down
14 changes: 11 additions & 3 deletions test/stopping_controls.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,23 @@ end
# A stopping criterion than uses training losses:

m = SquareRooter(4)
c = PQ()
c = PQ(k=5)

# Note that `SquareRooter` does not cache training losses. It only
# makes the most recent losses available, which is the worst-case
# scenario.

IC.train!(m, 3)
state = IC.update!(c, m, 0, 1)
@test state.training_losses == reverse(m.training_losses)
train_losses = m.training_losses
@test reverse(state.training_losses) == train_losses # length 3
@test !IC.done(c, state)

IC.train!(m, 2)
state = IC.update!(c, m, 0, 2, state)
@test state.training_losses == reverse(m.training_losses)
train_losses = vcat(train_losses, m.training_losses) # length 2+3 = 5

@test reverse(state.training_losses) == train_losses
@test !IC.done(c, state)
report = IC.takedown(c, 1, state)
@test !report.done
Expand Down
32 changes: 32 additions & 0 deletions test/train.jl
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,35 @@ end
WithNumberDo(),
IterationControl.skip(WithLossDo(), predicate=3)))
end

@testset "integration test with PQ" begin
# get complete training losses:
model = SquareRooter(1e40)
IC.train!(model, 70)
tlosses = IC.training_losses(model)

# get complete losses:
model = SquareRooter(1e40)
losses = map(1:70) do _
IC.train!(model, 1)
IC.loss(model)
end

# train with PQ criterion:
K = 5
model = SquareRooter(1e40)
pq = PQ(k=K)
states = Any[]
f(s) = push!(states, s)
controls = [Step(1), NumberLimit(70), IC.with_state_do(pq, f=f)]
IC.train!(model, controls..., verbosity=0)

# check internal state of PQ makes sense, as far as `loss` and
# `training_losses` are concerned:
@test map(s->s.loss, states) == losses
@test map(enumerate(states)) do (j, s)
L = min(K, j) # what length of PQ training_losses should be
t= s.loss == losses[j] &&
s.training_losses == reverse(tlosses[j-L+1:j])
end |> all
end