Callback

Author

galopy

Published

August 25, 2023

Callback

Callback image

There are many callbacks used in deep learning. So, what is a callback? Simply, a callback is a function.

Here is an example. Let’s say we have a fit function that takes a long time, like a neural net.

from time import sleep
from functools import partial
def fit(epochs):
    res = []
    for epoch in range(epochs):
        sleep(0.5)
        res.append(epoch)
    return res
fit(4)
[0, 1, 2, 3]

As the function runs, we want to know how it’s doing. So, we use a callback to figure that out. Here is fit with callback.

def fit(epochs, cb=None):
    res = []
    for epoch in range(epochs):
        sleep(0.5)
        res.append(epoch)
        if cb: cb(epoch)
    return res

And this is a callback to print current epoch.

def print_status(epoch):
    print(f'Currently on {epoch} epoch.')
fit(4, print_status)
Currently on 0 epoch.
Currently on 1 epoch.
Currently on 2 epoch.
Currently on 3 epoch.
[0, 1, 2, 3]

Okay that’s better. We know where it is in the fitting progress as it trains. print_status is just a function.

We can also customize print_status so that we can provide an expression. This provides more flexiblity and power. To use it, we can use a lambda, partial, or closure.

def print_status(expression, epoch):
    print(f'{expression} Currently on {epoch} epoch.')
fit(4, lambda epoch: print_status('Awesome!', epoch))
Awesome! Currently on 0 epoch.
Awesome! Currently on 1 epoch.
Awesome! Currently on 2 epoch.
Awesome! Currently on 3 epoch.
[0, 1, 2, 3]
fit(4, partial(print_status, 'Awesome!'))
Awesome! Currently on 0 epoch.
Awesome! Currently on 1 epoch.
Awesome! Currently on 2 epoch.
Awesome! Currently on 3 epoch.
[0, 1, 2, 3]
def make_print_status(expression):
    def _fn(epoch):
        print_status(expression, epoch)
    return _fn
fit(4, make_print_status('Awesome!'))
Awesome! Currently on 0 epoch.
Awesome! Currently on 1 epoch.
Awesome! Currently on 2 epoch.
Awesome! Currently on 3 epoch.
[0, 1, 2, 3]

So, there are many ways to create functions to be used as callbacks. Actually, callback does not even have to be a function. It can also be a callable class.

Class callback

class Callback:
    def __init__(self, expression): self.expression = expression

    def __call__(self, epoch): print_status(self.expression, epoch)
fit(4, Callback('Awesome!'))
Awesome! Currently on 0 epoch.
Awesome! Currently on 1 epoch.
Awesome! Currently on 2 epoch.
Awesome! Currently on 3 epoch.
[0, 1, 2, 3]