from time import sleep
from functools import partial
Callback
Callback
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.
def fit(epochs):
= []
res for epoch in range(epochs):
0.5)
sleep(
res.append(epoch)return res
4) fit(
[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):
0.5)
sleep(
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.')
4, print_status) fit(
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.')
4, lambda epoch: print_status('Awesome!', epoch)) fit(
Awesome! Currently on 0 epoch.
Awesome! Currently on 1 epoch.
Awesome! Currently on 2 epoch.
Awesome! Currently on 3 epoch.
[0, 1, 2, 3]
4, partial(print_status, 'Awesome!')) fit(
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
4, make_print_status('Awesome!')) fit(
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)
4, Callback('Awesome!')) fit(
Awesome! Currently on 0 epoch.
Awesome! Currently on 1 epoch.
Awesome! Currently on 2 epoch.
Awesome! Currently on 3 epoch.
[0, 1, 2, 3]