-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.py
50 lines (43 loc) · 1.38 KB
/
test.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
import copy
import numpy as np
from envs import make_env
from utils.os_utils import make_dir
class Tester:
def __init__(self, args):
self.args = args
self.env = make_env(args)
self.info = []
if args.save_rews:
make_dir('log/rews', clear=False)
self.rews_record = {}
self.rews_record[args.env] = []
def test_rollouts(self):
rewards_sum = 0.0
for _ in range(self.args.test_rollouts):
rewards = 0.0
obs = self.env.reset()
for timestep in range(self.args.test_timesteps):
action, info = self.args.agent.step(obs, explore=False, test_info=True)
self.args.logger.add_dict(info)
if self.args.learn[:5]=='atari':
if np.random.uniform(0.0,1.0)<=self.args.test_eps:
action = self.env.action_space.sample()
obs, reward, done, info = self.env.step(action)
rewards += reward
if done: break
rewards_sum += rewards
self.args.logger.add_dict(info)
if self.args.save_rews:
step = self.args.learner.steps_counter
rews = rewards_sum/self.args.test_rollouts
self.rews_record[self.args.env].append((step, rews))
def cycle_summary(self):
self.test_rollouts()
def epoch_summary(self):
if self.args.save_rews:
for key, acc_info in self.rews_record.items():
log_folder = 'rews'
if self.args.tag!='': log_folder = log_folder+'/'+self.args.tag
self.args.logger.save_npz(acc_info, key, log_folder)
def final_summary(self):
pass