What are callbacks in Julia and how do I use them?

Viewed 353

I have seen a few different documentations refer to the term "callbacks" (Flux.jl and some SciML packages) but it is not clear what that means in the context of Julia.

I know that a callback is a function passed as an argument to another function but is there more to it than that? What would the use case for such a paradigm be?

1 Answers

A classic example of use of a callback is the progress bar. A callback is supplied to the function that is doing work within some kind of sequence. At regular intervals during the sequence the callback is called with some kind of information about the work being done (the percent completed in the case of the progress bar, which updates a progress display).

Flux can call a callback each time it completes a segment of training, or in the example in the source code, every 10 seconds:

https://github.com/FluxML/Flux.jl/blob/b78a27b01c9629099adb059a98657b995760b617/src/optimise/train.jl#L80-L95

Related