-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathutil.py
More file actions
42 lines (40 loc) · 1.29 KB
/
Copy pathutil.py
File metadata and controls
42 lines (40 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
import numpy as np
from typing import Callable, Any
def log_and_save_progress(
step: int,
log_step: int,
episode_rewards: list[float],
episode_lengths: list[float],
best_reward: float,
pbar: Any,
eps: float,
save_callback: Callable[[], None],
) -> float:
"""
Periodically logs the best reward to a progress bar and saves the model.
Returns the updated best_reward.
"""
if step > 0 and step % log_step == 0:
if len(episode_rewards) >= 100:
recent_avg = np.mean(episode_rewards[-100:])
recent_len = np.mean(episode_lengths[-100:])
if recent_avg > best_reward:
best_reward = recent_avg
save_callback()
pbar.set_postfix(
{
"Avg Rwd (100)": f"{recent_avg:.2f}",
"Avg Len (100)": f"{recent_len:.2f}",
"Best": f"{best_reward:.2f}",
"Eps": f"{eps:.3f}",
}
)
elif len(episode_rewards) > 0:
pbar.set_postfix(
{
"Last Rwd": f"{episode_rewards[-1]:.2f}",
"Last Len": f"{episode_lengths[-1]:.2f}",
"Eps": f"{eps:.3f}",
}
)
return best_reward