-
Notifications
You must be signed in to change notification settings - Fork 1
/
callbacks.py
163 lines (129 loc) · 5.75 KB
/
callbacks.py
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
from stable_baselines3.common.callbacks import BaseCallback
import os
import numpy as np
from stable_baselines3.common.results_plotter import load_results, ts2xy
from stable_baselines3.common.logger import HParam
import matplotlib.pyplot as plt
class TensorboardCallback(BaseCallback):
"""
Custom callback for plotting additional values in tensorboard.
"""
def __init__(self, verbose=1):
super(TensorboardCallback, self).__init__(verbose)
def _on_step(self) -> bool:
self.logger.record('reward', self.training_env.get_attr('reward')[0])
self.logger.record('cum_reward', self.training_env.get_attr('episode_reward')[0])
self.logger.dump(self.num_timesteps)
return True
class SaveOnBestTrainingRewardCallback(BaseCallback):
"""
Callback for saving a model (the check is done every ``check_freq`` steps)
based on the training reward (in practice, we recommend using ``EvalCallback``).
:param check_freq: (int)
:param log_dir: (str) Path to the folder where the model will be saved.
It must contains the file created by the ``Monitor`` wrapper.
:param verbose: (int)
"""
def __init__(self, check_freq, log_dir, verbose=1):
super().__init__(verbose)
self.check_freq = check_freq
self.log_dir = log_dir
self.save_path = os.path.join(log_dir, "best_model")
self.best_mean_reward = -np.inf
def _init_callback(self) -> None:
# Create folder if needed
if self.save_path is not None:
os.makedirs(self.save_path, exist_ok=True)
def _on_step(self) -> bool:
if self.n_calls % self.check_freq == 0:
# Retrieve training reward
x, y = ts2xy(load_results(self.log_dir), "timesteps")
if len(x) > 0:
# Mean training reward over the last 100 episodes
mean_reward = np.mean(y[-100:])
if self.verbose > 0:
print("Num timesteps: {}".format(self.num_timesteps))
print(
"Best mean reward: {:.2f} - Last mean reward per episode: {:.2f}".format(
self.best_mean_reward, mean_reward
)
)
# New best model, you could save the agent here
if mean_reward > self.best_mean_reward:
self.best_mean_reward = mean_reward
# Example for saving best model
if self.verbose > 0:
print("Saving new best model at {} timesteps".format(x[-1]))
print("Saving new best model to {}.zip".format(self.save_path))
self.model.save(self.save_path)
return True
class HParamCallback(BaseCallback):
def __init__(self, verbose=1):
super().__init__(verbose)
"""
Saves the hyperparameters and metrics at the start of the training, and logs them to TensorBoard.
"""
def _on_training_start(self) -> None:
hparam_dict = {
"algorithm": self.model.__class__.__name__,
"learning rate": self.model.learning_rate,
"gamma": self.model.gamma,
}
# define the metrics that will appear in the `HPARAMS` Tensorboard tab by referencing their tag
# Tensorbaord will find & display metrics from the `SCALARS` tab
metric_dict = {
"rollout/ep_len_mean": 0,
"rollout/ep_rew_mean": 0,
"train/value_loss": 0.0,
"train/actor_loss": 0.0,
}
self.logger.record(
"hparams",
HParam(hparam_dict, metric_dict),
exclude=("stdout", "log", "json", "csv"),
)
def _on_step(self) -> bool:
return True
class MeticLogger(BaseCallback):
def __init__(self,log_frequency=10, verbose=1):
super(MeticLogger, self).__init__(verbose)
self.verbose=verbose
self.log_frequency=log_frequency
self.value_lossess=[]
def _on_step(self) -> bool:
if self.n_calls % self.log_frequency == 0:
if (self.verbose == 1):
print(f"iterations: {self.model.logger.name_to_value['train/n_updates']}")
print(f"ep_rew_mean: {self.model.logger.name_to_value['train/ep_rew_mean']}")
print(f"policy_loss: {self.model.logger.name_to_value['train/policy_loss']}")
print(f"value_loss: {self.model.logger.name_to_value['train/value_loss']}")
print(f"entropy_loss: {self.model.logger.name_to_value['train/entropy_loss']}")
print("--------------------------------")
self.value_lossess.append(self.model.logger.name_to_value['train/value_loss'])
return True
class PlottingCallback(BaseCallback):
"""
Callback for plotting the performance in realtime.
:param verbose: (int)
"""
def __init__(self, log_dir, verbose=1):
super().__init__(verbose)
self._plot = None
self.log_dir = log_dir
def _on_step(self) -> bool:
# get the monitor's data
x, y = ts2xy(load_results(self.log_dir), 'timesteps')
if self._plot is None: # make the plot
plt.ion()
fig = plt.figure(figsize=(6,3))
ax = fig.add_subplot(111)
line, = ax.plot(x, y)
self._plot = (line, ax, fig)
plt.show()
else: # update and rescale the plot
self._plot[0].set_data(x, y)
self._plot[-2].relim()
self._plot[-2].set_xlim([self.locals["total_timesteps"] * -0.02,
self.locals["total_timesteps"] * 1.02])
self._plot[-2].autoscale_view(True,True,True)
self._plot[-1].canvas.draw()