-
-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #136 from darsnack/log-value
Add ability to record and log arbitrary learner values
- Loading branch information
Showing
4 changed files
with
94 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
name = "FluxTraining" | ||
uuid = "7bf95e4d-ca32-48da-9824-f0dc5310474f" | ||
authors = ["lorenzoh <[email protected]>"] | ||
version = "0.3.2" | ||
version = "0.3.3" | ||
|
||
[deps] | ||
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0" | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
""" | ||
Traces(preprocess[, phase]) | ||
Record a trace during `phase` by apply each pre-processing function in | ||
`preprocess` to the [`Learner`](#) to produce a trace value. | ||
The trace is recorded at the end of each learning step. | ||
See [`LogTraces`](#) for logging of the trace values. | ||
```julia | ||
cb = Traces((loss2 = learner -> learner.step.loss^2, | ||
avg_gnorm = learner -> mean(map((_, g) -> norm(g), pairs(learner.step.grads)))) | ||
TrainingPhase) | ||
``` | ||
""" | ||
struct Traces{P<:Phase} <: Callback | ||
preprocess::NamedTuple | ||
end | ||
|
||
function Traces(preprocess::NamedTuple, P::Type{<:Phase} = Phase) | ||
return Traces{P}(preprocess) | ||
end | ||
|
||
stateaccess(::Traces) = (cbstate = (history = Read(), tracehistory = Write()), | ||
step = Read()) | ||
|
||
function init!(traces::Traces, learner) | ||
length(traces.preprocess) == length(unique(keys(traces.preprocess))) || | ||
error("Multiple traces have the same name!") | ||
if !haskey(learner.cbstate, :tracehistory) | ||
learner.cbstate.tracehistory = DefaultDict{Phase,MVHistory}(() -> MVHistory()) | ||
end | ||
end | ||
|
||
function on(::StepEnd, phase::P, traces::Traces{P}, learner) where {P<:Phase} | ||
step = learner.cbstate.history[phase].steps | ||
history = learner.cbstate.tracehistory[phase] | ||
for (trace_name, f) in pairs(traces.preprocess) | ||
val = f(learner) | ||
push!(history, trace_name, step, val) | ||
end | ||
end | ||
|
||
@testset "Traces" begin | ||
cb = Traces((keya = learner -> sum(learner.step.ys), | ||
keyb = learner -> sum(learner.step.ŷs)), | ||
ValidationPhase) | ||
learner = testlearner(Recorder(), cb) | ||
@test_nowarn fit!(learner, 1) | ||
@test :keya ∈ keys(learner.cbstate.tracehistory[ValidationPhase()]) | ||
@test :keyb ∈ keys(learner.cbstate.tracehistory[ValidationPhase()]) | ||
@test :keya ∉ keys(learner.cbstate.tracehistory[TrainingPhase()]) | ||
@test :keyb ∉ keys(learner.cbstate.tracehistory[TrainingPhase()]) | ||
end |