Shortcuts

pytorch_lightning.loggers.base module

class pytorch_lightning.loggers.base.DummyExperiment[source]

Bases: object

Dummy experiment

nop(**kw)[source]
class pytorch_lightning.loggers.base.DummyLogger[source]

Bases: pytorch_lightning.loggers.base.LightningLoggerBase

Dummy logger for internal use. Is usefull if we want to disable users logger for a feature, but still secure that users code can run

log_hyperparams(params)[source]

Record hyperparameters.

Parameters

paramsNamespace containing the hyperparameters

log_metrics(metrics, step)[source]

Records metrics. This method logs metrics as as soon as it received them. If you want to aggregate metrics for one specific step, use the agg_and_log_metrics() method.

Parameters
  • metrics – Dictionary with metric names as keys and measured quantities as values

  • step – Step number at which the metrics should be recorded

property experiment[source]

Return the experiment object associated with this logger.

property name[source]

Return the experiment name.

property version[source]

Return the experiment version.

class pytorch_lightning.loggers.base.LightningLoggerBase(agg_key_funcs=None, agg_default_func=numpy.mean)[source]

Bases: abc.ABC

Base class for experiment loggers.

Parameters
  • agg_key_funcs (Optional[Mapping[str, Callable[[Sequence[float]], float]]]) – Dictionary which maps a metric name to a function, which will aggregate the metric values for the same steps.

  • agg_default_func (Callable[[Sequence[float]], float]) – Default function to aggregate metric values. If some metric name is not presented in the agg_key_funcs dictionary, then the agg_default_func will be used for aggregation.

Note

The agg_key_funcs and agg_default_func arguments are used only when one logs metrics with the agg_and_log_metrics() method.

_aggregate_metrics(metrics, step=None)[source]

Aggregates metrics.

Parameters
  • metrics (Dict[str, float]) – Dictionary with metric names as keys and measured quantities as values

  • step (Optional[int]) – Step number at which the metrics should be recorded

Return type

Tuple[int, Optional[Dict[str, float]]]

Returns

Step and aggregated metrics. The return value could be None. In such case, metrics are added to the aggregation list, but not aggregated yet.

static _convert_params(params)[source]
Return type

Dict[str, Any]

_finalize_agg_metrics()[source]

This shall be called before save/close.

static _flatten_dict(params, delimiter='/')[source]

Flatten hierarchical dict, e.g. {'a': {'b': 'c'}} -> {'a/b': 'c'}.

Parameters
  • params (Dict[str, Any]) – Dictionary containing the hyperparameters

  • delimiter (str) – Delimiter to express the hierarchy. Defaults to '/'.

Return type

Dict[str, Any]

Returns

Flattened dict.

Examples

>>> LightningLoggerBase._flatten_dict({'a': {'b': 'c'}})
{'a/b': 'c'}
>>> LightningLoggerBase._flatten_dict({'a': {'b': 123}})
{'a/b': 123}
_reduce_agg_metrics()[source]

Aggregate accumulated metrics.

static _sanitize_params(params)[source]

Returns params with non-primitvies converted to strings for logging.

>>> params = {"float": 0.3,
...           "int": 1,
...           "string": "abc",
...           "bool": True,
...           "list": [1, 2, 3],
...           "namespace": Namespace(foo=3),
...           "layer": torch.nn.BatchNorm1d}
>>> import pprint
>>> pprint.pprint(LightningLoggerBase._sanitize_params(params))  
{'bool': True,
 'float': 0.3,
 'int': 1,
 'layer': "<class 'torch.nn.modules.batchnorm.BatchNorm1d'>",
 'list': '[1, 2, 3]',
 'namespace': 'Namespace(foo=3)',
 'string': 'abc'}
Return type

Dict[str, Any]

agg_and_log_metrics(metrics, step=None)[source]

Aggregates and records metrics. This method doesn’t log the passed metrics instantaneously, but instead it aggregates them and logs only if metrics are ready to be logged.

Parameters
  • metrics (Dict[str, float]) – Dictionary with metric names as keys and measured quantities as values

  • step (Optional[int]) – Step number at which the metrics should be recorded

close()[source]

Do any cleanup that is necessary to close an experiment.

Return type

None

finalize(status)[source]

Do any processing that is necessary to finalize an experiment.

Parameters

status (str) – Status that the experiment finished with (e.g. success, failed, aborted)

Return type

None

abstract log_hyperparams(params)[source]

Record hyperparameters.

Parameters

params (Namespace) – Namespace containing the hyperparameters

abstract log_metrics(metrics, step=None)[source]

Records metrics. This method logs metrics as as soon as it received them. If you want to aggregate metrics for one specific step, use the agg_and_log_metrics() method.

Parameters
  • metrics (Dict[str, float]) – Dictionary with metric names as keys and measured quantities as values

  • step (Optional[int]) – Step number at which the metrics should be recorded

save()[source]

Save log data.

Return type

None

update_agg_funcs(agg_key_funcs=None, agg_default_func=numpy.mean)[source]

Update aggregation methods.

Parameters
  • agg_key_funcs (Optional[Mapping[str, Callable[[Sequence[float]], float]]]) – Dictionary which maps a metric name to a function, which will aggregate the metric values for the same steps.

  • agg_default_func (Callable[[Sequence[float]], float]) – Default function to aggregate metric values. If some metric name is not presented in the agg_key_funcs dictionary, then the agg_default_func will be used for aggregation.

abstract property experiment[source]

Return the experiment object associated with this logger.

Return type

Any

abstract property name[source]

Return the experiment name.

Return type

str

property save_dir[source]

Return the root directory where experiment logs get saved, or None if the logger does not save data locally.

Return type

Optional[str]

abstract property version[source]

Return the experiment version.

Return type

Union[int, str]

class pytorch_lightning.loggers.base.LoggerCollection(logger_iterable)[source]

Bases: pytorch_lightning.loggers.base.LightningLoggerBase

The LoggerCollection class is used to iterate all logging actions over the given logger_iterable.

Parameters

logger_iterable (Iterable[LightningLoggerBase]) – An iterable collection of loggers

close()[source]

Do any cleanup that is necessary to close an experiment.

Return type

None

finalize(status)[source]

Do any processing that is necessary to finalize an experiment.

Parameters

status (str) – Status that the experiment finished with (e.g. success, failed, aborted)

Return type

None

log_hyperparams(params)[source]

Record hyperparameters.

Parameters

params (Union[Dict[str, Any], Namespace]) – Namespace containing the hyperparameters

Return type

None

log_metrics(metrics, step=None)[source]

Records metrics. This method logs metrics as as soon as it received them. If you want to aggregate metrics for one specific step, use the agg_and_log_metrics() method.

Parameters
  • metrics (Dict[str, float]) – Dictionary with metric names as keys and measured quantities as values

  • step (Optional[int]) – Step number at which the metrics should be recorded

Return type

None

save()[source]

Save log data.

Return type

None

property experiment[source]

Return the experiment object associated with this logger.

Return type

List[Any]

property name[source]

Return the experiment name.

Return type

str

property version[source]

Return the experiment version.

Return type

str

pytorch_lightning.loggers.base.merge_dicts(dicts, agg_key_funcs=None, default_func=numpy.mean)[source]

Merge a sequence with dictionaries into one dictionary by aggregating the same keys with some given function.

Parameters
  • dicts (Sequence[Mapping]) – Sequence of dictionaries to be merged.

  • agg_key_funcs (Optional[Mapping[str, Callable[[Sequence[float]], float]]]) – Mapping from key name to function. This function will aggregate a list of values, obtained from the same key of all dictionaries. If some key has no specified aggregation function, the default one will be used. Default is: None (all keys will be aggregated by the default function).

  • default_func (Callable[[Sequence[float]], float]) – Default function to aggregate keys, which are not presented in the agg_key_funcs map.

Return type

Dict

Returns

Dictionary with merged values.

Examples

>>> import pprint
>>> d1 = {'a': 1.7, 'b': 2.0, 'c': 1, 'd': {'d1': 1, 'd3': 3}}
>>> d2 = {'a': 1.1, 'b': 2.2, 'v': 1, 'd': {'d1': 2, 'd2': 3}}
>>> d3 = {'a': 1.1, 'v': 2.3, 'd': {'d3': 3, 'd4': {'d5': 1}}}
>>> dflt_func = min
>>> agg_funcs = {'a': np.mean, 'v': max, 'd': {'d1': sum}}
>>> pprint.pprint(merge_dicts([d1, d2, d3], agg_funcs, dflt_func))
{'a': 1.3,
 'b': 2.0,
 'c': 1,
 'd': {'d1': 3, 'd2': 3, 'd3': 3, 'd4': {'d5': 1}},
 'v': 2.3}
pytorch_lightning.loggers.base.rank_zero_experiment(fn)[source]

Returns the real experiment on rank 0 and otherwise the DummyExperiment.

Return type

Callable