2023-09-05 23:34:23 +02:00
|
|
|
from collections.abc import Callable
|
|
|
|
from typing import Any
|
2021-08-30 10:35:02 -04:00
|
|
|
|
2024-03-11 10:29:17 +01:00
|
|
|
import numpy as np
|
|
|
|
from matplotlib.figure import Figure
|
2021-08-30 10:35:02 -04:00
|
|
|
from tensorboard.backend.event_processing import event_accumulator
|
2021-09-03 05:05:04 +08:00
|
|
|
from torch.utils.tensorboard import SummaryWriter
|
2021-08-30 10:35:02 -04:00
|
|
|
|
2024-03-12 15:01:50 +01:00
|
|
|
from tianshou.utils.logger.base import VALID_LOG_VALS, VALID_LOG_VALS_TYPE, BaseLogger
|
2022-03-21 16:29:27 -04:00
|
|
|
from tianshou.utils.warning import deprecation
|
2021-08-30 10:35:02 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TensorboardLogger(BaseLogger):
|
2023-08-25 23:40:56 +02:00
|
|
|
"""A logger that relies on tensorboard SummaryWriter by default to visualize and log statistics.
|
2021-08-30 10:35:02 -04:00
|
|
|
|
|
|
|
:param SummaryWriter writer: the writer to log data.
|
2023-10-08 17:57:03 +02:00
|
|
|
:param train_interval: the log interval in log_train_data(). Default to 1000.
|
|
|
|
:param test_interval: the log interval in log_test_data(). Default to 1.
|
|
|
|
:param update_interval: the log interval in log_update_data(). Default to 1000.
|
Feature/dataclasses (#996)
This PR adds strict typing to the output of `update` and `learn` in all
policies. This will likely be the last large refactoring PR before the
next release (0.6.0, not 1.0.0), so it requires some attention. Several
difficulties were encountered on the path to that goal:
1. The policy hierarchy is actually "broken" in the sense that the keys
of dicts that were output by `learn` did not follow the same enhancement
(inheritance) pattern as the policies. This is a real problem and should
be addressed in the near future. Generally, several aspects of the
policy design and hierarchy might deserve a dedicated discussion.
2. Each policy needs to be generic in the stats return type, because one
might want to extend it at some point and then also extend the stats.
Even within the source code base this pattern is necessary in many
places.
3. The interaction between learn and update is a bit quirky, we
currently handle it by having update modify special field inside
TrainingStats, whereas all other fields are handled by learn.
4. The IQM module is a policy wrapper and required a
TrainingStatsWrapper. The latter relies on a bunch of black magic.
They were addressed by:
1. Live with the broken hierarchy, which is now made visible by bounds
in generics. We use type: ignore where appropriate.
2. Make all policies generic with bounds following the policy
inheritance hierarchy (which is incorrect, see above). We experimented a
bit with nested TrainingStats classes, but that seemed to add more
complexity and be harder to understand. Unfortunately, mypy thinks that
the code below is wrong, wherefore we have to add `type: ignore` to the
return of each `learn`
```python
T = TypeVar("T", bound=int)
def f() -> T:
return 3
```
3. See above
4. Write representative tests for the `TrainingStatsWrapper`. Still, the
black magic might cause nasty surprises down the line (I am not proud of
it)...
Closes #933
---------
Co-authored-by: Maximilian Huettenrauch <m.huettenrauch@appliedai.de>
Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2023-12-30 11:09:03 +01:00
|
|
|
:param info_interval: the log interval in log_info_data(). Default to 1.
|
2023-10-08 17:57:03 +02:00
|
|
|
:param save_interval: the save interval in save_data(). Default to 1 (save at
|
2021-08-30 10:35:02 -04:00
|
|
|
the end of each epoch).
|
2023-10-08 17:57:03 +02:00
|
|
|
:param write_flush: whether to flush tensorboard result after each
|
2022-03-29 20:04:23 -04:00
|
|
|
add_scalar operation. Default to True.
|
2021-08-30 10:35:02 -04:00
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(
|
|
|
|
self,
|
|
|
|
writer: SummaryWriter,
|
|
|
|
train_interval: int = 1000,
|
|
|
|
test_interval: int = 1,
|
|
|
|
update_interval: int = 1000,
|
Feature/dataclasses (#996)
This PR adds strict typing to the output of `update` and `learn` in all
policies. This will likely be the last large refactoring PR before the
next release (0.6.0, not 1.0.0), so it requires some attention. Several
difficulties were encountered on the path to that goal:
1. The policy hierarchy is actually "broken" in the sense that the keys
of dicts that were output by `learn` did not follow the same enhancement
(inheritance) pattern as the policies. This is a real problem and should
be addressed in the near future. Generally, several aspects of the
policy design and hierarchy might deserve a dedicated discussion.
2. Each policy needs to be generic in the stats return type, because one
might want to extend it at some point and then also extend the stats.
Even within the source code base this pattern is necessary in many
places.
3. The interaction between learn and update is a bit quirky, we
currently handle it by having update modify special field inside
TrainingStats, whereas all other fields are handled by learn.
4. The IQM module is a policy wrapper and required a
TrainingStatsWrapper. The latter relies on a bunch of black magic.
They were addressed by:
1. Live with the broken hierarchy, which is now made visible by bounds
in generics. We use type: ignore where appropriate.
2. Make all policies generic with bounds following the policy
inheritance hierarchy (which is incorrect, see above). We experimented a
bit with nested TrainingStats classes, but that seemed to add more
complexity and be harder to understand. Unfortunately, mypy thinks that
the code below is wrong, wherefore we have to add `type: ignore` to the
return of each `learn`
```python
T = TypeVar("T", bound=int)
def f() -> T:
return 3
```
3. See above
4. Write representative tests for the `TrainingStatsWrapper`. Still, the
black magic might cause nasty surprises down the line (I am not proud of
it)...
Closes #933
---------
Co-authored-by: Maximilian Huettenrauch <m.huettenrauch@appliedai.de>
Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2023-12-30 11:09:03 +01:00
|
|
|
info_interval: int = 1,
|
2021-08-30 10:35:02 -04:00
|
|
|
save_interval: int = 1,
|
2022-03-29 20:04:23 -04:00
|
|
|
write_flush: bool = True,
|
2021-08-30 10:35:02 -04:00
|
|
|
) -> None:
|
Feature/dataclasses (#996)
This PR adds strict typing to the output of `update` and `learn` in all
policies. This will likely be the last large refactoring PR before the
next release (0.6.0, not 1.0.0), so it requires some attention. Several
difficulties were encountered on the path to that goal:
1. The policy hierarchy is actually "broken" in the sense that the keys
of dicts that were output by `learn` did not follow the same enhancement
(inheritance) pattern as the policies. This is a real problem and should
be addressed in the near future. Generally, several aspects of the
policy design and hierarchy might deserve a dedicated discussion.
2. Each policy needs to be generic in the stats return type, because one
might want to extend it at some point and then also extend the stats.
Even within the source code base this pattern is necessary in many
places.
3. The interaction between learn and update is a bit quirky, we
currently handle it by having update modify special field inside
TrainingStats, whereas all other fields are handled by learn.
4. The IQM module is a policy wrapper and required a
TrainingStatsWrapper. The latter relies on a bunch of black magic.
They were addressed by:
1. Live with the broken hierarchy, which is now made visible by bounds
in generics. We use type: ignore where appropriate.
2. Make all policies generic with bounds following the policy
inheritance hierarchy (which is incorrect, see above). We experimented a
bit with nested TrainingStats classes, but that seemed to add more
complexity and be harder to understand. Unfortunately, mypy thinks that
the code below is wrong, wherefore we have to add `type: ignore` to the
return of each `learn`
```python
T = TypeVar("T", bound=int)
def f() -> T:
return 3
```
3. See above
4. Write representative tests for the `TrainingStatsWrapper`. Still, the
black magic might cause nasty surprises down the line (I am not proud of
it)...
Closes #933
---------
Co-authored-by: Maximilian Huettenrauch <m.huettenrauch@appliedai.de>
Co-authored-by: Michael Panchenko <m.panchenko@appliedai.de>
2023-12-30 11:09:03 +01:00
|
|
|
super().__init__(train_interval, test_interval, update_interval, info_interval)
|
2021-08-30 10:35:02 -04:00
|
|
|
self.save_interval = save_interval
|
2022-03-29 20:04:23 -04:00
|
|
|
self.write_flush = write_flush
|
2021-08-30 10:35:02 -04:00
|
|
|
self.last_save_step = -1
|
|
|
|
self.writer = writer
|
|
|
|
|
2024-03-11 10:29:17 +01:00
|
|
|
def prepare_dict_for_logging(
|
2024-03-12 15:17:33 +01:00
|
|
|
self,
|
2024-03-11 10:29:17 +01:00
|
|
|
input_dict: dict[str, Any],
|
|
|
|
parent_key: str = "",
|
|
|
|
delimiter: str = "/",
|
|
|
|
exclude_arrays: bool = True,
|
|
|
|
) -> dict[str, VALID_LOG_VALS_TYPE]:
|
|
|
|
"""Flattens and filters a nested dictionary by recursively traversing all levels and compressing the keys.
|
|
|
|
|
|
|
|
Filtering is performed with respect to valid logging data types.
|
|
|
|
|
|
|
|
:param input_dict: The nested dictionary to be flattened and filtered.
|
|
|
|
:param parent_key: The parent key used as a prefix before the input_dict keys.
|
|
|
|
:param delimiter: The delimiter used to separate the keys.
|
|
|
|
:param exclude_arrays: Whether to exclude numpy arrays from the output.
|
|
|
|
:return: A flattened dictionary where the keys are compressed and values are filtered.
|
|
|
|
"""
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
def add_to_result(
|
|
|
|
cur_dict: dict,
|
|
|
|
prefix: str = "",
|
|
|
|
) -> None:
|
|
|
|
for key, value in cur_dict.items():
|
|
|
|
if exclude_arrays and isinstance(value, np.ndarray):
|
|
|
|
continue
|
|
|
|
|
|
|
|
new_key = prefix + delimiter + key
|
|
|
|
new_key = new_key.lstrip(delimiter)
|
|
|
|
|
|
|
|
if isinstance(value, dict):
|
|
|
|
add_to_result(
|
|
|
|
value,
|
|
|
|
new_key,
|
|
|
|
)
|
|
|
|
elif isinstance(value, VALID_LOG_VALS):
|
|
|
|
result[new_key] = value
|
|
|
|
|
|
|
|
add_to_result(input_dict, prefix=parent_key)
|
|
|
|
return result
|
|
|
|
|
|
|
|
def write(self, step_type: str, step: int, data: dict[str, Any]) -> None:
|
|
|
|
scope, step_name = step_type.split("/")
|
2024-03-12 11:31:41 +01:00
|
|
|
self.writer.add_scalar(step_type, step, global_step=step)
|
2021-08-30 10:35:02 -04:00
|
|
|
for k, v in data.items():
|
2024-03-12 15:01:50 +01:00
|
|
|
scope_key = f"{scope}/{k}"
|
2024-03-11 10:29:17 +01:00
|
|
|
if isinstance(v, np.ndarray):
|
|
|
|
self.writer.add_histogram(scope_key, v, global_step=step, bins="auto")
|
|
|
|
elif isinstance(v, Figure):
|
|
|
|
self.writer.add_figure(scope_key, v, global_step=step)
|
|
|
|
else:
|
|
|
|
self.writer.add_scalar(scope_key, v, global_step=step)
|
2022-03-29 20:04:23 -04:00
|
|
|
if self.write_flush: # issue 580
|
|
|
|
self.writer.flush() # issue #482
|
2021-08-30 10:35:02 -04:00
|
|
|
|
|
|
|
def save_data(
|
|
|
|
self,
|
|
|
|
epoch: int,
|
|
|
|
env_step: int,
|
|
|
|
gradient_step: int,
|
2023-09-05 23:34:23 +02:00
|
|
|
save_checkpoint_fn: Callable[[int, int, int], str] | None = None,
|
2021-08-30 10:35:02 -04:00
|
|
|
) -> None:
|
|
|
|
if save_checkpoint_fn and epoch - self.last_save_step >= self.save_interval:
|
|
|
|
self.last_save_step = epoch
|
|
|
|
save_checkpoint_fn(epoch, env_step, gradient_step)
|
|
|
|
self.write("save/epoch", epoch, {"save/epoch": epoch})
|
|
|
|
self.write("save/env_step", env_step, {"save/env_step": env_step})
|
2021-09-03 05:05:04 +08:00
|
|
|
self.write(
|
2023-08-25 23:40:56 +02:00
|
|
|
"save/gradient_step",
|
|
|
|
gradient_step,
|
|
|
|
{"save/gradient_step": gradient_step},
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-08-30 10:35:02 -04:00
|
|
|
|
2023-08-25 23:40:56 +02:00
|
|
|
def restore_data(self) -> tuple[int, int, int]:
|
2021-08-30 10:35:02 -04:00
|
|
|
ea = event_accumulator.EventAccumulator(self.writer.log_dir)
|
|
|
|
ea.Reload()
|
|
|
|
|
|
|
|
try: # epoch / gradient_step
|
|
|
|
epoch = ea.scalars.Items("save/epoch")[-1].step
|
|
|
|
self.last_save_step = self.last_log_test_step = epoch
|
|
|
|
gradient_step = ea.scalars.Items("save/gradient_step")[-1].step
|
|
|
|
self.last_log_update_step = gradient_step
|
|
|
|
except KeyError:
|
|
|
|
epoch, gradient_step = 0, 0
|
|
|
|
try: # offline trainer doesn't have env_step
|
|
|
|
env_step = ea.scalars.Items("save/env_step")[-1].step
|
|
|
|
self.last_log_train_step = env_step
|
|
|
|
except KeyError:
|
|
|
|
env_step = 0
|
|
|
|
|
|
|
|
return epoch, env_step, gradient_step
|
|
|
|
|
2024-03-12 15:01:50 +01:00
|
|
|
def restore_logged_data(self, log_path: str) -> dict[str, Any]:
|
2024-03-11 10:29:17 +01:00
|
|
|
ea = event_accumulator.EventAccumulator(log_path)
|
|
|
|
ea.Reload()
|
|
|
|
|
2024-03-12 15:01:50 +01:00
|
|
|
def add_to_dict(data_dict: dict[str, Any], keys: list[str], value: Any) -> None:
|
|
|
|
current_dict = data_dict
|
2024-03-11 10:29:17 +01:00
|
|
|
for key in keys[:-1]:
|
|
|
|
current_dict = current_dict.setdefault(key, {})
|
|
|
|
current_dict[keys[-1]] = value
|
|
|
|
|
2024-03-12 15:01:50 +01:00
|
|
|
data: dict[str, Any] = {}
|
2024-03-11 10:29:17 +01:00
|
|
|
for key in ea.scalars.Keys():
|
2024-03-12 15:01:50 +01:00
|
|
|
split_keys = key.split("/")
|
2024-03-11 10:29:17 +01:00
|
|
|
add_to_dict(data, split_keys, np.array([s.value for s in ea.scalars.Items(key)]))
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
2021-08-30 10:35:02 -04:00
|
|
|
|
|
|
|
class BasicLogger(TensorboardLogger):
|
|
|
|
"""BasicLogger has changed its name to TensorboardLogger in #427.
|
|
|
|
|
|
|
|
This class is for compatibility.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, *args: Any, **kwargs: Any) -> None:
|
2022-03-21 16:29:27 -04:00
|
|
|
deprecation(
|
|
|
|
"Class BasicLogger is marked as deprecated and will be removed soon. "
|
2023-08-25 23:40:56 +02:00
|
|
|
"Please use TensorboardLogger instead.",
|
2021-09-03 05:05:04 +08:00
|
|
|
)
|
2021-08-30 10:35:02 -04:00
|
|
|
super().__init__(*args, **kwargs)
|