Skip to content

tqdm_progress_bar

TQDMProgressBar

Classes

TQDMProgressBar

TQDMProgressBar(metrics_separator: str = ' - ', overall_bar_format: str = '{l_bar}{bar} {n_fmt}/{total_fmt} ETA: {remaining}s,  {rate_fmt}{postfix}', epoch_bar_format: str = '{n_fmt}/{total_fmt}{bar} ETA: {remaining}s - {desc}', metrics_format: str = '{name}: {value:0.4f}', update_per_second: int = 10, leave_epoch_progress: bool = True, leave_overall_progress: bool = True, show_epoch_progress: bool = True, show_overall_progress: bool = True)

TQDM Progress Bar callback.

Parameters:

  • metrics_separator (str, default: ' - ' ) –

    Custom separator between metrics. Defaults to ' - '.

  • overall_bar_format (str, default: '{l_bar}{bar} {n_fmt}/{total_fmt} ETA: {remaining}s, {rate_fmt}{postfix}' ) –

    Custom bar format for overall (outer) progress bar, see https://github.com/tqdm/tqdm#parameters for more detail.

  • epoch_bar_format (str, default: '{n_fmt}/{total_fmt}{bar} ETA: {remaining}s - {desc}' ) –

    Custom bar format for epoch (inner) progress bar, see https://github.com/tqdm/tqdm#parameters for more detail.

  • update_per_second (int, default: 10 ) –

    Maximum number of updates in the epochs bar per second, this is to prevent small batches from slowing down training. Defaults to 10.

  • metrics_format (str, default: '{name}: {value:0.4f}' ) –

    Custom format for how metrics are formatted. See https://github.com/tqdm/tqdm#parameters for more detail.

  • leave_epoch_progress (bool, default: True ) –

    True to leave epoch progress bars.

  • leave_overall_progress (bool, default: True ) –

    True to leave overall progress bar.

  • show_epoch_progress (bool, default: True ) –

    False to hide epoch progress bars.

  • show_overall_progress (bool, default: True ) –

    False to hide overall progress bar.

Example:

import time
import neuralspot_edge as nse

# Create a TQDM progress bar
pb_callback = nse.callbacks.TQDMProgressBar(
    overall_bar_format="{l_bar}{bar} {n_fmt}/{total_fmt},  {rate_fmt}{postfix}",
    epoch_bar_format="{n_fmt}/{total_fmt}{bar} - {desc}",
    metrics_format="{name}: {value:0.4f}",
    update_per_second=10,
    leave_epoch_progress=True,
)

# Simulate training
epochs = 10
steps = 5
pb_callback.set_params(dict(epochs=epochs, steps=steps))
pb_callback.on_train_begin()
loss = 1.0
accuracy = 0.0
for epoch in range(epochs):
    pb_callback.on_epoch_begin(epoch)
    for step in range(steps):
        loss -= epoch * step / (epochs * steps)
        accuracy += epoch * step / (epochs * steps)
        pb_callback.on_batch_end(step, {"loss": loss, "accuracy": accuracy})
        time.sleep(0.1)
    pb_callback.on_epoch_end(epoch, {"loss": loss, "accuracy": accuracy})
pb_callback.on_train_end()
Source code in neuralspot_edge/callbacks/tqdm_progress_bar.py
def __init__(
    self,
    metrics_separator: str = " - ",
    overall_bar_format: str = "{l_bar}{bar} {n_fmt}/{total_fmt} ETA: " "{remaining}s,  {rate_fmt}{postfix}",
    epoch_bar_format: str = "{n_fmt}/{total_fmt}{bar} ETA: " "{remaining}s - {desc}",
    metrics_format: str = "{name}: {value:0.4f}",
    update_per_second: int = 10,
    leave_epoch_progress: bool = True,
    leave_overall_progress: bool = True,
    show_epoch_progress: bool = True,
    show_overall_progress: bool = True,
):
    """TQDM Progress Bar callback.

    Args:
        metrics_separator (str, optional): Custom separator between metrics.
            Defaults to ' - '.
        overall_bar_format (str, optional): Custom bar format for overall
            (outer) progress bar, see https://github.com/tqdm/tqdm#parameters
            for more detail.
        epoch_bar_format (str, optional): Custom bar format for epoch
            (inner) progress bar, see https://github.com/tqdm/tqdm#parameters
            for more detail.
        update_per_second (int, optional): Maximum number of updates in the epochs bar
            per second, this is to prevent small batches from slowing down
            training. Defaults to 10.
        metrics_format (str, optional): Custom format for how metrics are formatted.
            See https://github.com/tqdm/tqdm#parameters for more detail.
        leave_epoch_progress (bool, optional): `True` to leave epoch progress bars.
        leave_overall_progress: `True` to leave overall progress bar.
        show_epoch_progress (bool, optional): `False` to hide epoch progress bars.
        show_overall_progress (bool, optional): `False` to hide overall progress bar.

    Example:

    ```python

    import time
    import neuralspot_edge as nse

    # Create a TQDM progress bar
    pb_callback = nse.callbacks.TQDMProgressBar(
        overall_bar_format="{l_bar}{bar} {n_fmt}/{total_fmt},  {rate_fmt}{postfix}",
        epoch_bar_format="{n_fmt}/{total_fmt}{bar} - {desc}",
        metrics_format="{name}: {value:0.4f}",
        update_per_second=10,
        leave_epoch_progress=True,
    )

    # Simulate training
    epochs = 10
    steps = 5
    pb_callback.set_params(dict(epochs=epochs, steps=steps))
    pb_callback.on_train_begin()
    loss = 1.0
    accuracy = 0.0
    for epoch in range(epochs):
        pb_callback.on_epoch_begin(epoch)
        for step in range(steps):
            loss -= epoch * step / (epochs * steps)
            accuracy += epoch * step / (epochs * steps)
            pb_callback.on_batch_end(step, {"loss": loss, "accuracy": accuracy})
            time.sleep(0.1)
        pb_callback.on_epoch_end(epoch, {"loss": loss, "accuracy": accuracy})
    pb_callback.on_train_end()
    ```
    """

    self.metrics_separator = metrics_separator
    self.overall_bar_format = overall_bar_format
    self.epoch_bar_format = epoch_bar_format
    self.leave_epoch_progress = leave_epoch_progress
    self.leave_overall_progress = leave_overall_progress
    self.show_epoch_progress = show_epoch_progress
    self.show_overall_progress = show_overall_progress
    self.metrics_format = metrics_format

    # compute update interval (inverse of update per second)
    self.update_interval = 1 / update_per_second

    self.last_update_time = time.time()
    self.overall_progress_tqdm = None
    self.epoch_progress_tqdm = None
    self.is_training = False
    self.num_epochs = None
    self.logs = None
    super().__init__()

Functions

format_metrics
format_metrics(logs: dict = {}, factor=1) -> str

Format metrics in logs into a string.

Parameters:

  • logs (dict, default: {} ) –

    dictionary of metrics and their values. Defaults to empty dictionary.

  • factor (int, default: 1 ) –

    The factor we want to divide the metrics in logs by, useful when we are computing the logs after each batch. Defaults to 1.

Returns:

  • metrics_string ( str ) –

    a string displaying metrics using the given

  • str

    formators passed in through the constructor.

Source code in neuralspot_edge/callbacks/tqdm_progress_bar.py
def format_metrics(self, logs: dict = {}, factor=1) -> str:
    """Format metrics in logs into a string.

    Args:
        logs (dict): dictionary of metrics and their values. Defaults to
            empty dictionary.
        factor (int): The factor we want to divide the metrics in logs
            by, useful when we are computing the logs after each batch.
            Defaults to 1.

    Returns:
        metrics_string: a string displaying metrics using the given
        formators passed in through the constructor.
    """

    metric_value_pairs = []
    for key, value in logs.items():
        if key in ["batch", "size"]:
            continue
        pair = self.metrics_format.format(name=key, value=value / factor)
        metric_value_pairs.append(pair)
    metrics_string = self.metrics_separator.join(metric_value_pairs)
    return metrics_string