-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinstinct_training_loop.py
214 lines (179 loc) · 8.49 KB
/
instinct_training_loop.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
from math import log
import pickle
import torch
import numpy as np
import gym
from gym.envs.registration import register
from gym.utils import seeding
import safety_gym_mod
import matplotlib.pyplot as plt
from a2c_ppo_acktr import algo, utils
from a2c_ppo_acktr.envs import make_vec_envs
from a2c_ppo_acktr.evaluation import evaluate
from a2c_ppo_acktr.model import init_default_ppo, Policy, custom_weight_init
from a2c_ppo_acktr.storage import RolloutStorage
from arguments import get_args
try:
from exp_dir_util import get_experiment_save_dir
except:
pass
from copy import deepcopy
from os.path import join
from torch.utils.tensorboard import SummaryWriter
from enum import Enum
from double_rl_loop_main import ENV_NAME, NUM_PROC, policy_instinct_combinator, reward_cost_combinator, \
compare_two_models, EvalActorCritic
def instinct_loop_ppo(
args,
learning_rate,
num_steps,
num_updates,
inst_on,
visualize,
save_dir
):
torch.set_num_threads(1)
log_writer = SummaryWriter(save_dir, max_queue=1, filename_suffix="log")
device = torch.device("cpu")
env_name = ENV_NAME #"Safexp-PointGoal1-v0"
envs = make_vec_envs(env_name, np.random.randint(2 ** 32), NUM_PROC,
args.gamma, None, device, allow_early_resets=True, normalize=args.norm_vectors)
eval_envs = make_vec_envs(env_name, np.random.randint(2 ** 32), 1,
args.gamma, None, device, allow_early_resets=True, normalize=args.norm_vectors)
actor_critic_policy = torch.load("pretrained_policy.pt")
# Prepare modified observation shape for instinct
obs_shape = envs.observation_space.shape
inst_action_space = deepcopy(envs.action_space)
inst_obs_shape = list(obs_shape)
inst_obs_shape[0] = inst_obs_shape[0] + envs.action_space.shape[0]
# Prepare modified action space for instinct
inst_action_space.shape = list(inst_action_space.shape)
inst_action_space.shape[0] = inst_action_space.shape[0] + 1
inst_action_space.shape = tuple(inst_action_space.shape)
actor_critic_instinct = Policy(tuple(inst_obs_shape),
inst_action_space,
init_log_std=log(args.init_sigma),
base_kwargs={'recurrent': False})
actor_critic_policy.to(device)
actor_critic_instinct.to(device)
#agent_policy = algo.PPO(
# actor_critic_policy,
# args.clip_param,
# args.ppo_epoch,
# args.num_mini_batch,
# args.value_loss_coef,
# args.entropy_coef,
# lr=learning_rate,
# eps=args.eps,
# max_grad_norm=args.max_grad_norm)
agent_instinct = algo.PPO(
actor_critic_instinct,
args.clip_param,
args.ppo_epoch,
args.num_mini_batch,
args.value_loss_coef,
args.entropy_coef,
lr=learning_rate,
eps=args.eps,
max_grad_norm=args.max_grad_norm)
rollouts_cost = RolloutStorage(num_steps, NUM_PROC,
inst_obs_shape, inst_action_space,
actor_critic_instinct.recurrent_hidden_state_size)
obs = envs.reset()
i_obs = torch.cat([obs, torch.zeros((NUM_PROC, envs.action_space.shape[0]))], dim=1) # Add zero action to the observation
rollouts_cost.obs[0].copy_(i_obs)
rollouts_cost.to(device)
fitnesses = []
best_fitness_so_far = float("-Inf")
masks = torch.ones(num_steps + 1, NUM_PROC, 1)
recurrent_hidden_states = torch.zeros(num_steps + 1, NUM_PROC, actor_critic_policy.recurrent_hidden_state_size)
for j in range(num_updates):
training_collisions_current_update = 0
for step in range(num_steps):
# Sample actions
with torch.no_grad():
# (value, action, action_log_probs, rnn_hxs), (instinct_value, instinct_action, instinct_outputs_log_prob, i_rnn_hxs), final_action
value, action, action_log_probs, recurrent_hidden_states = actor_critic_policy.act(
obs,
recurrent_hidden_states,
masks,
deterministic=False
)
instinct_value, instinct_action, instinct_outputs_log_prob, instinct_recurrent_hidden_states = actor_critic_instinct.act(
rollouts_cost.obs[step],
rollouts_cost.recurrent_hidden_states[step],
rollouts_cost.masks[step],
deterministic=False,
)
# Combine two networks
final_action, i_control = policy_instinct_combinator(action, instinct_action)
obs, reward, done, infos = envs.step(final_action)
#envs.render()
training_collisions_current_update += sum([i['cost'] for i in infos])
reward, violation_cost = reward_cost_combinator(reward, infos, NUM_PROC, i_control)
# If done then clean the history of observations.
masks = torch.FloatTensor([[0.0] if done_ else [1.0] for done_ in done])
bad_masks = torch.FloatTensor([[0.0] if 'bad_transition' in info.keys() else [1.0] for info in infos])
i_obs = torch.cat([obs, action], dim=1)
rollouts_cost.insert(i_obs, instinct_recurrent_hidden_states, instinct_action, instinct_outputs_log_prob,
instinct_value, violation_cost, masks, bad_masks)
with torch.no_grad():
next_value_instinct = actor_critic_instinct.get_value(rollouts_cost.obs[-1],
rollouts_cost.recurrent_hidden_states[-1],
rollouts_cost.masks[-1].detach())
rollouts_cost.compute_returns(next_value_instinct, args.use_gae, args.gamma,
args.gae_lambda, args.use_proper_time_limits)
print("training instinct")
# Instinct training phase
p_before = deepcopy(actor_critic_policy)
val_loss_i, action_loss_i, dist_entropy_i = agent_instinct.update(rollouts_cost)
p_after = deepcopy(actor_critic_policy)
assert compare_two_models(p_before, p_after), "policy changed when it shouldn't"
rollouts_cost.after_update()
ob_rms = utils.get_vec_normalize(envs)
if ob_rms is not None:
ob_rms = ob_rms.ob_rms
fits, info = evaluate(EvalActorCritic(actor_critic_policy, actor_critic_instinct), ob_rms, eval_envs, NUM_PROC,
reward_cost_combinator, device, instinct_on=inst_on, visualise=visualize)
instinct_reward = info['instinct_reward']
hazard_collisions = info['hazard_collisions']
print(
f"Step {j}, Fitness {fits.item()} ")
print(
f"Step {j}, Cost {instinct_reward}, value_loss instinct = {val_loss_i}, action_loss instinct= {action_loss_i}, "
f"dist_entropy instinct = {dist_entropy_i}")
print("-----------------------------------------------------------------")
# Tensorboard logging
log_writer.add_scalar("Task reward", fits.item(), j)
log_writer.add_scalar("cost/Training hazard collisions", training_collisions_current_update, j)
log_writer.add_scalar("cost/Instinct reward", instinct_reward, j)
log_writer.add_scalar("cost/Hazard collisions", hazard_collisions, j)
log_writer.add_scalar("value loss instinct", val_loss_i, j)
log_writer.add_scalar("action loss instinct", action_loss_i, j)
log_writer.add_scalar("dist entropy instinct", dist_entropy_i, j)
fitnesses.append(fits)
if fits.item() > best_fitness_so_far:
best_fitness_so_far = fits.item()
torch.save(actor_critic_instinct, join(save_dir, "model_rl_instinct.pt"))
torch.save(actor_critic_instinct, join(save_dir, "model_rl_instinct_latest.pt"))
torch.save(actor_critic_instinct, join(save_dir, f"model_rl_instinct_latest_update_{j}.pt"))
pickle.dump(ob_rms, open(join(save_dir, "ob_rms.pt"), "wb"))
return (fitnesses[-1]), 0, 0
def main():
args = get_args()
print("start the train function")
args.init_sigma = 0.7
args.lr = 0.001
# plot_weight_histogram(parameters)
exp_save_dir = get_experiment_save_dir(args)
instinct_loop_ppo(
args,
args.lr,
num_steps=9000,
num_updates=5000,
inst_on=False,
visualize=False,
save_dir=exp_save_dir
)
if __name__ == "__main__":
main()