Skip to content

Commit 3560be6

Browse files
authored
Merge pull request #128 from nomadbl/step_context
do-notation API for logging everything in one step
2 parents 7678145 + 65badd3 commit 3560be6

File tree

3 files changed

+76
-1
lines changed

3 files changed

+76
-1
lines changed

Diff for: examples/Scalars.jl

+25
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,28 @@ with_logger(logger) do
2323
@info "scalar/complex" y = z
2424
end
2525
end
26+
27+
28+
################control step increments with context################
29+
with_logger(logger) do
30+
for epoch in 1:10
31+
for i=1:100
32+
# increments global_step by default
33+
with_TBLogger_hold_step() do
34+
# all of these are logged at the same global_step
35+
# and the logger global_step is only then increased
36+
@info "train1/scalar" val=i
37+
@info "train2/scalar" val2=i/2
38+
@info "train3/scalar" val3=100-i
39+
end
40+
end
41+
# step increment at end can be disabled for easy train/test sync
42+
with_TBLogger_hold_step(;step_at_end=false) do
43+
# all of these are logged at the same global_step
44+
# and the logger global_step is only then increased
45+
@info "test1/scalar" epoch=epoch
46+
@info "test2/scalar" epoch2=epoch^2
47+
@info "test3/scalar" epoch3=epoch^3
48+
end
49+
end
50+
end

Diff for: src/TBLogger.jl

+50
Original file line numberDiff line numberDiff line change
@@ -298,3 +298,53 @@ Base.show(io::IO, mime::MIME"text/plain", tbl::TBLogger) = begin
298298
"""
299299
Base.print(io, str)
300300
end
301+
302+
"""
303+
`with_TBLogger_hold_step(f, [step]; step_at_end::Bool=true)`
304+
Context function to ease control of logging steps and synchronization.
305+
Amount of step increment can be controlled via `set_step_increment!``.
306+
307+
Example:
308+
```julia
309+
with_logger(lg) do
310+
for epoch in 1:10
311+
for i=1:100
312+
# increments global_step by default
313+
with_TBLogger_hold_step() do
314+
# all of these are logged at the same global_step
315+
# and the logger global_step is only then increased
316+
@info "train1/scalar" i=i
317+
@info "train2/scalar" i2=i^2
318+
@info "train3/scalar" i3=i^3
319+
end
320+
end
321+
# step increment at end can be disabled for easy train/test sync
322+
with_TBLogger_hold_step(;step_at_end=false) do
323+
# all of these are logged at the same global_step
324+
# and the logger global_step is only then increased
325+
@info "test1/scalar" i=i
326+
@info "test2/scalar" i2=i^2
327+
@info "test3/scalar" i3=i^3
328+
end
329+
end
330+
end
331+
```
332+
333+
"""
334+
function with_TBLogger_hold_step(f, step::Int; step_at_end::Bool=true)
335+
logger = CoreLogging.current_logger()
336+
@assert logger isa TBLogger "with_TBLogger_hold_step: current logger is not a TBLogger, cannot establish current step automatically"
337+
curr_step = logger.global_step
338+
curr_increment = logger.step_increment
339+
set_step!(logger, step)
340+
set_step_increment!(logger, 0)
341+
f()
342+
set_step!(logger, curr_step)
343+
set_step_increment!(logger, curr_increment)
344+
step_at_end && increment_step!(logger, curr_increment)
345+
end
346+
function with_TBLogger_hold_step(f; step_at_end::Bool=true)
347+
logger = CoreLogging.current_logger()
348+
isa(logger, TBLogger) || error("with_TBLogger_hold_step: current logger is not a TBLogger, cannot establish current step automatically")
349+
with_TBLogger_hold_step(f, logger.global_step; step_at_end=step_at_end)
350+
end

Diff for: src/TensorBoardLogger.jl

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ using Base.CoreLogging: CoreLogging, AbstractLogger, LogLevel, Info,
1919
handle_message, shouldlog, min_enabled_level, catch_exceptions, with_logger,
2020
NullLogger
2121

22-
export TBLogger, reset!, set_step!, increment_step!, set_step_increment!
22+
export TBLogger, reset!, set_step!, increment_step!, set_step_increment!, with_TBLogger_hold_step
2323
export log_histogram, log_value, log_vector, log_text, log_image, log_images,
2424
log_audio, log_audios, log_graph, log_embeddings, log_custom_scalar
2525
export map_summaries, TBReader

0 commit comments

Comments
 (0)