helios.core.loggers.tensorboard

Classes

TensorboardWriter

Wrapper for the Tensorboard SummaryWriter.

Module Contents

class helios.core.loggers.tensorboard.TensorboardWriter[source]

Bases: helios.core.loggers.base.Logger

Wrapper for the Tensorboard SummaryWriter.

Data for the logger will be placed under log_root/tensorboard. When resuming, the original run directory is restored and new data is appended to it.

Requires the tensorboard package. Install it with:

pip install tensorboard
setup(run_name: str, log_root: pathlib.Path | None, is_resume: bool) None[source]

Finish configuring the TensorboardWriter.

In particular, this function will create the writer instance and assign it to the given path. If the path already exists, Tensorboard will automatically append to the previous run. In distributed training, the writer will only be created on rank 0.

Parameters:
  • run_name – the name of the current run.

  • log_root – root directory for logs. None disables Tensorboard.

  • is_resumeTrue when continuing a previous run.

property run_path: pathlib.Path

Return the path to the current run folder.

add_scalar(tag: str, scalar_value: float | str | torch.Tensor, global_step: int | None = None) None[source]

Add scalar data to the log.

Parameters:
  • tag – name of the scalar to plot.

  • scalar_value – the scalar to plot.

  • global_step – (optional) step for the given scalar.

add_scalars(main_tag: str, tag_scalar_dict: dict[str, float | torch.Tensor], global_step: int | None) None[source]

Add multiple scalars to the log.

Parameters:
  • main_tag – the parent name for the tags.

  • tag_scalar_dict – key-value pair storing tag and corresponding values.

  • global_step – global step value to record.

add_image(tag: str, img_tensor: torch.Tensor | numpy.typing.NDArray, global_step: int | None = None, dataformats: str = 'CHW') None[source]

Add image data to the log.

Parameters:
  • tag – data identifier.

  • img_tensor – image data.

  • global_step – (optional) global step value to record.

  • dataformats – (optional) image data format specification in the form CHW, HWC, HW, WH, etc.

add_images(tag: str, img_tensor: torch.Tensor | numpy.typing.NDArray, global_step: int | None = None, dataformats: str = 'NCHW') None[source]

Add batched image data to the log.

Parameters:
  • tag – data identifier.

  • img_tensor – image data.

  • global_step – (optional) global step value to record.

  • dataformats – (optional) image data format specification in the form NCHW, NHWC, CHW, HWC, HW, WH, etc.

add_figure(tag: str, figure: matplotlib.pyplot.Figure | list[matplotlib.pyplot.Figure], global_step: int | None = None, close: bool = True) None[source]

Render a matplotlib figure into an image and add it to a summary.

Parameters:
  • tag – data identifier.

  • figure – figure or a list of figures.

  • global_step – (optional) global step value to record.

  • close – (optional) flag to automatically close the figure.

add_text(tag: str, text_string: str, global_step: int | None = None) None[source]

Add text data to the log.

Parameters:
  • tag – data identifier.

  • text_string – string to save.

  • global_step – (optional) global step value to record.

add_graph(model: torch.nn.Module, input_to_model: torch.Tensor | list[torch.Tensor] | None = None, verbose: bool = False, use_strict_trace: bool = True) None[source]

Add graph data to the log.

PyTorch currently supports CPU tracing only, so the model and its input(s) will be automatically moved to the CPU prior to logging. The model is copied first so the caller’s model is not affected.

Parameters:
  • model – model to draw.

  • input_to_model – (optional) variable(s) to feed to the model.

  • verbose – (optional) if True, print graph structure in console.

  • use_strict_trace – (optional) if True, pass keyword argument strict to torch.jit.trace.

add_pr_curve(tag: str, labels: torch.Tensor | numpy.typing.NDArray, predictions: torch.Tensor | numpy.typing.NDArray, global_step: int | None = None, num_thresholds: int = 127) None[source]

Add a precision-recall curve to the log.

Parameters:
  • tag – data identifier.

  • labels – ground truth data. Binary label for each element.

  • predictions – the probability that an element be classified as true. Value should be in [0, 1].

  • global_step – (optional) global step value to record.

  • num_thresholds – (optional) number of thresholds used to draw the curve.

add_hparams(hparam_dict: dict[str, Any], metric_dict: dict[str, Any], hparam_domain_discrete: dict[str, list[Any]] | None = None, run_name: str | None = None, global_step: int | None = None) None[source]

Add a set of hyper-parameters to be compared in the log.

Parameters:
  • hparam_dict – each key-value pair is the name of a hyper-parameter and its value. Valid types are bool, str, float, int, or None.

  • metric_dict – each key-value pair is the name of a metric and its value. Keys must be unique in the Tensorboard record.

  • hparam_domain_discrete – (optional) dictionary containing hyper-parameter names and all the discrete values they can hold.

  • run_name – (optional) name of the run, to be included as part of the log dir.

  • global_step – (optional) global step value to record.

flush() None[source]

Flush any cached values to Tensorboard.

close() None[source]

Close the Tensorboard writer.

state_dict() dict[str, Any][source]

Return a dictionary containing the writer state.

The state will be saved under a key called "run_path" holding the current run folder. If Tensorboard was disabled, then None is stored instead.

Returns:

A dictionary with the logger state.

load_state_dict(state_dict: dict[str, Any]) None[source]

Restore the writer state from a previously saved dictionary.

Parameters:

state_dict – the state dictionary returned by state_dict().