forked from tianjunz/TEMPERA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
evaluation.py
179 lines (154 loc) · 6.79 KB
/
evaluation.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import numpy as np
import torch
import copy
from a2c_ppo_acktr import utils
from a2c_ppo_acktr.envs import make_vec_envs, make_vec_envs_eval
def evaluate(actor_critic, obs_rms, env_name, seed, num_processes, eval_log_dir,
device):
eval_envs = make_vec_envs(env_name, seed + num_processes, num_processes,
None, eval_log_dir, device, True)
vec_norm = utils.get_vec_normalize(eval_envs)
if vec_norm is not None:
vec_norm.eval()
vec_norm.obs_rms = obs_rms
eval_episode_rewards = []
obs = eval_envs.reset()
eval_recurrent_hidden_states = torch.zeros(
num_processes, actor_critic.recurrent_hidden_state_size, device=device)
eval_masks = torch.zeros(num_processes, 1, device=device)
while len(eval_episode_rewards) < 10:
with torch.no_grad():
_, action, _, eval_recurrent_hidden_states = actor_critic.act(
obs,
eval_recurrent_hidden_states,
eval_masks,
deterministic=True)
# Obser reward and next obs
obs, _, done, infos = eval_envs.step(action)
eval_masks = torch.tensor(
[[0.0] if done_ else [1.0] for done_ in done],
dtype=torch.float32,
device=device)
for info in infos:
if 'episode' in info.keys():
eval_episode_rewards.append(info['episode']['r'])
eval_envs.close()
print(" Evaluation using {} episodes: mean reward {:.5f}\n".format(
len(eval_episode_rewards), np.mean(eval_episode_rewards)))
def evaluate_lm(i, actor_critic, obs_rms, eval_envs, seed, num_processes,
num_test, orig_results, results, env_queue, params, args, obs_size, tp, fp, fn):
# vec_norm = utils.get_vec_normalize(eval_envs)
# if vec_norm is not None:
# vec_norm.eval()
# vec_norm.obs_rms = obs_rms
print('Evaluation ', i, ' started.', flush=True)
if args.load_ckpt:
file_path = 'checkpoints/'+str(args.models)+'_'+str(args.datasets)+'_'+str(args.seed)+'/'
eval_envs.envs[0].load_ckpt(file_path, i, num_test)
else:
if eval_envs.envs[0].embedding_prepared == False:
# eval_envs, _ = make_vec_envs_eval(params['seed'], params, args.max_steps, args.num_processes, args.gamma, obs_size, False, i, i%torch.cuda.device_count())
eval_envs.envs[0].prepare_embedding()
# if eval_envs.envs[0].embedding_prepared == False:
# # eval_envs, _ = make_vec_envs_eval(params['seed'], params, args.max_steps, args.num_processes, args.gamma, obs_size, False, i, i%torch.cuda.device_count())
# eval_envs.envs[0].prepare_embedding()
assert eval_envs.envs[0].embedding_prepared == True
eval_episode_rewards = []
obs = eval_envs.reset()
# eval_recurrent_hidden_states = torch.zeros(
# num_processes, actor_critic.recurrent_hidden_state_size).cuda()
# eval_masks = torch.zeros(num_processes, 1).cuda()
total_correct = 0
total_orig_correct = 0
total_samples = 0
_tp, _fp, _fn = 0, 0, 0
for i in range(0, num_test, num_processes):
if i + num_processes > num_test:
idxs = np.arange(i, num_test)
else:
idxs = np.arange(i, i + num_processes)
# TODO: auto this
# eval_envs.venv.envs[0].idxs = idxs
eval_envs.envs[0].idxs = idxs
obs = eval_envs.reset()
_done = False
while not _done:
with torch.no_grad():
_, action, _, eval_recurrent_hidden_states = actor_critic.act(
# obs.cpu().cuda(),
torch.tensor(obs).float(),
# eval_recurrent_hidden_states.cpu().cuda(),
# eval_masks.cpu().cuda(),
None,
None,
deterministic=True)
# deterministic=False)
# Obser reward and next obs
obs, _, done, infos = eval_envs.step(action)
_done = done[0]
# eval_masks = torch.tensor(
# [[0.0] if done_ else [1.0] for done_ in done],
# dtype=torch.float32,
# device=device)
total_correct += infos[0]['correct']
total_orig_correct += infos[0]['orig_correct']
total_samples += infos[0]['total']
_tp += infos[0]['tp']
_fp += infos[0]['fp']
_fn += infos[0]['fn']
# eval_envs.close()
orig_results.put(total_orig_correct/total_samples)
results.put(total_correct/total_samples)
tp.put(_tp)
fp.put(_fp)
fn.put(_fn)
print(" Evaluation using {} episodes: mean reward {:.5f}, original mean reward {:.5f}".format(
len(eval_episode_rewards), total_correct/total_samples, total_orig_correct/total_samples), flush=True)
def evaluate_fs_lm(actor_critic, obs_rms, eval_envs, seed, num_processes,
num_test, params, args, obs_size):
# vec_norm = utils.get_vec_normalize(eval_envs)
# if vec_norm is not None:
# vec_norm.eval()
# vec_norm.obs_rms = obs_rms
print('Evaluation fs started.', flush=True)
eval_episode_rewards = []
obs = eval_envs.reset()
# eval_recurrent_hidden_states = torch.zeros(
# num_processes, actor_critic.recurrent_hidden_state_size).cuda()
# eval_masks = torch.zeros(num_processes, 1).cuda()
total_correct = 0
total_orig_correct = 0
total_samples = 0
for i in range(0, num_test, num_processes):
if i + num_processes > num_test:
idxs = np.arange(i, num_test)
else:
idxs = np.arange(i, i + num_processes)
# TODO: auto this
# eval_envs.venv.envs[0].idxs = idxs
eval_envs.envs[0].idxs = idxs
obs = eval_envs.reset()
_done = False
while not _done:
with torch.no_grad():
_, action, _, eval_recurrent_hidden_states = actor_critic.act(
# obs.cpu().cuda(),
torch.tensor(obs).float(),
# eval_recurrent_hidden_states.cpu().cuda(),
# eval_masks.cpu().cuda(),
None,
None,
deterministic=True)
# deterministic=False)
# Obser reward and next obs
obs, _, done, infos = eval_envs.step(action)
_done = done[0]
# eval_masks = torch.tensor(
# [[0.0] if done_ else [1.0] for done_ in done],
# dtype=torch.float32,
# device=device)
total_correct += infos[0]['correct']
total_orig_correct += infos[0]['orig_correct']
total_samples += infos[0]['total']
# eval_envs.close()
print(" Evaluation fs using: mean reward {:.5f}, original mean reward {:.5f}".format(total_correct/total_samples, total_orig_correct/total_samples), flush=True)