2020-08-20 02:03:22 +00:00
|
|
|
# Copyright The PyTorch Lightning team.
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
2020-11-13 15:05:54 +00:00
|
|
|
"""Abstract base class used to build new loggers."""
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
import argparse
|
2020-04-08 12:35:47 +00:00
|
|
|
import functools
|
|
|
|
import operator
|
2020-02-25 19:52:39 +00:00
|
|
|
from abc import ABC, abstractmethod
|
2020-03-04 14:33:39 +00:00
|
|
|
from argparse import Namespace
|
2020-06-30 22:09:16 +00:00
|
|
|
from functools import wraps
|
2022-02-02 22:29:01 +00:00
|
|
|
from typing import Any, Callable, Dict, Iterable, List, Mapping, Optional, Sequence, Tuple, Union
|
2021-05-27 18:15:02 +00:00
|
|
|
from weakref import ReferenceType
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-04-08 12:35:47 +00:00
|
|
|
import numpy as np
|
2020-03-14 17:02:05 +00:00
|
|
|
|
2021-06-25 19:16:11 +00:00
|
|
|
import pytorch_lightning as pl
|
2021-05-27 18:15:02 +00:00
|
|
|
from pytorch_lightning.callbacks.model_checkpoint import ModelCheckpoint
|
2022-02-07 08:09:55 +00:00
|
|
|
from pytorch_lightning.utilities.rank_zero import rank_zero_deprecation, rank_zero_only
|
2020-06-30 22:09:16 +00:00
|
|
|
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-12-01 06:05:00 +00:00
|
|
|
def rank_zero_experiment(fn: Callable) -> Callable:
|
2021-07-26 11:37:35 +00:00
|
|
|
"""Returns the real experiment on rank 0 and otherwise the DummyExperiment."""
|
2021-02-08 19:28:38 +00:00
|
|
|
|
2020-12-01 06:05:00 +00:00
|
|
|
@wraps(fn)
|
|
|
|
def experiment(self):
|
|
|
|
@rank_zero_only
|
|
|
|
def get_experiment():
|
|
|
|
return fn(self)
|
2021-02-08 19:28:38 +00:00
|
|
|
|
2020-12-01 06:05:00 +00:00
|
|
|
return get_experiment() or DummyExperiment()
|
2021-02-08 19:28:38 +00:00
|
|
|
|
2020-12-01 06:05:00 +00:00
|
|
|
return experiment
|
|
|
|
|
|
|
|
|
2019-12-08 00:25:12 +00:00
|
|
|
class LightningLoggerBase(ABC):
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Base class for experiment loggers.
|
2020-04-16 16:04:12 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
agg_key_funcs:
|
|
|
|
Dictionary which maps a metric name to a function, which will
|
|
|
|
aggregate the metric values for the same steps.
|
|
|
|
agg_default_func:
|
|
|
|
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 :meth:`~LightningLoggerBase.agg_and_log_metrics` method.
|
|
|
|
"""
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-04-08 12:35:47 +00:00
|
|
|
def __init__(
|
2021-02-08 19:28:38 +00:00
|
|
|
self,
|
|
|
|
agg_key_funcs: Optional[Mapping[str, Callable[[Sequence[float]], float]]] = None,
|
2021-07-26 11:37:35 +00:00
|
|
|
agg_default_func: Callable[[Sequence[float]], float] = np.mean,
|
2020-04-08 12:35:47 +00:00
|
|
|
):
|
2020-04-15 00:32:33 +00:00
|
|
|
self._prev_step: int = -1
|
2020-04-08 12:35:47 +00:00
|
|
|
self._metrics_to_agg: List[Dict[str, float]] = []
|
|
|
|
self._agg_key_funcs = agg_key_funcs if agg_key_funcs else {}
|
|
|
|
self._agg_default_func = agg_default_func
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def after_save_checkpoint(self, checkpoint_callback: "ReferenceType[ModelCheckpoint]") -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Called after model checkpoint callback saves a new checkpoint.
|
2021-05-27 18:15:02 +00:00
|
|
|
|
|
|
|
Args:
|
2021-08-05 15:05:12 +00:00
|
|
|
checkpoint_callback: the model checkpoint callback instance
|
2021-05-27 18:15:02 +00:00
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2020-04-08 12:35:47 +00:00
|
|
|
def update_agg_funcs(
|
2021-02-08 19:28:38 +00:00
|
|
|
self,
|
|
|
|
agg_key_funcs: Optional[Mapping[str, Callable[[Sequence[float]], float]]] = None,
|
2021-07-26 11:37:35 +00:00
|
|
|
agg_default_func: Callable[[Sequence[float]], float] = np.mean,
|
2020-04-08 12:35:47 +00:00
|
|
|
):
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Update aggregation methods.
|
2020-04-08 12:35:47 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
agg_key_funcs:
|
|
|
|
Dictionary which maps a metric name to a function, which will
|
|
|
|
aggregate the metric values for the same steps.
|
|
|
|
agg_default_func:
|
|
|
|
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.
|
|
|
|
"""
|
|
|
|
if agg_key_funcs:
|
|
|
|
self._agg_key_funcs.update(agg_key_funcs)
|
|
|
|
if agg_default_func:
|
|
|
|
self._agg_default_func = agg_default_func
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def _aggregate_metrics(
|
|
|
|
self, metrics: Dict[str, float], step: Optional[int] = None
|
|
|
|
) -> Tuple[int, Optional[Dict[str, float]]]:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Aggregates metrics.
|
2020-04-08 12:35:47 +00:00
|
|
|
|
2022-02-18 02:54:33 +00:00
|
|
|
.. deprecated:: v1.6
|
|
|
|
This method is deprecated in v1.6 and will be removed in v1.8.
|
|
|
|
|
2020-04-08 12:35:47 +00:00
|
|
|
Args:
|
|
|
|
metrics: Dictionary with metric names as keys and measured quantities as values
|
|
|
|
step: Step number at which the metrics should be recorded
|
|
|
|
|
|
|
|
Returns:
|
2020-04-16 16:04:12 +00:00
|
|
|
Step and aggregated metrics. The return value could be ``None``. In such case, metrics
|
2020-04-08 12:35:47 +00:00
|
|
|
are added to the aggregation list, but not aggregated yet.
|
|
|
|
"""
|
|
|
|
# if you still receiving metric from the same step, just accumulate it
|
|
|
|
if step == self._prev_step:
|
|
|
|
self._metrics_to_agg.append(metrics)
|
|
|
|
return step, None
|
|
|
|
|
|
|
|
# compute the metrics
|
2020-04-15 00:32:33 +00:00
|
|
|
agg_step, agg_mets = self._reduce_agg_metrics()
|
2020-04-08 12:35:47 +00:00
|
|
|
|
|
|
|
# as new step received reset accumulator
|
|
|
|
self._metrics_to_agg = [metrics]
|
|
|
|
self._prev_step = step
|
|
|
|
return agg_step, agg_mets
|
|
|
|
|
2020-04-15 00:32:33 +00:00
|
|
|
def _reduce_agg_metrics(self):
|
2022-02-18 02:54:33 +00:00
|
|
|
"""Aggregate accumulated metrics.
|
|
|
|
|
|
|
|
See deprecation warning below.
|
|
|
|
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This method is deprecated in v1.6 and will be removed in v1.8.
|
|
|
|
"""
|
2020-04-08 12:35:47 +00:00
|
|
|
# compute the metrics
|
|
|
|
if not self._metrics_to_agg:
|
|
|
|
agg_mets = None
|
|
|
|
elif len(self._metrics_to_agg) == 1:
|
|
|
|
agg_mets = self._metrics_to_agg[0]
|
|
|
|
else:
|
|
|
|
agg_mets = merge_dicts(self._metrics_to_agg, self._agg_key_funcs, self._agg_default_func)
|
|
|
|
return self._prev_step, agg_mets
|
|
|
|
|
2020-04-15 00:32:33 +00:00
|
|
|
def _finalize_agg_metrics(self):
|
2022-02-18 02:54:33 +00:00
|
|
|
"""This shall be called before save/close.
|
|
|
|
|
|
|
|
See deprecation warning below.
|
|
|
|
|
|
|
|
.. deprecated:: v1.6
|
|
|
|
This method is deprecated in v1.6 and will be removed in v1.8.
|
|
|
|
"""
|
2020-04-15 00:32:33 +00:00
|
|
|
agg_step, metrics_to_log = self._reduce_agg_metrics()
|
|
|
|
self._metrics_to_agg = []
|
|
|
|
|
|
|
|
if metrics_to_log is not None:
|
|
|
|
self.log_metrics(metrics=metrics_to_log, step=agg_step)
|
|
|
|
|
2020-04-08 12:35:47 +00:00
|
|
|
def agg_and_log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None):
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Aggregates and records metrics. This method doesn't log the passed metrics instantaneously, but instead
|
2020-04-08 12:35:47 +00:00
|
|
|
it aggregates them and logs only if metrics are ready to be logged.
|
|
|
|
|
2022-02-18 02:54:33 +00:00
|
|
|
.. deprecated:: v1.6
|
|
|
|
This method is deprecated in v1.6 and will be removed in v1.8.
|
|
|
|
Please use `LightningLoggerBase.log_metrics` instead.
|
|
|
|
|
2020-04-08 12:35:47 +00:00
|
|
|
Args:
|
|
|
|
metrics: Dictionary with metric names as keys and measured quantities as values
|
|
|
|
step: Step number at which the metrics should be recorded
|
|
|
|
"""
|
|
|
|
agg_step, metrics_to_log = self._aggregate_metrics(metrics=metrics, step=step)
|
|
|
|
|
2020-05-02 12:50:47 +00:00
|
|
|
if metrics_to_log:
|
2020-04-08 12:35:47 +00:00
|
|
|
self.log_metrics(metrics=metrics_to_log, step=agg_step)
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
@abstractmethod
|
|
|
|
def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None):
|
2020-04-16 16:04:12 +00:00
|
|
|
"""
|
|
|
|
Records metrics.
|
2020-04-08 12:35:47 +00:00
|
|
|
This method logs metrics as as soon as it received them. If you want to aggregate
|
2020-04-16 16:04:12 +00:00
|
|
|
metrics for one specific `step`, use the
|
|
|
|
:meth:`~pytorch_lightning.loggers.base.LightningLoggerBase.agg_and_log_metrics` method.
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
Args:
|
|
|
|
metrics: Dictionary with metric names as keys and measured quantities as values
|
|
|
|
step: Step number at which the metrics should be recorded
|
2019-09-27 16:05:29 +00:00
|
|
|
"""
|
2020-04-08 12:35:47 +00:00
|
|
|
pass
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
@abstractmethod
|
2021-03-09 23:18:38 +00:00
|
|
|
def log_hyperparams(self, params: argparse.Namespace, *args, **kwargs):
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Record hyperparameters.
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
Args:
|
2020-04-16 16:04:12 +00:00
|
|
|
params: :class:`~argparse.Namespace` containing the hyperparameters
|
2021-03-09 23:18:38 +00:00
|
|
|
args: Optional positional arguments, depends on the specific logger being used
|
|
|
|
kwargs: Optional keywoard arguments, depends on the specific logger being used
|
2019-09-27 16:05:29 +00:00
|
|
|
"""
|
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def log_graph(self, model: "pl.LightningModule", input_array=None) -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Record model graph.
|
2020-08-19 23:08:46 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
model: lightning model
|
|
|
|
input_array: input passes to `model.forward`
|
|
|
|
"""
|
|
|
|
pass
|
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def save(self) -> None:
|
2019-12-08 00:25:12 +00:00
|
|
|
"""Save log data."""
|
2020-04-15 00:32:33 +00:00
|
|
|
self._finalize_agg_metrics()
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def finalize(self, status: str) -> None:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Do any processing that is necessary to finalize an experiment.
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
Args:
|
|
|
|
status: Status that the experiment finished with (e.g. success, failed, aborted)
|
2019-09-27 16:05:29 +00:00
|
|
|
"""
|
2020-04-15 00:32:33 +00:00
|
|
|
self.save()
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def close(self) -> None:
|
2021-09-20 22:00:09 +00:00
|
|
|
"""Do any cleanup that is necessary to close an experiment.
|
|
|
|
|
|
|
|
See deprecation warning below.
|
|
|
|
|
|
|
|
.. deprecated:: v1.5
|
|
|
|
This method is deprecated in v1.5 and will be removed in v1.7.
|
|
|
|
Please use `LightningLoggerBase.finalize` instead.
|
|
|
|
"""
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"`LightningLoggerBase.close` method is deprecated in v1.5 and will be removed in v1.7."
|
|
|
|
" Please use `LightningLoggerBase.finalize` instead."
|
|
|
|
)
|
2020-04-15 00:32:33 +00:00
|
|
|
self.save()
|
2019-09-27 16:05:29 +00:00
|
|
|
|
2020-07-09 11:15:41 +00:00
|
|
|
@property
|
|
|
|
def save_dir(self) -> Optional[str]:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Return the root directory where experiment logs get saved, or `None` if the logger does not save data
|
|
|
|
locally."""
|
2020-07-09 11:15:41 +00:00
|
|
|
return None
|
|
|
|
|
2021-10-20 15:48:36 +00:00
|
|
|
@property
|
|
|
|
def group_separator(self):
|
|
|
|
"""Return the default separator used by the logger to group the data into subfolders."""
|
|
|
|
return "/"
|
|
|
|
|
2019-11-05 15:41:59 +00:00
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
@abstractmethod
|
|
|
|
def name(self) -> str:
|
2019-12-08 00:25:12 +00:00
|
|
|
"""Return the experiment name."""
|
2019-11-05 15:41:59 +00:00
|
|
|
|
2019-09-27 16:05:29 +00:00
|
|
|
@property
|
2020-02-25 19:52:39 +00:00
|
|
|
@abstractmethod
|
|
|
|
def version(self) -> Union[int, str]:
|
2019-12-08 00:25:12 +00:00
|
|
|
"""Return the experiment version."""
|
2020-02-25 19:52:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LoggerCollection(LightningLoggerBase):
|
2021-09-06 12:49:09 +00:00
|
|
|
"""The :class:`LoggerCollection` class is used to iterate all logging actions over the given `logger_iterable`.
|
2020-02-25 19:52:39 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
logger_iterable: An iterable collection of loggers
|
|
|
|
"""
|
2020-04-23 21:32:36 +00:00
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
def __init__(self, logger_iterable: Iterable[LightningLoggerBase]):
|
|
|
|
super().__init__()
|
|
|
|
self._logger_iterable = logger_iterable
|
|
|
|
|
2020-02-27 20:54:06 +00:00
|
|
|
def __getitem__(self, index: int) -> LightningLoggerBase:
|
2021-06-27 09:00:02 +00:00
|
|
|
return list(self._logger_iterable)[index]
|
2020-02-27 20:54:06 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def after_save_checkpoint(self, checkpoint_callback: "ReferenceType[ModelCheckpoint]") -> None:
|
2021-05-27 18:15:02 +00:00
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.after_save_checkpoint(checkpoint_callback)
|
|
|
|
|
2020-07-29 21:53:02 +00:00
|
|
|
def update_agg_funcs(
|
2021-02-08 19:28:38 +00:00
|
|
|
self,
|
|
|
|
agg_key_funcs: Optional[Mapping[str, Callable[[Sequence[float]], float]]] = None,
|
2021-07-26 11:37:35 +00:00
|
|
|
agg_default_func: Callable[[Sequence[float]], float] = np.mean,
|
2020-07-29 21:53:02 +00:00
|
|
|
):
|
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.update_agg_funcs(agg_key_funcs, agg_default_func)
|
|
|
|
|
2020-02-25 19:52:39 +00:00
|
|
|
@property
|
|
|
|
def experiment(self) -> List[Any]:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Returns a list of experiment objects for all the loggers in the logger collection."""
|
2020-03-03 01:49:14 +00:00
|
|
|
return [logger.experiment for logger in self._logger_iterable]
|
2020-02-25 19:52:39 +00:00
|
|
|
|
2020-07-29 21:53:02 +00:00
|
|
|
def agg_and_log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None):
|
|
|
|
for logger in self._logger_iterable:
|
2022-02-18 02:54:33 +00:00
|
|
|
logger.agg_and_log_metrics(metrics=metrics, step=step)
|
2020-07-29 21:53:02 +00:00
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def log_metrics(self, metrics: Dict[str, float], step: Optional[int] = None) -> None:
|
2020-07-29 21:53:02 +00:00
|
|
|
for logger in self._logger_iterable:
|
2022-02-18 02:54:33 +00:00
|
|
|
logger.log_metrics(metrics=metrics, step=step)
|
2020-02-25 19:52:39 +00:00
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def log_hyperparams(self, params: Union[Dict[str, Any], Namespace]) -> None:
|
2020-07-29 21:53:02 +00:00
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.log_hyperparams(params)
|
2020-02-25 19:52:39 +00:00
|
|
|
|
2021-07-26 11:37:35 +00:00
|
|
|
def log_graph(self, model: "pl.LightningModule", input_array=None) -> None:
|
2020-08-19 23:08:46 +00:00
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.log_graph(model, input_array)
|
|
|
|
|
2021-10-11 09:15:36 +00:00
|
|
|
def log_text(self, *args, **kwargs) -> None:
|
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.log_text(*args, **kwargs)
|
|
|
|
|
|
|
|
def log_image(self, *args, **kwargs) -> None:
|
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.log_image(*args, **kwargs)
|
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def save(self) -> None:
|
2020-07-29 21:53:02 +00:00
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.save()
|
2020-02-25 19:52:39 +00:00
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def finalize(self, status: str) -> None:
|
2020-07-29 21:53:02 +00:00
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.finalize(status)
|
2020-02-25 19:52:39 +00:00
|
|
|
|
2020-03-04 14:33:39 +00:00
|
|
|
def close(self) -> None:
|
2021-09-20 22:00:09 +00:00
|
|
|
"""
|
|
|
|
.. deprecated:: v1.5
|
|
|
|
This method is deprecated in v1.5 and will be removed in v1.7.
|
|
|
|
Please use `LoggerCollection.finalize` instead.
|
|
|
|
"""
|
|
|
|
rank_zero_deprecation(
|
|
|
|
"`LoggerCollection.close` method is deprecated in v1.5 and will be removed in v1.7."
|
|
|
|
" Please use `LoggerCollection.finalize` instead."
|
|
|
|
)
|
2020-07-29 21:53:02 +00:00
|
|
|
for logger in self._logger_iterable:
|
|
|
|
logger.close()
|
|
|
|
|
|
|
|
@property
|
|
|
|
def save_dir(self) -> Optional[str]:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Returns ``None`` as checkpoints should be saved to default / chosen location when using multiple
|
|
|
|
loggers."""
|
2020-07-29 21:53:02 +00:00
|
|
|
# Checkpoints should be saved to default / chosen location when using multiple loggers
|
|
|
|
return None
|
2020-02-25 19:52:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def name(self) -> str:
|
2021-12-23 00:35:38 +00:00
|
|
|
"""Returns the unique experiment names for all the loggers in the logger collection joined by an
|
|
|
|
underscore."""
|
|
|
|
return "_".join(dict.fromkeys(str(logger.name) for logger in self._logger_iterable))
|
2020-02-25 19:52:39 +00:00
|
|
|
|
|
|
|
@property
|
|
|
|
def version(self) -> str:
|
2021-12-23 00:35:38 +00:00
|
|
|
"""Returns the unique experiment versions for all the loggers in the logger collection joined by an
|
|
|
|
underscore."""
|
|
|
|
return "_".join(dict.fromkeys(str(logger.version) for logger in self._logger_iterable))
|
2020-04-08 12:35:47 +00:00
|
|
|
|
|
|
|
|
2021-07-26 12:38:12 +00:00
|
|
|
class DummyExperiment:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Dummy experiment."""
|
2021-02-08 19:28:38 +00:00
|
|
|
|
2021-07-21 10:10:33 +00:00
|
|
|
def nop(self, *args, **kw):
|
2020-05-14 14:34:11 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
def __getattr__(self, _):
|
|
|
|
return self.nop
|
|
|
|
|
2021-03-09 23:18:38 +00:00
|
|
|
def __getitem__(self, idx) -> "DummyExperiment":
|
|
|
|
# enables self.logger.experiment[0].add_image(...)
|
2020-12-05 21:00:31 +00:00
|
|
|
return self
|
|
|
|
|
2021-12-03 17:54:05 +00:00
|
|
|
def __setitem__(self, *args, **kwargs) -> None:
|
|
|
|
pass
|
|
|
|
|
2020-05-14 14:34:11 +00:00
|
|
|
|
|
|
|
class DummyLogger(LightningLoggerBase):
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Dummy logger for internal use.
|
|
|
|
|
|
|
|
It is useful if we want to disable user's logger for a feature, but still ensure that user code can run
|
2021-03-09 23:18:38 +00:00
|
|
|
"""
|
2021-02-08 19:28:38 +00:00
|
|
|
|
2020-05-14 14:34:11 +00:00
|
|
|
def __init__(self):
|
|
|
|
super().__init__()
|
|
|
|
self._experiment = DummyExperiment()
|
|
|
|
|
|
|
|
@property
|
2021-03-09 23:18:38 +00:00
|
|
|
def experiment(self) -> DummyExperiment:
|
2021-09-02 22:55:12 +00:00
|
|
|
"""Return the experiment object associated with this logger."""
|
2020-05-14 14:34:11 +00:00
|
|
|
return self._experiment
|
|
|
|
|
2021-03-09 23:18:38 +00:00
|
|
|
def log_metrics(self, *args, **kwargs) -> None:
|
2020-05-14 14:34:11 +00:00
|
|
|
pass
|
|
|
|
|
2021-03-09 23:18:38 +00:00
|
|
|
def log_hyperparams(self, *args, **kwargs) -> None:
|
2020-05-14 14:34:11 +00:00
|
|
|
pass
|
|
|
|
|
|
|
|
@property
|
2021-03-09 23:18:38 +00:00
|
|
|
def name(self) -> str:
|
2021-09-02 22:55:12 +00:00
|
|
|
"""Return the experiment name."""
|
2021-03-09 23:18:38 +00:00
|
|
|
return ""
|
2020-05-14 14:34:11 +00:00
|
|
|
|
|
|
|
@property
|
2021-03-09 23:18:38 +00:00
|
|
|
def version(self) -> str:
|
2021-09-02 22:55:12 +00:00
|
|
|
"""Return the experiment version."""
|
2021-03-09 23:18:38 +00:00
|
|
|
return ""
|
2020-05-14 14:34:11 +00:00
|
|
|
|
2021-03-09 23:18:38 +00:00
|
|
|
def __getitem__(self, idx) -> "DummyLogger":
|
|
|
|
# enables self.logger[0].experiment.add_image(...)
|
2020-12-05 21:00:31 +00:00
|
|
|
return self
|
|
|
|
|
2021-10-29 07:22:59 +00:00
|
|
|
def __iter__(self):
|
|
|
|
# if DummyLogger is substituting a logger collection, pretend it is empty
|
|
|
|
yield from ()
|
|
|
|
|
2020-05-14 14:34:11 +00:00
|
|
|
|
2020-04-08 12:35:47 +00:00
|
|
|
def merge_dicts(
|
2021-02-08 19:28:38 +00:00
|
|
|
dicts: Sequence[Mapping],
|
|
|
|
agg_key_funcs: Optional[Mapping[str, Callable[[Sequence[float]], float]]] = None,
|
2021-07-26 11:37:35 +00:00
|
|
|
default_func: Callable[[Sequence[float]], float] = np.mean,
|
2020-04-08 12:35:47 +00:00
|
|
|
) -> Dict:
|
2021-09-06 12:49:09 +00:00
|
|
|
"""Merge a sequence with dictionaries into one dictionary by aggregating the same keys with some given
|
|
|
|
function.
|
2020-04-08 12:35:47 +00:00
|
|
|
|
|
|
|
Args:
|
|
|
|
dicts:
|
|
|
|
Sequence of dictionaries to be merged.
|
|
|
|
agg_key_funcs:
|
|
|
|
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
|
2020-04-16 16:04:12 +00:00
|
|
|
will be used. Default is: ``None`` (all keys will be aggregated by the
|
2020-04-08 12:35:47 +00:00
|
|
|
default function).
|
|
|
|
default_func:
|
|
|
|
Default function to aggregate keys, which are not presented in the
|
|
|
|
`agg_key_funcs` map.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
Dictionary with merged values.
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
>>> import pprint
|
2020-04-23 21:32:36 +00:00
|
|
|
>>> 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}}}
|
2020-04-08 12:35:47 +00:00
|
|
|
>>> dflt_func = min
|
2020-04-23 21:32:36 +00:00
|
|
|
>>> agg_funcs = {'a': np.mean, 'v': max, 'd': {'d1': sum}}
|
2020-04-08 12:35:47 +00:00
|
|
|
>>> pprint.pprint(merge_dicts([d1, d2, d3], agg_funcs, dflt_func))
|
2020-04-23 21:32:36 +00:00
|
|
|
{'a': 1.3,
|
|
|
|
'b': 2.0,
|
|
|
|
'c': 1,
|
|
|
|
'd': {'d1': 3, 'd2': 3, 'd3': 3, 'd4': {'d5': 1}},
|
|
|
|
'v': 2.3}
|
2020-04-08 12:35:47 +00:00
|
|
|
"""
|
2021-07-14 10:32:13 +00:00
|
|
|
agg_key_funcs = agg_key_funcs or {}
|
2020-04-08 12:35:47 +00:00
|
|
|
keys = list(functools.reduce(operator.or_, [set(d.keys()) for d in dicts]))
|
|
|
|
d_out = {}
|
|
|
|
for k in keys:
|
2020-04-23 21:32:36 +00:00
|
|
|
fn = agg_key_funcs.get(k)
|
|
|
|
values_to_agg = [v for v in [d_in.get(k) for d_in in dicts] if v is not None]
|
|
|
|
|
|
|
|
if isinstance(values_to_agg[0], dict):
|
|
|
|
d_out[k] = merge_dicts(values_to_agg, fn, default_func)
|
|
|
|
else:
|
|
|
|
d_out[k] = (fn or default_func)(values_to_agg)
|
2020-04-08 12:35:47 +00:00
|
|
|
|
|
|
|
return d_out
|