Skip to content

Keras

Package containing the Telegram Keras functions and classes.

KerasTelegramCallback

This class allows to send through a Telegram Bot updates about your training.

__init__(self, bot, epoch_bar=True, to_plot=[]) special

Constructor

Parameters:

Name Type Description Default
bot TelegramBot

TelegramBot object

required
epoch_bar bool

True to receive the current epoch progress bar

True
to_plot list

list of dict contaings plot details (e.g. metric to plots and style)

{
    'metrics': ['acc', 'val_acc'],
    'title':'Accuracy plot',
    'ylabel':'acc',
    'ylim':(0, 1),
    'xlim':(1, n_epochs)
}
[]
Source code in bob_telegram_tools\keras.py
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
def __init__(self, bot: TelegramBot, epoch_bar: bool = True, to_plot: list = []):
    """
    Constructor

    Arguments:
        bot: TelegramBot object

        epoch_bar: True to receive the current epoch progress bar

        to_plot: list of dict contaings plot details (e.g. metric to plots and style)
            ```python
            {
                'metrics': ['acc', 'val_acc'],
                'title':'Accuracy plot',
                'ylabel':'acc',
                'ylim':(0, 1),
                'xlim':(1, n_epochs)
            }
            ```

    """

    self.bot = bot
    self.name = str(int(time.time()))+'.png'
    self.epoch_bar = epoch_bar
    if self.epoch_bar:
        self.pbar = None
    self.to_plot = to_plot
    self.plot_id = {}

    for i in range(len(self.to_plot)):
        p = self.to_plot[i]
        p['id'] = i
        self.plot_id[p['id']] = None

on_batch_begin(self, batch, logs={})

A backwards compatibility alias for on_train_batch_begin.

Source code in bob_telegram_tools\keras.py
76
77
78
79
80
def on_batch_begin(self, batch, logs={}):
    if self.epoch_bar:
        if self.pbar is None:
            self.obj = TelegramTqdm(self.bot)
            self.pbar = self.obj(total=self.n_steps)

on_batch_end(self, batch, logs={})

A backwards compatibility alias for on_train_batch_end.

Source code in bob_telegram_tools\keras.py
82
83
84
85
86
87
88
89
90
def on_batch_end(self, batch, logs={}):
    if self.epoch_bar:
        message = ''
        for m in self.metrics:
            if 'val_' not in m:
                message += m+(': %.4f - ' % logs[m])

        self.pbar.set_description(message[:-3])
        self.pbar.update(1)

on_epoch_end(self, batch, logs={})

Called at the end of an epoch.

Subclasses should override for any actions to run. This function should only be called during TRAIN mode.

Parameters:

Name Type Description Default
epoch

integer, index of epoch.

required
logs

dict, metric results for this training epoch, and for the validation epoch if validation is performed. Validation result keys are prefixed with val_.

{}
Source code in bob_telegram_tools\keras.py
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
def on_epoch_end(self, batch, logs={}):
    self.current_epoch += 1

    fields = ['Status', 'Epoch']
    units = ['', '']
    values = ['TRAINING', str(self.current_epoch)+'/'+str(self.n_epochs)]

    self.bot.update_structured_text(self.msg, fields, values, units)

    for m in self.metrics:
        self.history[m].append(logs[m])

    for plot_par in self.to_plot:
        self.plot_id[plot_par['id']] = self.plot(
            plot_par, self.plot_id[plot_par['id']])

on_train_begin(self, logs={})

Called at the beginning of training.

Subclasses should override for any actions to run.

Parameters:

Name Type Description Default
logs

dict. Currently no data is passed to this argument for this method but that may change in the future.

{}
Source code in bob_telegram_tools\keras.py
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
def on_train_begin(self, logs={}):
    self.n_epochs = self.params['epochs']
    self.metrics = self.params['metrics']

    self.n_steps = self.params['samples']//self.params['batch_size']
    self.n_steps += 1 if self.params['samples'] % self.params['batch_size'] != 0 else 0

    self.history = {}
    for metric in self.metrics:
        self.history[metric] = []

    self.current_epoch = 0

    fields = ['Status', 'Epoch']
    units = ['', '']
    values = ['TRAINING', str(self.current_epoch)+'/'+str(self.n_epochs)]

    self.msg = self.bot.send_structured_text(fields, values, units)

on_train_end(self, logs={})

Called at the end of training.

Subclasses should override for any actions to run.

Parameters:

Name Type Description Default
logs

dict. Currently no data is passed to this argument for this method but that may change in the future.

{}
Source code in bob_telegram_tools\keras.py
108
109
110
111
112
113
def on_train_end(self, logs={}):
    fields = ['Status']
    units = ['']
    values = ['TRAINING END']

    self.bot.update_structured_text(self.msg, fields, values, units)