From 7eeeb24087419575a3b38e95769f5f4f7cd38514 Mon Sep 17 00:00:00 2001 From: Acme Contributor Date: Mon, 17 Aug 2020 07:11:14 -0700 Subject: [PATCH] Add constant info logger. PiperOrigin-RevId: 327014404 Change-Id: I84884cc9019064d2bb2be8556d4c794d78e14f34 --- acme/utils/loggers/__init__.py | 1 + acme/utils/loggers/constant.py | 44 ++++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 acme/utils/loggers/constant.py diff --git a/acme/utils/loggers/__init__.py b/acme/utils/loggers/__init__.py index da162bb686..e0f5792bd1 100644 --- a/acme/utils/loggers/__init__.py +++ b/acme/utils/loggers/__init__.py @@ -21,6 +21,7 @@ from acme.utils.loggers.base import LoggingData from acme.utils.loggers.base import NoOpLogger from acme.utils.loggers.base import to_numpy +from acme.utils.loggers.constant import ConstantLogger from acme.utils.loggers.csv import CSVLogger from acme.utils.loggers.filters import NoneFilter from acme.utils.loggers.filters import TimeFilter diff --git a/acme/utils/loggers/constant.py b/acme/utils/loggers/constant.py new file mode 100644 index 0000000000..dafd98c752 --- /dev/null +++ b/acme/utils/loggers/constant.py @@ -0,0 +1,44 @@ +# Lint as: python3 +# Copyright 2018 DeepMind Technologies Limited. All rights reserved. +# +# 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. + +"""Acme specific utils.""" +from acme.utils.loggers import base + + +class ConstantLogger(base.Logger): + """Logger for values that remain constant throughout the experiment. + + This logger is used to log additional values e.g. level_name or + hyperparameters that do not change in an experiment. Having these values + allows to group or facet plots when analysing data post-experiment. + """ + + def __init__(self, + constant_data: base.LoggingData, + to: base.Logger): + """Initialise the extra info logger. + + Args: + constant_data: Dictionary containing the constant info to be logged. + to: The logger to add these extra info to. + """ + self._constant_data = constant_data + self._to = to + + def write(self, data: base.LoggingData): + # Need to create a new mutable dictionary that will be updated with the data + # we will be writing. base.LoggingData is immutable and hence cannot be + # updated. + self._to.write({**self._constant_data, **data})