浏览代码

collecting latest step as a stat (#5264) (#5295)

* collecting latest step as a stat

* adding a list of hidden_keys to TB summarywriter to hide unnecessary stats from user

* fixing precommit

* formating

* defined the property types

* moving custom defaults to get_default_stats_writers

* new test for TensorboardWriter.hidden_keys

* improved testing

* explicit None evaluation

Co-authored-by: Ervin T. <ervin@unity3d.com>

* make hidden_keys optional

Co-authored-by: Ervin T. <ervin@unity3d.com>

* adding optional argument

* lowering the training threshold to 0.8 on test_var_len_obs_and_goal_poca

* Update pytest.yml

* Do not merge! droping pytest 3.9 job

* -add back pytest
-format imports and comments

* back to default threshold for test_var_len_obs_and_goal_poca

Co-authored-by: mahon94 <maryam.honari@unity3d.com>
Co-authored-by: Ervin T. <ervin@unity3d.com>

Co-authored-by: mahon94 <maryam.honari@unity3d.com>
Co-authored-by: Ervin...
/release_17_branch
GitHub 4 年前
当前提交
ae01cfc9
共有 4 个文件被更改,包括 39 次插入3 次删除
  1. 1
      ml-agents/mlagents/plugins/stats_writer.py
  2. 15
      ml-agents/mlagents/trainers/stats.py
  3. 25
      ml-agents/mlagents/trainers/tests/test_stats.py
  4. 1
      ml-agents/mlagents/trainers/trainer/rl_trainer.py

1
ml-agents/mlagents/plugins/stats_writer.py


TensorboardWriter(
checkpoint_settings.write_path,
clear_past_data=not checkpoint_settings.resume,
hidden_keys=["Is Training", "Step"],
),
GaugeWriter(),
ConsoleWriter(),

15
ml-agents/mlagents/trainers/stats.py


from collections import defaultdict
from enum import Enum
from typing import List, Dict, NamedTuple, Any
from typing import List, Dict, NamedTuple, Any, Optional
import numpy as np
import abc
import os

from mlagents_envs.timers import set_gauge
from torch.utils.tensorboard import SummaryWriter
from mlagents.torch_utils.globals import get_rank
logger = get_logger(__name__)

class TensorboardWriter(StatsWriter):
def __init__(self, base_dir: str, clear_past_data: bool = False):
def __init__(
self,
base_dir: str,
clear_past_data: bool = False,
hidden_keys: Optional[List[str]] = None,
):
"""
A StatsWriter that writes to a Tensorboard summary.

category.
:param hidden_keys: If provided, Tensorboard Writer won't write statistics identified with these Keys in
Tensorboard summary.
self.hidden_keys: List[str] = hidden_keys if hidden_keys is not None else []
def write_stats(
self, category: str, values: Dict[str, StatsSummary], step: int

if key in self.hidden_keys:
continue
self.summary_writers[category].add_scalar(
f"{key}", value.aggregated_value, step
)

25
ml-agents/mlagents/trainers/tests/test_stats.py


assert len(os.listdir(os.path.join(tmp_path, "category1"))) == 1
@mock.patch("mlagents.trainers.stats.SummaryWriter")
def test_tensorboard_writer_hidden_keys(mock_summary):
# Test write_stats
category = "category1"
with tempfile.TemporaryDirectory(prefix="unittest-") as base_dir:
tb_writer = TensorboardWriter(
base_dir, clear_past_data=False, hidden_keys="hiddenKey"
)
statssummary1 = StatsSummary(
full_dist=[1.0], aggregation_method=StatsAggregationMethod.AVERAGE
)
tb_writer.write_stats("category1", {"hiddenKey": statssummary1}, 10)
# Test that the filewriter has been created and the directory has been created.
filewriter_dir = "{basedir}/{category}".format(
basedir=base_dir, category=category
)
assert os.path.exists(filewriter_dir)
mock_summary.assert_called_once_with(filewriter_dir)
# Test that the filewriter was not written to since we used the hidden key.
mock_summary.return_value.add_scalar.assert_not_called()
mock_summary.return_value.flush.assert_not_called()
def test_gauge_stat_writer_sanitize():
assert GaugeWriter.sanitize_string("Policy/Learning Rate") == "Policy.LearningRate"
assert (

1
ml-agents/mlagents/trainers/trainer/rl_trainer.py


p = self.get_policy(name_behavior_id)
if p:
p.increment_step(n_steps)
self.stats_reporter.set_stat("Step", float(self.get_step))
def _get_next_interval_step(self, interval: int) -> int:
"""

正在加载...
取消
保存