-
Notifications
You must be signed in to change notification settings - Fork 25
/
symbolic_ppo_agent.py
395 lines (311 loc) · 17.3 KB
/
symbolic_ppo_agent.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
"""
This file is a modified version of the PPO agent provided by BabyAI
"""
from .base_ppo_agent import BasePPOAgent
import babyai.utils
from babyai.rl.utils import DictList
from babyai.rl.format import default_preprocess_obss
import torch
from torch.distributions import Categorical
import numpy
from tqdm import tqdm
import logging
class SymbolicPPOAgent(BasePPOAgent):
"""The class for the Proximal Policy Optimization algorithm
([Schulman et al., 2015](https://arxiv.org/abs/1707.06347))."""
def __init__(self, envs, acmodel, num_frames_per_proc=None, discount=0.99, lr=7e-4, beta1=0.9, beta2=0.999,
gae_lambda=0.95, entropy_coef=0.01, value_loss_coef=0.5, max_grad_norm=0.5, recurrence=4,
adam_eps=1e-5, clip_eps=0.2, epochs=4, batch_size=256, preprocess_obss=None, reshape_reward=None,
aux_info=None, use_penv=False, sampling_temperature=1, debug=False):
super().__init__(envs, num_frames_per_proc, discount, lr, gae_lambda, entropy_coef, value_loss_coef,
max_grad_norm, reshape_reward, aux_info,
device=torch.device("cuda" if torch.cuda.is_available() else "cpu"))
self.acmodel = acmodel
self.acmodel.train()
self.recurrence = recurrence
self.preprocess_obss = preprocess_obss or default_preprocess_obss
self.sampling_temperature = sampling_temperature
assert self.num_frames_per_proc % self.recurrence == 0
shape = (self.num_frames_per_proc, self.num_procs)
logging.info("resetting environment")
self.obs = self.env.reset()
logging.info("reset environment")
self.memory = torch.zeros(shape[1], self.acmodel.memory_size, device=self.device)
self.memories = torch.zeros(*shape, self.acmodel.memory_size, device=self.device)
if "cont" in self.acmodel.arch:
self.actions = torch.zeros(self.num_frames_per_proc, self.num_procs,
2, device=self.device, dtype=torch.float)
else:
self.actions = torch.zeros(*shape, device=self.device, dtype=torch.int)
self.pi_l_actions = torch.zeros(*shape, device=self.device, dtype=torch.int)
self.clip_eps = clip_eps
self.epochs = epochs
self.batch_size = batch_size
self.debug = debug
assert self.batch_size % self.recurrence == 0
self.optimizer = torch.optim.Adam(self.acmodel.parameters(), lr, (beta1, beta2), eps=adam_eps)
self.batch_num = 0
def collect_experiences(self, debug=False):
"""Collects rollouts and computes advantages.
Runs several environments concurrently. The next actions are computed
in a batch mode for all environments at the same time. The rollouts
and advantages from all environments are concatenated together.
Returns
-------
exps : DictList
Contains actions, rewards, advantages etc as attributes.
Each attribute, e.g. `exps.reward` has a shape
(self.num_frames_per_proc * num_envs, ...). k-th block
of consecutive `self.num_frames_per_proc` frames contains
data obtained from the k-th environment. Be careful not to mix
data from different environments!
logs : dict
Useful stats about the training process, including the average
reward, policy loss, value loss, etc.
"""
for i in tqdm(range(self.num_frames_per_proc), ascii=" "*9 + ">", ncols=100):
# Do one agent-environment interaction
preprocessed_obs = self.preprocess_obss(self.obs, device=self.device)
with torch.no_grad():
model_results = self.acmodel(preprocessed_obs, self.memory * self.mask.unsqueeze(1))
dist = model_results['dist']
value = model_results['value']
memory = model_results['memory']
extra_predictions = model_results['extra_predictions']
if self.sampling_temperature != 1:
dist = Categorical(logits=dist.logits/self.sampling_temperature)
action = dist.sample()
if debug:
a = numpy.array([int(input())])
obs, reward, done, env_info = self.env.step(a)
else:
a = action.cpu().numpy()
real_a = numpy.copy(a)
real_a[real_a > 6] = 6
obs, reward, done, env_info = self.env.step(real_a)
if isinstance(env_info, tuple) and len(env_info) == 2:
info, pi_l_actions = env_info
# if pi_l_actions is not None:
# if self.pi_l_actions.shape[1] != pi_l_actions.shape[0]:
# self.pi_l_actions = torch.zeros((self.num_frames_per_proc, pi_l_actions.shape[0]), device=self.device, dtype=torch.int)
# self.pi_l_actions[i] = pi_l_actions
else:
info = env_info
if self.aux_info:
env_info = self.aux_info_collector.process(env_info)
# env_info = self.process_aux_info(env_info)
if debug:
babyai.utils.viz(self.env)
print(babyai.utils.info(reward, heading="Reward"))
print(babyai.utils.info(info, "Subtasks"))
# Update experiences values
self.obss[i] = self.obs
self.obs = obs
self.memories[i] = self.memory
self.memory = memory
self.masks[i] = self.mask
self.mask = 1 - torch.tensor(done, device=self.device, dtype=torch.float)
self.actions[i] = action
self.values[i] = value
if self.reshape_reward is not None:
rewards_shaped = torch.tensor([
self.reshape_reward(obs_, action_, reward_, done_, info_)
for obs_, action_, reward_, done_, info_ in zip(obs, action, reward, done, info)
], device=self.device)
self.rewards[i] = rewards_shaped[:,0]
self.rewards_bonus[i] = rewards_shaped[:,1]
else:
self.rewards[i] = torch.tensor(reward, device=self.device)
log_prob = dist.log_prob(action)
if len(log_prob.shape) > 1:
log_prob = log_prob.sum(dim=-1)
self.log_probs[i] = log_prob
if self.aux_info:
self.aux_info_collector.fill_dictionaries(i, env_info, extra_predictions)
# Update log values
self.log_episode_return += torch.tensor(reward, device=self.device, dtype=torch.float)
self.log_episode_reshaped_return += self.rewards[i]
self.log_episode_reshaped_return_bonus += self.rewards_bonus[i]
self.log_episode_num_frames += torch.ones(self.num_procs, device=self.device)
for i, done_ in enumerate(done):
if done_:
self.log_done_counter += 1
self.log_return.append(self.log_episode_return[i].item())
self.log_reshaped_return.append(self.log_episode_reshaped_return[i].item())
self.log_reshaped_return_bonus.append(self.log_episode_reshaped_return_bonus[i].item())
self.log_num_frames.append(self.log_episode_num_frames[i].item())
self.log_episode_return *= self.mask
self.log_episode_reshaped_return *= self.mask
self.log_episode_reshaped_return_bonus *= self.mask
self.log_episode_num_frames *= self.mask
# Add advantage and return to experiences
preprocessed_obs = self.preprocess_obss(self.obs, device=self.device)
with torch.no_grad():
next_value = self.acmodel(preprocessed_obs, self.memory * self.mask.unsqueeze(1))['value']
for i in reversed(range(self.num_frames_per_proc)):
next_mask = self.masks[i+1] if i < self.num_frames_per_proc - 1 else self.mask
next_value = self.values[i+1] if i < self.num_frames_per_proc - 1 else next_value
next_advantage = self.advantages[i+1] if i < self.num_frames_per_proc - 1 else 0
delta = self.rewards[i] + self.discount * next_value * next_mask - self.values[i]
self.advantages[i] = delta + self.discount * self.gae_lambda * next_advantage * next_mask
# Flatten the data correctly, making sure that
# each episode's data is a continuous chunk
exps = DictList()
exps.obs = [self.obss[i][j]
for j in range(self.num_procs)
for i in range(self.num_frames_per_proc)]
# In commments below T is self.num_frames_per_proc, P is self.num_procs,
# D is the dimensionality
# T x P x D -> P x T x D -> (P * T) x D
exps.memory = self.memories.transpose(0, 1).reshape(-1, *self.memories.shape[2:])
# T x P -> P x T -> (P * T) x 1
exps.mask = self.masks.transpose(0, 1).reshape(-1).unsqueeze(1)
# for all tensors below, T x P -> P x T -> P * T
if "cont" in self.acmodel.arch:
exps.action = self.actions.transpose(0, 1).reshape(-1, 2)
else:
exps.action = self.actions.transpose(0, 1).reshape(-1)
exps.pi_l_action = self.pi_l_actions.transpose(0, 1).reshape(-1)
exps.value = self.values.transpose(0, 1).reshape(-1)
exps.reward = self.rewards.transpose(0, 1).reshape(-1)
exps.advantage = self.advantages.transpose(0, 1).reshape(-1)
exps.returnn = exps.value + exps.advantage
exps.log_prob = self.log_probs.transpose(0, 1).reshape(-1)
if self.aux_info:
exps = self.aux_info_collector.end_collection(exps)
# Preprocess experiences
exps.obs = self.preprocess_obss(exps.obs, device=self.device)
# Log some values
keep = max(self.log_done_counter, self.num_procs)
log = {
"return_per_episode": self.log_return[-keep:],
"reshaped_return_per_episode": self.log_reshaped_return[-keep:],
"reshaped_return_bonus_per_episode": self.log_reshaped_return_bonus[-keep:],
"num_frames_per_episode": self.log_num_frames[-keep:],
"num_frames": self.num_frames,
"episodes_done": self.log_done_counter,
}
self.log_done_counter = 0
self.log_return = self.log_return[-self.num_procs:]
self.log_reshaped_return = self.log_reshaped_return[-self.num_procs:]
self.log_reshaped_return_bonus = self.log_reshaped_return_bonus[-self.num_procs:]
self.log_num_frames = self.log_num_frames[-self.num_procs:]
return exps, log
def update_parameters(self):
# Collect experiences
exps, logs = self.collect_experiences(debug=self.debug)
# print(exps.action)
# action_counts = exps.action.unique(return_counts=True)
# pi_l_action_counts = exps.pi_l_action.unique(return_counts=True)
'''
exps is a DictList with the following keys ['obs', 'memory', 'mask', 'action', 'value', 'reward',
'advantage', 'returnn', 'log_prob'] and ['collected_info', 'extra_predictions'] if we use aux_info
exps.obs is a DictList with the following keys ['image', 'instr']
exps.obj.image is a (n_procs * n_frames_per_proc) x image_size 4D tensor
exps.obs.instr is a (n_procs * n_frames_per_proc) x (max number of words in an instruction) 2D tensor
exps.memory is a (n_procs * n_frames_per_proc) x (memory_size = 2*image_embedding_size) 2D tensor
exps.mask is (n_procs * n_frames_per_proc) x 1 2D tensor
if we use aux_info: exps.collected_info and exps.extra_predictions are DictLists with keys
being the added information. They are either (n_procs * n_frames_per_proc) 1D tensors or
(n_procs * n_frames_per_proc) x k 2D tensors where k is the number of classes for multiclass classification
'''
for _ in tqdm(range(self.epochs), ascii=" "*9 + "<", ncols=100):
# Initialize log values
log_entropies = []
log_values = []
log_policy_losses = []
log_value_losses = []
log_grad_norms = []
log_losses = []
'''
For each epoch, we create int(total_frames / batch_size + 1) batches, each of size batch_size (except
maybe the last one. Each batch is divided into sub-batches of size recurrence (frames are contiguous in
a sub-batch), but the position of each sub-batch in a batch and the position of each batch in the whole
list of frames is random thanks to self._get_batches_starting_indexes().
'''
for inds in self._get_batches_starting_indexes():
# inds is a numpy array of indices that correspond to the beginning of a sub-batch
# there are as many inds as there are batches
# Initialize batch values
batch_entropy = 0
batch_value = 0
batch_policy_loss = 0
batch_value_loss = 0
batch_loss = 0
# Initialize memory
memory = exps.memory[inds]
for i in range(self.recurrence):
# Create a sub-batch of experience
sb = exps[inds + i]
# Compute loss
model_results = self.acmodel(sb.obs, memory * sb.mask)
dist = model_results['dist']
value = model_results['value']
memory = model_results['memory']
extra_predictions = model_results['extra_predictions']
entropy = dist.entropy().mean()
log_prob = dist.log_prob(sb.action)
if len(log_prob.shape) > 1:
log_prob = log_prob.sum(dim=-1)
ratio = torch.exp(log_prob - sb.log_prob)
surr1 = ratio * sb.advantage
surr2 = torch.clamp(ratio, 1.0 - self.clip_eps, 1.0 + self.clip_eps) * sb.advantage
policy_loss = -torch.min(surr1, surr2).mean()
value_clipped = sb.value + torch.clamp(value - sb.value, -self.clip_eps, self.clip_eps)
surr1 = (value - sb.returnn).pow(2)
surr2 = (value_clipped - sb.returnn).pow(2)
value_loss = torch.max(surr1, surr2).mean()
loss = policy_loss - self.entropy_coef * entropy + self.value_loss_coef * value_loss
# Update batch values
batch_entropy += entropy.item()
batch_value += value.mean().item()
batch_policy_loss += policy_loss.item()
batch_value_loss += value_loss.item()
batch_loss += loss
# Update memories for next epoch
if i < self.recurrence - 1:
exps.memory[inds + i + 1] = memory.detach()
# Update batch values
batch_entropy /= self.recurrence
batch_value /= self.recurrence
batch_policy_loss /= self.recurrence
batch_value_loss /= self.recurrence
batch_loss /= self.recurrence
# Update actor-critic
self.optimizer.zero_grad()
batch_loss.backward()
grad_norm = sum(p.grad.data.norm(2) ** 2 for p in self.acmodel.parameters() if p.grad is not None) ** 0.5
torch.nn.utils.clip_grad_norm_(self.acmodel.parameters(), self.max_grad_norm)
self.optimizer.step()
# Update log values
log_entropies.append(batch_entropy)
log_values.append(batch_value)
log_policy_losses.append(batch_policy_loss)
log_value_losses.append(batch_value_loss)
log_grad_norms.append(grad_norm.item())
log_losses.append(batch_loss.item())
# Log some values
logs["entropy"] = numpy.mean(log_entropies)
logs["value"] = numpy.mean(log_values)
logs["policy_loss"] = numpy.mean(log_policy_losses)
logs["value_loss"] = numpy.mean(log_value_losses)
logs["grad_norm"] = numpy.mean(log_grad_norms)
logs["loss"] = numpy.mean(log_losses)
# logs["actions"] = action_counts[0].cpu().numpy(), (action_counts[1].float()/action_counts[1].sum()).cpu().numpy()
# logs["pi_l_actions"] = pi_l_action_counts[0].cpu().numpy(), (pi_l_action_counts[1].float()/pi_l_action_counts[1].sum()).cpu().numpy()
return logs
def _get_batches_starting_indexes(self):
"""Gives, for each batch, the indexes of the observations given to
the model and the experiences used to compute the loss at first.
Returns
-------
batches_starting_indexes : list of list of int
the indexes of the experiences to be used at first for each batch
"""
indexes = numpy.arange(0, self.num_frames, self.recurrence)
indexes = numpy.random.permutation(indexes)
num_indexes = self.batch_size // self.recurrence
batches_starting_indexes = [indexes[i:i + num_indexes] for i in range(0, len(indexes), num_indexes)]
return batches_starting_indexes
def generate_trajectories(self, dict_modifier, n_tests, language='english', im_learning=False, debug=False):
raise NotImplementedError()