TorchTensorBoard

class lib.training.tensorboard.TorchTensorBoard(log_dir: str = 'logs', write_graph: bool = True, update_freq: Literal['batch', 'epoch'] | int = 'epoch')

Bases: Callback

Enable visualizations for TensorBoard. Adapted from Keras’ Tensorboard Callback keeping only the parts we need, and using Torch rather than TensorFlow

Parameters:
  • log_dir (str) – The path of the directory where to save the log files to be parsed by TensorBoard. e.g., log_dir = os.path.join(working_dir, ‘logs’). This directory should not be reused by any other callbacks.

  • write_graph (bool) – Whether to visualize the graph in TensorBoard. Note that the log file can become quite large when write_graph is set to True. Note: Not supported at this time

  • update_freq (T.Literal['batch', 'epoch'] | int) – When using “epoch”, writes the losses and metrics to TensorBoard after every epoch. If using an integer, let’s say 1000, all metrics and losses (including custom ones added by Model.compile) will be logged to TensorBoard every 1000 batches. “batch” is a synonym for 1, meaning that they will be written every batch. Note however that writing too frequently to TensorBoard can slow down your training, especially when used with distribution strategies as it will incur additional synchronization overhead. Batch- level summary writing is also available via train_step override. Please see [TensorBoard Scalars tutorial](https://www.tensorflow.org/tensorboard/scalars_and_keras#batch-level_logging)

Attributes Summary

model

Methods Summary

on_batch_begin(batch[, logs])

A backwards compatibility alias for on_train_batch_begin.

on_batch_end(batch[, logs])

A backwards compatibility alias for on_train_batch_end.

on_epoch_begin(epoch[, logs])

Called at the start of an epoch.

on_epoch_end(epoch[, logs])

Called at the end of an epoch.

on_predict_batch_begin(batch[, logs])

Called at the beginning of a batch in predict methods.

on_predict_batch_end(batch[, logs])

Called at the end of a batch in predict methods.

on_predict_begin([logs])

Called at the beginning of prediction.

on_predict_end([logs])

Called at the end of prediction.

on_save()

Flush data to disk on save

on_test_batch_begin(batch[, logs])

Called at the beginning of a batch in evaluate methods.

on_test_batch_end(batch[, logs])

Called at the end of a batch in evaluate methods.

on_test_begin([logs])

Called at the beginning of evaluation or validation.

on_test_end([logs])

Called at the end of evaluation or validation.

on_train_batch_begin(batch[, logs])

Called at the beginning of a training batch in fit methods.

on_train_batch_end(batch[, logs])

Update Tensorboard logs on batch end

on_train_begin([logs])

Initialize the call back on train start

on_train_end([logs])

Close the writer on train completion

set_model(model)

Sets Keras model and writes graph if specified.

set_params(params)

Attributes Documentation

model

Methods Documentation

on_batch_begin(batch, logs=None)

A backwards compatibility alias for on_train_batch_begin.

on_batch_end(batch, logs=None)

A backwards compatibility alias for on_train_batch_end.

on_epoch_begin(epoch, logs=None)

Called at the start of an epoch.

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

Parameters:
  • epoch – Integer, index of epoch.

  • logs – Dict. Currently no data is passed to this argument for this method but that may change in the future.

on_epoch_end(epoch, logs=None)

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:
  • epoch – Integer, index of epoch.

  • logs – Dict, metric results for this training epoch, and for the validation epoch if validation is performed. Validation result keys are prefixed with val_. For training epoch, the values of the Model’s metrics are returned. Example: {‘loss’: 0.2, ‘accuracy’: 0.7}.

on_predict_batch_begin(batch, logs=None)

Called at the beginning of a batch in predict methods.

Subclasses should override for any actions to run.

Note that if the steps_per_execution argument to compile in Model is set to N, this method will only be called every N batches.

Parameters:
  • batch – Integer, index of batch within the current epoch.

  • logs – Dict. Currently no data is passed to this argument for this method but that may change in the future.

on_predict_batch_end(batch, logs=None)

Called at the end of a batch in predict methods.

Subclasses should override for any actions to run.

Note that if the steps_per_execution argument to compile in Model is set to N, this method will only be called every N batches.

Parameters:
  • batch – Integer, index of batch within the current epoch.

  • logs – Dict. Aggregated metric results up until this batch.

on_predict_begin(logs=None)

Called at the beginning of prediction.

Subclasses should override for any actions to run.

Parameters:

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

on_predict_end(logs=None)

Called at the end of prediction.

Subclasses should override for any actions to run.

Parameters:

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

on_save() None

Flush data to disk on save

Return type:

None

on_test_batch_begin(batch, logs=None)

Called at the beginning of a batch in evaluate methods.

Also called at the beginning of a validation batch in the fit methods, if validation data is provided.

Subclasses should override for any actions to run.

Note that if the steps_per_execution argument to compile in Model is set to N, this method will only be called every N batches.

Parameters:
  • batch – Integer, index of batch within the current epoch.

  • logs – Dict. Currently no data is passed to this argument for this method but that may change in the future.

on_test_batch_end(batch, logs=None)

Called at the end of a batch in evaluate methods.

Also called at the end of a validation batch in the fit methods, if validation data is provided.

Subclasses should override for any actions to run.

Note that if the steps_per_execution argument to compile in Model is set to N, this method will only be called every N batches.

Parameters:
  • batch – Integer, index of batch within the current epoch.

  • logs – Dict. Aggregated metric results up until this batch.

on_test_begin(logs=None)

Called at the beginning of evaluation or validation.

Subclasses should override for any actions to run.

Parameters:

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

on_test_end(logs=None)

Called at the end of evaluation or validation.

Subclasses should override for any actions to run.

Parameters:

logs – Dict. Currently the output of the last call to on_test_batch_end() is passed to this argument for this method but that may change in the future.

on_train_batch_begin(batch, logs=None)

Called at the beginning of a training batch in fit methods.

Subclasses should override for any actions to run.

Note that if the steps_per_execution argument to compile in Model is set to N, this method will only be called every N batches.

Parameters:
  • batch – Integer, index of batch within the current epoch.

  • logs – Dict. Currently no data is passed to this argument for this method but that may change in the future.

on_train_batch_end(batch: int, logs: dict[str, float | dict[str, float]] | None = None) None

Update Tensorboard logs on batch end

Parameters:
  • batch (int) – The current iteration count

  • logs (dict[str, float | dict[str, float]] | None) – The logs to write

Return type:

None

on_train_begin(logs=None) None

Initialize the call back on train start

Parameters:

logs – Unused

Return type:

None

on_train_end(logs=None) None

Close the writer on train completion

Parameters:

logs – Unused

Return type:

None

set_model(model: Model) None

Sets Keras model and writes graph if specified.

Parameters:

model (Model) – The model that is being trained

Return type:

None

set_params(params)
on_save() None

Flush data to disk on save

Return type:

None

on_train_batch_end(batch: int, logs: dict[str, float | dict[str, float]] | None = None) None

Update Tensorboard logs on batch end

Parameters:
  • batch (int) – The current iteration count

  • logs (dict[str, float | dict[str, float]] | None) – The logs to write

Return type:

None

on_train_begin(logs=None) None

Initialize the call back on train start

Parameters:

logs – Unused

Return type:

None

on_train_end(logs=None) None

Close the writer on train completion

Parameters:

logs – Unused

Return type:

None

set_model(model: Model) None

Sets Keras model and writes graph if specified.

Parameters:

model (Model) – The model that is being trained

Return type:

None