Skip to content

Commit

Permalink
Add header
Browse files Browse the repository at this point in the history
  • Loading branch information
takuseno committed Oct 16, 2024
1 parent 19064fa commit 8e6ec36
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions d3rlpy/logging/file_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,12 @@ class FileAdapter(LoggerAdapter):

_algo: AlgProtocol
_logdir: str
_is_model_watched: bool

def __init__(self, algo: AlgProtocol, logdir: str):
self._algo = algo
self._logdir = logdir
self._is_model_watched = False
if not os.path.exists(self._logdir):
os.makedirs(self._logdir)
LOG.info(f"Directory is created at {self._logdir}")
Expand Down Expand Up @@ -91,14 +93,29 @@ def watch_model(
step: int,
) -> None:
assert self._algo.impl

Check warning on line 95 in d3rlpy/logging/file_adapter.py

View check run for this annotation

Codecov / codecov/patch

d3rlpy/logging/file_adapter.py#L95

Added line #L95 was not covered by tests

# write header at the first call
if not self._is_model_watched:
self._is_model_watched = True
for name, grad in self._algo.impl.modules.get_gradients():

Check warning on line 100 in d3rlpy/logging/file_adapter.py

View check run for this annotation

Codecov / codecov/patch

d3rlpy/logging/file_adapter.py#L98-L100

Added lines #L98 - L100 were not covered by tests
path = os.path.join(self._logdir, f"{name}_grad.csv")
with open(path, "w") as f:

Check warning on line 102 in d3rlpy/logging/file_adapter.py

View check run for this annotation

Codecov / codecov/patch

d3rlpy/logging/file_adapter.py#L102

Added line #L102 was not covered by tests
print(
",".join(
["epoch", "step", "min", "max", "mean", "std"]
),
file=f,
)

for name, grad in self._algo.impl.modules.get_gradients():
path = os.path.join(self._logdir, f"{name}_grad.csv")
with open(path, "a") as f:
min_grad = grad.min()
max_grad = grad.max()
mean_grad = grad.mean()
mean = grad.mean()
std = grad.std()
print(

Check warning on line 117 in d3rlpy/logging/file_adapter.py

View check run for this annotation

Codecov / codecov/patch

d3rlpy/logging/file_adapter.py#L110-L117

Added lines #L110 - L117 were not covered by tests
f"{epoch},{step},{min_grad},{max_grad},{mean_grad}",
f"{epoch},{step},{min_grad},{max_grad},{mean},{std}",
file=f,
)

Expand Down

0 comments on commit 8e6ec36

Please sign in to comment.