Skip to content

Tensorboard eval

neps.plot.tensorboard_eval #

The tblogger module provides a simplified interface for logging to TensorBoard.

SummaryWriter_ #

Bases: SummaryWriter

This class inherits from the base SummaryWriter class and provides modifications to improve the logging. It simplifies the logging structure and ensures consistent tag formatting for metrics.

Changes Made: - Avoids creating unnecessary subfolders in the log directory. - Ensures all logs are stored in the same 'tfevent' directory for better organization. - Updates metric keys to have a consistent 'Summary/' prefix for clarity. - Improves the display of 'Loss' or 'Accuracy' on the Summary file.

Methods: - add_hparams: Overrides the base method to log hyperparameters and metrics with better formatting.

add_hparams #

add_hparams(
    hparam_dict: dict[str, Any],
    metric_dict: dict[str, Any],
    global_step: int,
) -> None

Add a set of hyperparameters to be logged.

Source code in neps/plot/tensorboard_eval.py
@override
def add_hparams(  # type: ignore
    self,
    hparam_dict: dict[str, Any],
    metric_dict: dict[str, Any],
    global_step: int,
) -> None:
    """Add a set of hyperparameters to be logged."""
    if not isinstance(hparam_dict, dict) or not isinstance(metric_dict, dict):
        raise TypeError("hparam_dict and metric_dict should be dictionary.")
    updated_metric = {f"Summary/{key}": val for key, val in metric_dict.items()}
    exp, ssi, sei = hparams(hparam_dict, updated_metric)

    assert self.file_writer is not None
    self.file_writer.add_summary(exp)
    self.file_writer.add_summary(ssi)
    self.file_writer.add_summary(sei)
    for k, v in updated_metric.items():
        self.add_scalar(tag=k, scalar_value=v, global_step=global_step)

tblogger #

The tblogger class provides a simplified interface for logging to tensorboard.

logger_bool class-attribute #

logger_bool: bool = False

logger_bool is true only if tblogger.log is used by the user, this allows to always capturing the configuration data.

disable staticmethod #

disable() -> None

The function allows for disabling the logger functionality. When the logger is disabled, it will not perform logging operations.

By default tblogger is enabled when used.

Example
Disable the logger#

tblogger.disable()

Source code in neps/plot/tensorboard_eval.py
@staticmethod
def disable() -> None:
    """The function allows for disabling the logger functionality.
    When the logger is disabled, it will not perform logging operations.

    By default tblogger is enabled when used.

    Example:
        # Disable the logger
        tblogger.disable()
    """
    tblogger.disable_logging = True

enable staticmethod #

enable() -> None

The function allows for enabling the logger functionality. When the logger is enabled, it will perform the logging operations.

By default this is enabled.

Example
Enable the logger#

tblogger.enable()

Source code in neps/plot/tensorboard_eval.py
@staticmethod
def enable() -> None:
    """The function allows for enabling the logger functionality.
    When the logger is enabled, it will perform the logging operations.

    By default this is enabled.

    Example:
        # Enable the logger
        tblogger.enable()
    """
    tblogger.disable_logging = False

end_of_config staticmethod #

end_of_config() -> None

Closes the writer.

Source code in neps/plot/tensorboard_eval.py
@staticmethod
def end_of_config() -> None:
    """Closes the writer."""
    if tblogger.config_writer:
        # Close and reset previous config writers for consistent logging.
        # Prevent conflicts by reinitializing writers when logging ongoing.
        tblogger.config_writer.close()
        tblogger.config_writer = None

    if tblogger.write_incumbent:
        tblogger._tracking_incumbent_api()

get_status staticmethod #

get_status() -> bool

Returns the currect state of tblogger ie. whether the logger is enabled or not.

Source code in neps/plot/tensorboard_eval.py
@staticmethod
def get_status() -> bool:
    """Returns the currect state of tblogger ie. whether the logger is
    enabled or not.
    """
    return not tblogger.disable_logging

image_logging staticmethod #

image_logging(
    image: Tensor,
    counter: int = 1,
    *,
    resize_images: list[None | int] | None = None,
    random_images: bool = True,
    num_images: int = 20,
    seed: int | RandomState | None = None
) -> tuple[
    str,
    Tensor,
    int,
    list[None | int] | None,
    bool,
    int,
    int | RandomState | None,
]

Prepare an image tensor for logging.

PARAMETER DESCRIPTION
image

Image tensor to be logged.

TYPE: Tensor

counter

Counter value associated with the images.

TYPE: int DEFAULT: 1

resize_images

List of integers for image sizes after resizing.

TYPE: list[None | int] | None DEFAULT: None

random_images

Images are randomly selected if True.

TYPE: bool DEFAULT: True

num_images

Number of images to log.

TYPE: int DEFAULT: 20

seed

Seed value or RandomState instance to control randomness.

TYPE: int | RandomState | None DEFAULT: None

RETURNS DESCRIPTION
tuple[str, Tensor, int, list[None | int] | None, bool, int, int | RandomState | None]

A tuple containing the logging mode and all the necessary parameters for image logging. Tuple: (logging_mode, img_tensor, counter, resize_images, random_images, num_images, seed).

Source code in neps/plot/tensorboard_eval.py
@staticmethod
def image_logging(
    image: torch.Tensor,
    counter: int = 1,
    *,
    resize_images: list[None | int] | None = None,
    random_images: bool = True,
    num_images: int = 20,
    seed: int | np.random.RandomState | None = None,
) -> tuple[
    str,
    torch.Tensor,
    int,
    list[None | int] | None,
    bool,
    int,
    int | np.random.RandomState | None,
]:
    """Prepare an image tensor for logging.

    Args:
        image: Image tensor to be logged.
        counter: Counter value associated with the images.
        resize_images: List of integers for image sizes after resizing.
        random_images: Images are randomly selected if True.
        num_images: Number of images to log.
        seed: Seed value or RandomState instance to control randomness.

    Returns:
        A tuple containing the logging mode and all the necessary parameters for
        image logging.
        Tuple: (logging_mode, img_tensor, counter, resize_images,
                        random_images, num_images, seed).
    """
    logging_mode = "image"
    return (
        logging_mode,
        image,
        counter,
        resize_images,
        random_images,
        num_images,
        seed,
    )

log staticmethod #

log(
    loss: float,
    current_epoch: int,
    *,
    writer_config_scalar: bool = True,
    writer_config_hparam: bool = True,
    write_summary_incumbent: bool = False,
    extra_data: dict | None = None
) -> None

Log experiment data to the logger, including scalar values, hyperparameters, and images.

PARAMETER DESCRIPTION
loss

Current loss value.

TYPE: float

current_epoch

Current epoch of the experiment (used as the global step).

TYPE: int

writer_config_scalar

Displaying the loss or accuracy curve on tensorboard (default: True)

TYPE: bool DEFAULT: True

writer_config_hparam

Write hyperparameters logging of the configs.

TYPE: bool DEFAULT: True

write_summary_incumbent

Set to True for a live incumbent trajectory.

TYPE: bool DEFAULT: False

extra_data

Additional experiment data for logging.

TYPE: dict | None DEFAULT: None

Source code in neps/plot/tensorboard_eval.py
@staticmethod
def log(
    loss: float,
    current_epoch: int,
    *,
    writer_config_scalar: bool = True,
    writer_config_hparam: bool = True,
    write_summary_incumbent: bool = False,
    extra_data: dict | None = None,
) -> None:
    """Log experiment data to the logger, including scalar values,
    hyperparameters, and images.

    Args:
        loss: Current loss value.
        current_epoch: Current epoch of the experiment (used as the global step).
        writer_config_scalar: Displaying the loss or accuracy
            curve on tensorboard (default: True)
        writer_config_hparam: Write hyperparameters logging of the configs.
        write_summary_incumbent: Set to `True` for a live incumbent trajectory.
        extra_data: Additional experiment data for logging.
    """
    if tblogger.disable_logging:
        tblogger.logger_bool = False
        return

    tblogger.logger_bool = True

    tblogger.current_epoch = current_epoch
    tblogger.loss = loss
    tblogger.write_incumbent = write_summary_incumbent

    tblogger._initiate_internal_configurations()

    if writer_config_scalar:
        tblogger._write_scalar_config(tag="Loss", value=loss)

    if writer_config_hparam:
        tblogger._write_hparam_config()

    if extra_data is not None:
        for key in extra_data:
            if extra_data[key][0] == "scalar":
                tblogger._write_scalar_config(tag=str(key), value=extra_data[key][1])

            elif extra_data[key][0] == "image":
                tblogger._write_image_config(
                    tag=str(key),
                    image=extra_data[key][1],
                    counter=extra_data[key][2],
                    resize_images=extra_data[key][3],
                    random_images=extra_data[key][4],
                    num_images=extra_data[key][5],
                    seed=extra_data[key][6],
                )

scalar_logging staticmethod #

scalar_logging(value: float) -> tuple[str, float]

Prepare a scalar value for logging.

PARAMETER DESCRIPTION
value

The scalar value to be logged.

TYPE: float

RETURNS DESCRIPTION
Tuple

A tuple containing the logging mode and the value for logging. The tuple format is (logging_mode, value).

TYPE: tuple[str, float]

Source code in neps/plot/tensorboard_eval.py
@staticmethod
def scalar_logging(value: float) -> tuple[str, float]:
    """Prepare a scalar value for logging.

    Args:
        value (float): The scalar value to be logged.

    Returns:
        Tuple: A tuple containing the logging mode and the value for logging.
            The tuple format is (logging_mode, value).
    """
    logging_mode = "scalar"
    return (logging_mode, value)