-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainer.py
111 lines (86 loc) · 3.99 KB
/
trainer.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
#!/usr/local/bin/python3
# The MIT License (MIT)
# Copyright (c) 2022 Prasanth Suresh and Yikang Gui
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
import os
from time import time, sleep
from datetime import timedelta
from torch.utils.tensorboard import SummaryWriter
class Trainer:
def __init__(self, env, env_test, algo, log_dir, seed=0, num_steps=10**5,
eval_interval=10**3, num_eval_episodes=5, action_discrete=False):
super().__init__()
# Env to collect samples.
self.env = env
self.seed = seed
self.env.seed(self.seed)
# Env for evaluation.
self.env_test = env_test
self.env_test.seed(2**31-seed)
self.algo = algo
self.log_dir = log_dir
# Log setting.
self.summary_dir = os.path.join(log_dir, 'summary')
self.writer = SummaryWriter(log_dir=self.summary_dir)
self.model_dir = os.path.join(log_dir, 'model')
if not os.path.exists(self.model_dir):
os.makedirs(self.model_dir)
# Other parameters.
self.num_steps = num_steps
self.eval_interval = eval_interval
self.num_eval_episodes = num_eval_episodes
self.action_discrete = action_discrete
def train(self):
# Time to start training.
self.start_time = time()
# Episode's timestep.
t = 0
# Initialize the environment.
state = self.env.reset()
for step in range(1, self.num_steps + 1):
# Pass to the algorithm to update state and episode timestep.
state, t = self.algo.step(self.env, state, t, step)
# Update the algorithm whenever ready.
if self.algo.is_update(step):
self.algo.update(self.writer)
# Evaluate regularly.
if step % self.eval_interval == 0:
self.evaluate(step)
self.algo.save_models(
os.path.join(self.model_dir, f'step{step}'))
# Wait for the logging to be finished.
# sleep(10)
def evaluate(self, step):
mean_return = 0.0
for _ in range(self.num_eval_episodes):
state = self.env_test.reset()
episode_return = 0.0
done = False
while (not done):
if self.action_discrete:
action = self.algo.exploit(state, action_discrete=self.action_discrete)
else: action = self.algo.exploit(state)
state, reward, done, _ = self.env_test.step(action)
episode_return += reward
mean_return += episode_return / self.num_eval_episodes
self.writer.add_scalar('return/test', mean_return, step)
print(f'Num steps: {step:<6} '
f'Return: {mean_return:<5.1f} '
f'Time: {self.time}')
@property
def time(self):
return str(timedelta(seconds=int(time() - self.start_time)))