Shortcuts

pl_bolts.callbacks.data_monitor module

class pl_bolts.callbacks.data_monitor.DataMonitorBase(log_every_n_steps=None)[source]

Bases: pytorch_lightning.Callback

Base class for monitoring data histograms in a LightningModule. This requires a logger configured in the Trainer, otherwise no data is logged. The specific class that inherits from this base defines what data gets collected.

Parameters

log_every_n_steps (Optional[int]) – The interval at which histograms should be logged. This defaults to the interval defined in the Trainer. Use this to override the Trainer default.

_is_logger_available(logger)[source]
Return type

bool

log_histogram(tensor, name)[source]

Override this method to customize the logging of histograms. Detaches the tensor from the graph and moves it to the CPU for logging.

Parameters
  • tensor (Tensor) – The tensor for which to log a histogram

  • name (str) – The name of the tensor as determined by the callback. Example: ìnput/0/[64, 1, 28, 28]

Return type

None

log_histograms(batch, group='')[source]

Logs the histograms at the interval defined by row_log_interval, given a logger is available.

Parameters
  • batch (Any) – torch or numpy arrays, or a collection of it (tuple, list, dict, …), can be nested. If the data appears in a dictionary, the keys are used as labels for the corresponding histogram. Otherwise the histograms get labelled with an integer index. Each label also has the tensors’s shape as suffix.

  • group (str) – Name under which the histograms will be grouped.

Return type

None

on_train_batch_start(trainer, pl_module, batch, batch_idx, dataloader_idx)[source]
Return type

None

on_train_start(trainer, pl_module)[source]
Return type

None

supported_loggers = (pytorch_lightning.loggers.TensorBoardLogger, pytorch_lightning.loggers.WandbLogger)[source]
class pl_bolts.callbacks.data_monitor.ModuleDataMonitor(submodules=None, log_every_n_steps=None)[source]

Bases: pl_bolts.callbacks.data_monitor.DataMonitorBase

Parameters
  • submodules (Union[bool, List[str], None]) – If True, logs the in- and output histograms of every submodule in the LightningModule, including the root module itself. This parameter can also take a list of names of specifc submodules (see example below). Default: None, logs only the in- and output of the root module.

  • log_every_n_steps (Optional[int]) – The interval at which histograms should be logged. This defaults to the interval defined in the Trainer. Use this to override the Trainer default.

Note

A too low value for log_every_n_steps may have a significant performance impact especially when many submodules are involved, since the logging occurs during the forward pass. It should only be used for debugging purposes.

Example

# log the in- and output histograms of the `forward` in LightningModule
trainer = Trainer(callbacks=[ModuleDataMonitor()])

# all submodules in LightningModule
trainer = Trainer(callbacks=[ModuleDataMonitor(submodules=True)])

# specific submodules
trainer = Trainer(callbacks=[ModuleDataMonitor(submodules=["generator", "generator.conv1"])])
_get_submodule_names(root_module)[source]
Return type

List[str]

_register_hook(module_name, module)[source]
Return type

RemovableHandle

on_train_end(trainer, pl_module)[source]
Return type

None

on_train_start(trainer, pl_module)[source]
Return type

None

GROUP_NAME_INPUT = 'input'[source]
GROUP_NAME_OUTPUT = 'output'[source]
class pl_bolts.callbacks.data_monitor.TrainingDataMonitor(log_every_n_steps=None)[source]

Bases: pl_bolts.callbacks.data_monitor.DataMonitorBase

Callback that logs the histogram of values in the batched data passed to training_step.

Parameters

log_every_n_steps (Optional[int]) – The interval at which histograms should be logged. This defaults to the interval defined in the Trainer. Use this to override the Trainer default.

Example

# log histogram of training data passed to `LightningModule.training_step`
trainer = Trainer(callbacks=[TrainingDataMonitor()])
on_train_batch_start(trainer, pl_module, batch, *args, **kwargs)[source]
Return type

None

GROUP_NAME = 'training_step'[source]
pl_bolts.callbacks.data_monitor.collect_and_name_tensors(data, output, parent_name='input')[source]

Recursively fetches all tensors in a (nested) collection of data (depth-first search) and names them. Data in dictionaries get named by their corresponding keys and otherwise they get indexed by an increasing integer. The shape of the tensor gets appended to the name as well.

Parameters
  • data (Any) – A collection of data (potentially nested).

  • output (Dict[str, Tensor]) – A dictionary in which the outputs will be stored.

  • parent_name (str) – Used when called recursively on a nested input data.

Example

>>> data = {"x": torch.zeros(2, 3), "y": {"z": torch.zeros(5)}, "w": 1}
>>> output = {}
>>> collect_and_name_tensors(data, output)
>>> output  
{'input/x/[2, 3]': ..., 'input/y/z/[5]': ...}
Return type

None

pl_bolts.callbacks.data_monitor.shape2str(tensor)[source]

Returns the shape of a tensor in bracket notation as a string.

Example

>>> shape2str(torch.rand(1, 2, 3))
'[1, 2, 3]'
>>> shape2str(torch.rand(4))
'[4]'
Return type

str

Read the Docs v: 0.3.0
Versions
latest
stable
0.3.0
0.2.5
0.2.4
0.2.3
0.2.2
0.2.1
0.2.0
0.1.1
0.1.0
Downloads
On Read the Docs
Project Home
Builds

Free document hosting provided by Read the Docs.