I’m creating my custom progress bar for my ML related loop. It’s based on ProgressMeter.jl. The macro takes an argument as input that is a dictionary associating names to vectors of data that I’m saving during the computation. At each loop the metrics are updated and the last updated metric is rounded and then put into the progressbar.
I’m creating this macro such that I can have an easy usage such as:
@trainprogress metrics for i in 1:epochs
. I would also though add a way to disable the progressbar passing an optional enabled, like this
@trainprogress false metrics for i in 1:epochs
. This would disable the progressbar.
Is it possible in general to have a macro with optional arguments? Or even optional keyword arguments? How could I achieve it?
function ml_showvalues(
epoch::Integer,
metrics::Dict{String,<:AbstractVector};
n_digits::Integer=4
)
vec_metrics = Vector{Tuple{String,Any}}([("Epoch", epoch)])
for (key, value_arr) in metrics
value = round(value_arr[epoch], digits=n_digits)
push!(vec_metrics, (key, value))
end
() -> vec_metrics
end
macro trainprogress(metrics_expr, loop_expr; enabled_expr=???)
loop_assignment = loop_expr.args[1]
loop_body = loop_expr.args[2]
iter_var = loop_assignment.args[1]
iter_range = loop_assignment.args[2]
return quote
local metrics_dict = $(esc(metrics_expr))
local epoch_progress = Progress(
length($(esc(iter_range))),
desc="Training: ",
enabled=$(esc(enabled_expr))
)
for $(esc(iter_var)) in $(esc(iter_range))
$(esc(loop_body))
next!(
epoch_progress,
showvalues=ml_showvalues($(esc(iter_var)), metrics_dict)
)
end
end
end