forked from higgsfield/Imagination-Augmented-Agents
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagination-augmented_agent.py
More file actions
450 lines (306 loc) · 12.5 KB
/
imagination-augmented_agent.py
File metadata and controls
450 lines (306 loc) · 12.5 KB
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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#!/usr/bin/env python
# coding: utf-8
# In[1]:
import sys
import numpy as np
import os
import torch
import torch.nn as nn
import torch.optim as optim
import torch.nn.functional as F
import torch.autograd as autograd
from common.multiprocessing_env import SubprocVecEnv
from common.minipacman import MiniPacman
from common.environment_model import EnvModel
from common.actor_critic import OnPolicy, ActorCritic, RolloutStorage
# In[2]:
# from IPython.display import clear_output
import matplotlib.pyplot as plt
# get_ipython().magic(u'matplotlib inline')
# <h4>USE CUDA</h4>
# In[3]:
USE_CUDA = torch.cuda.is_available()
Variable = lambda *args, **kwargs: autograd.Variable(*args, **kwargs).cuda() if USE_CUDA else autograd.Variable(*args, **kwargs)
# <h3>Pixels and Rewards</h3>
# In[4]:
pixels = (
(0.0, 1.0, 0.0),
(0.0, 1.0, 1.0),
(0.0, 0.0, 1.0),
(1.0, 1.0, 1.0),
(1.0, 1.0, 0.0),
(0.0, 0.0, 0.0),
(1.0, 0.0, 0.0)
)
pixel_to_onehot = {pix:i for i, pix in enumerate(pixels)}
num_pixels = len(pixels)
task_rewards = {
"regular": [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
"avoid": [0.1, -0.1, -5, -10, -20],
"hunt": [0, 1, 10, -20],
"ambush": [0, -0.1, 10, -20],
"rush": [0, -0.1, 9.9]
}
reward_to_onehot = {mode: {reward:i for i, reward in enumerate(task_rewards[mode])} for mode in task_rewards.keys()}
def pix_to_target(next_states):
target = []
for pixel in next_states.transpose(0, 2, 3, 1).reshape(-1, 3):
target.append(pixel_to_onehot[tuple([np.round(pixel[0]), np.round(pixel[1]), np.round(pixel[2])])])
return target
def target_to_pix(imagined_states):
pixels = []
to_pixel = {value: key for key, value in pixel_to_onehot.items()}
for target in imagined_states:
pixels.append(list(to_pixel[target]))
return np.array(pixels)
def rewards_to_target(mode, rewards):
target = []
for reward in rewards:
target.append(reward_to_onehot[mode][reward])
return target
def displayImage(image, step, reward):
s = str(step) + " " + str(reward)
plt.title(s)
plt.imshow(image)
plt.show()
# <h3>Creating environments</h3>
# In[5]:
mode = "regular"
num_envs = 16
def make_env():
def _thunk():
env = MiniPacman(mode, 1000)
return env
return _thunk
envs = [make_env() for i in range(num_envs)]
envs = SubprocVecEnv(envs)
state_shape = envs.observation_space.shape
num_actions = envs.action_space.n
num_rewards = len(task_rewards[mode])
# <h1>I2A components</h1>
# <p>The Rollout Encoder is an GRU with convolutional encoder which sequentially processes
# a trajectory</p>
# In[6]:
class RolloutEncoder(nn.Module):
def __init__(self, in_shape, num_rewards, hidden_size):
super(RolloutEncoder, self).__init__()
self.in_shape = in_shape
self.features = nn.Sequential(
nn.Conv2d(in_shape[0], 16, kernel_size=3, stride=1),
nn.ReLU(),
nn.Conv2d(16, 16, kernel_size=3, stride=2),
nn.ReLU(),
)
self.gru = nn.GRU(self.feature_size() + num_rewards, hidden_size)
def forward(self, state, reward):
num_steps = state.size(0)
batch_size = state.size(1)
state = state.view(-1, *self.in_shape)
state = self.features(state)
state = state.view(num_steps, batch_size, -1)
rnn_input = torch.cat([state, reward], 2)
_, hidden = self.gru(rnn_input)
return hidden.squeeze(0)
def feature_size(self):
return self.features(autograd.Variable(torch.zeros(1, *self.in_shape))).view(1, -1).size(1)
# <p>For the model-free path of the I2A, it's used a standard network of convolutional layers plus one fully
# connected one</p>
# In[7]:
class I2A(OnPolicy):
def __init__(self, in_shape, num_actions, num_rewards, hidden_size, imagination, full_rollout=True):
super(I2A, self).__init__()
self.in_shape = in_shape
self.num_actions = num_actions
self.num_rewards = num_rewards
self.imagination = imagination
self.features = nn.Sequential(
nn.Conv2d(in_shape[0], 16, kernel_size=3, stride=1),
nn.ReLU(),
nn.Conv2d(16, 16, kernel_size=3, stride=2),
nn.ReLU(),
)
self.encoder = RolloutEncoder(in_shape, num_rewards, hidden_size)
if full_rollout:
self.fc = nn.Sequential(
nn.Linear(self.feature_size() + num_actions * hidden_size, 256),
nn.ReLU(),
)
else:
self.fc = nn.Sequential(
nn.Linear(self.feature_size() + hidden_size, 256),
nn.ReLU(),
)
self.critic = nn.Linear(256, 1)
self.actor = nn.Linear(256, num_actions)
def forward(self, state):
batch_size = state.size(0)
imagined_state, imagined_reward = self.imagination(state.data)
hidden = self.encoder(Variable(imagined_state), Variable(imagined_reward))
hidden = hidden.view(batch_size, -1)
state = self.features(state)
state = state.view(state.size(0), -1)
x = torch.cat([state, hidden], 1)
x = self.fc(x)
logit = self.actor(x)
value = self.critic(x)
return logit, value
def feature_size(self):
return self.features(autograd.Variable(torch.zeros(1, *self.in_shape))).view(1, -1).size(1)
# <p>The imagination core (IC) predicts the next time step conditioned on an action sampled from the rollout policy (distil_policy).<br>
# See Figure 1 a. in the paper
# </p>
# In[8]:
class ImaginationCore(object):
def __init__(self, num_rolouts, in_shape, num_actions, num_rewards, env_model, distil_policy, full_rollout=True):
self.num_rolouts = num_rolouts
self.in_shape = in_shape
self.num_actions = num_actions
self.num_rewards = num_rewards
self.env_model = env_model
self.distil_policy = distil_policy
self.full_rollout = full_rollout
def __call__(self, state):
state = state.cpu()
batch_size = state.size(0)
rollout_states = []
rollout_rewards = []
if self.full_rollout:
state = state.unsqueeze(0).repeat(self.num_actions, 1, 1, 1, 1).view(-1, *self.in_shape)
action = torch.LongTensor([[i] for i in range(self.num_actions)]*batch_size)
action = action.view(-1)
rollout_batch_size = batch_size * self.num_actions
else:
action = self.distil_policy.act(Variable(state, volatile=True))
action = action.data.cpu()
rollout_batch_size = batch_size
for step in range(self.num_rolouts):
onehot_action = torch.zeros(rollout_batch_size, self.num_actions, *self.in_shape[1:])
onehot_action[range(rollout_batch_size), action] = 1
inputs = torch.cat([state, onehot_action], 1)
imagined_state, imagined_reward = self.env_model(Variable(inputs, volatile=True))
imagined_state = F.softmax(imagined_state).max(1)[1].data.cpu()
imagined_reward = F.softmax(imagined_reward).max(1)[1].data.cpu()
imagined_state = target_to_pix(imagined_state.numpy())
imagined_state = torch.FloatTensor(imagined_state).view(rollout_batch_size, *self.in_shape)
onehot_reward = torch.zeros(rollout_batch_size, self.num_rewards)
onehot_reward[range(rollout_batch_size), imagined_reward] = 1
rollout_states.append(imagined_state.unsqueeze(0))
rollout_rewards.append(onehot_reward.unsqueeze(0))
state = imagined_state
action = self.distil_policy.act(Variable(state, volatile=True))
action = action.data.cpu()
return torch.cat(rollout_states), torch.cat(rollout_rewards)
# <h3>Full Rollout</h3>
# <p>
# if full_rollout == True: perform rollout for each possible action in the environment. <br>
# if full_rollout == False: perform rollout for one action from distil policy.
# </p>
# In[9]:
full_rollout = True
# In[10]:
env_model = EnvModel(envs.observation_space.shape, num_pixels, num_rewards)
env_model.load_state_dict(torch.load("env_model_" + mode))
distil_policy = ActorCritic(envs.observation_space.shape, envs.action_space.n)
distil_optimizer = optim.Adam(distil_policy.parameters())
rollout_steps = int(sys.argv[1]) # the number of steps in the rollout
imagination = ImaginationCore(rollout_steps, state_shape, num_actions, num_rewards, env_model, distil_policy, full_rollout=full_rollout)
actor_critic = I2A(state_shape, num_actions, num_rewards, 256, imagination, full_rollout=full_rollout)
#rmsprop hyperparams:
lr = 7e-4
eps = 1e-5
alpha = 0.99
optimizer = optim.RMSprop(actor_critic.parameters(), lr, eps=eps, alpha=alpha)
if USE_CUDA:
env_model = env_model.cuda()
distil_policy = distil_policy.cuda()
actor_critic = actor_critic.cuda()
# <h2>Training</h2>
# In[11]:
gamma = 0.99
entropy_coef = 0.01
value_loss_coef = 0.5
max_grad_norm = 0.5
num_steps = 5
num_frames = int(10000)
rollout = RolloutStorage(num_steps, num_envs, envs.observation_space.shape)
rollout.cuda()
all_rewards = []
all_losses = []
# In[ ]:
state = envs.reset()
current_state = torch.FloatTensor(np.float32(state))
rollout.states[0].copy_(current_state)
episode_rewards = torch.zeros(num_envs, 1)
final_rewards = torch.zeros(num_envs, 1)
os.system("mkdir -p data/{}".format(mode))
filename1 = "data/{}/reward_output_{}_{}.txt".format(mode, rollout_steps, mode)
filename2 = "data/{}/loss_output_{}_{}.txt".format(mode, rollout_steps, mode)
f1 = open(filename1, 'w')
f2 = open(filename2, 'w')
f1.close()
f2.close()
for i_update in range(num_frames):
for step in range(num_steps):
if USE_CUDA:
current_state = current_state.cuda()
action = actor_critic.act(Variable(current_state))
next_state, reward, done, _ = envs.step(action.squeeze(1).cpu().data.numpy())
reward = torch.FloatTensor(reward).unsqueeze(1)
episode_rewards += reward
masks = torch.FloatTensor(1-np.array(done)).unsqueeze(1)
final_rewards *= masks
final_rewards += (1-masks) * episode_rewards
episode_rewards *= masks
if USE_CUDA:
masks = masks.cuda()
current_state = torch.FloatTensor(np.float32(next_state))
rollout.insert(step, current_state, action.data, reward, masks)
_, next_value = actor_critic(Variable(rollout.states[-1], volatile=True))
next_value = next_value.data
returns = rollout.compute_returns(next_value, gamma)
logit, action_log_probs, values, entropy = actor_critic.evaluate_actions(
Variable(rollout.states[:-1]).view(-1, *state_shape),
Variable(rollout.actions).view(-1, 1)
)
distil_logit, _, _, _ = distil_policy.evaluate_actions(
Variable(rollout.states[:-1]).view(-1, *state_shape),
Variable(rollout.actions).view(-1, 1)
)
distil_loss = 0.01 * (F.softmax(logit).detach() * F.log_softmax(distil_logit)).sum(1).mean()
values = values.view(num_steps, num_envs, 1)
action_log_probs = action_log_probs.view(num_steps, num_envs, 1)
advantages = Variable(returns) - values
value_loss = advantages.pow(2).mean()
action_loss = -(Variable(advantages.data) * action_log_probs).mean()
optimizer.zero_grad()
loss = value_loss * value_loss_coef + action_loss - entropy * entropy_coef
loss.backward()
nn.utils.clip_grad_norm(actor_critic.parameters(), max_grad_norm)
optimizer.step()
distil_optimizer.zero_grad()
distil_loss.backward()
optimizer.step()
if i_update % 100 == 0:
all_rewards.append(final_rewards.mean())
all_losses.append(loss.data[0])
re = all_rewards[-1].item()
lo = all_losses[-1].item()
print "reward: {}, loss: {}, {}% done".format(re, lo, float(i_update)/100)
with open(filename1, 'a') as out1:
out1.writelines(str(re)+"\n")
with open(filename2, 'a') as out2:
out2.writelines(str(lo)+"\n")
rollout.after_update()
# <h2>Save the model</h2>
# In[ ]:
torch.save(actor_critic.state_dict(), "i2a_{}_{}".format(mode, rollout_steps))
plt.figure(figsize=(20, 5))
plt.subplot(131)
plt.title('epoch vs reward')
plt.plot(all_rewards)
plt.subplot(132)
plt.title('loss')
plt.plot(all_losses)
plt.savefig("figures/{}_fig_N_{}".format(mode, rollout_steps))
# In[ ]:
# In[ ]: