This repository has been archived by the owner on Sep 19, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodels.py
executable file
·489 lines (385 loc) · 17.9 KB
/
models.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
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
import numpy as np
import copy
import gym
import time
import torch
import torch.nn.functional as F
from torch import nn
from torch.autograd import Variable
from torch import optim
from storage import ACStorage
from ac_nets import OneDimensionalACNets, KDimensionalACNets
"""
Implementations of algorithmn for RL
"""
class AgentA3C(nn.Module):
"""
Implementation of A3C algorithm
(https://arxiv.org/abs/1602.01783)
"""
def __init__(self, envs, gamma=0.99, optimizer=None, lr=1e-4, save_returns=True):
"""
Constructor
Arguments:
envs -- list of game environments
gamma -- discount factor, float
optimizer -- optimizer for learning proc., torch.Optimizer (default Adam)
lr -- learning rate, float (default 1e-4)
save_returns -- whether save returns on each steps, bool
"""
super(AgentA3C, self).__init__()
self.envs = envs
self.gamma = gamma
self.save_returns = save_returns
self.n_envs = len(envs)
self.n_actions = int(envs[0].action_space.n)
self.is_cuda = False
if (type(envs[0].observation_space) == gym.spaces.Discrete) or (
type(envs[0].observation_space) == gym.spaces.Box and
len(envs[0].observation_space.shape) == 1):
if type(envs[0].observation_space) == gym.spaces.Discrete:
self.states_shape = (envs[0].observation_space.n, )
self.nets = OneDimensionalACNets(
self.states_shape[0], self.n_actions, need_encode=True
)
else:
self.states_shape = (envs[0].observation_space.shape[0], )
self.nets = OneDimensionalACNets(
self.states_shape[0], self.n_actions, need_encode=False
)
elif (type(envs[0].observation_space) == gym.spaces.Box and
len(envs[0].observation_space.shape) == 3):
self.states_shape = envs[0].observation_space.shape
self.nets = KDimensionalACNets(self.states_shape, self.n_actions)
"""
# define type of environment and init AC NNs
if (type(envs[0].observation_space) == gym.spaces.Discrete) or (
type(envs[0].observation_space) == gym.spaces.Box and
len(envs[0].observation_space.shape) == 1):
if type(envs[0].observation_space) == gym.spaces.Discrete:
self.states_shape = (envs[0].observation_space.n, )
self.nets = OneDimensionalACNets(
self.states_shape[0], self.n_actions, need_encode=True
)
else:
self.states_shape = (envs[0].observation_space.shape[0], )
self.nets = OneDimensionalACNets(
self.states_shape[0], self.n_actions, need_encode=False
)
elif (type(envs[0].observation_space) == gym.spaces.Box
and len(envs[0].observation_space.shape) == 3):
self.states_shape = envs[0].observation_space.shape
self.nets = KDimensionalACNets(self.states_shape, self.n_actions)
"""
# set optimizer of create default
if optimizer is None:
self.optimizer = optim.Adam(self.parameters(), lr=lr)
else:
self.optimizer = copy.deepcopy(optimizer)
def cuda(self):
"""
Move model to GPU
"""
self.is_cuda = True
self.nets.cuda()
return super(AgentA3C, self).cuda()
def cpu(self):
"""
Move nets to CPU
"""
self.is_cuda = False
self.nets.cpu()
return super(AgentA3C, self).cpu()
def get_distr(self, state):
"""
Logits as Variable -> Distribution
Arguments:
state -- state for which actions distr. need to be computed
"""
state_enc = Variable(self.nets.encode_state(state), volatile=True)
return F.softmax(self.nets.actor_nn(state_enc), dim=-1)
def act(self, state):
"""
Choose action according to probabilities
Arguments:
state -- state from which agent acts
"""
return torch.multinomial(self.get_distr(state), 1).data[0]
def get_policy(self):
"""
Returns distributions for all possible states
"""
if type(self.envs[0].observation_space) != gym.spaces.Discrete:
raise ValueError('Avaliable only for discrete state spaces')
all_states = np.arange(self.states_shape[0])
states = Variable(self.nets.encode_states(all_states), volatile=True)
return F.softmax(self.nets.actor_nn(states), dim=-1).cpu().data.numpy()
def learn(self, n_agents, n_episodes, n_steps=10, k_bootstrap=5, entr_C=1e-3, verbosity=0):
"""
Learn model via Actor-Critic interaction
Arguments:
n_agents -- number of async. agents, int
n_episodes -- number of full interaction emulations, int
n_steps -- number of steps for each parameters update, int
k_bootstrap -- number of rewards used to compute GAE, int
entr_C -- entropy loss multiplier, float
verbosity -- numbeg of episodes to print debug inf, int (default 0, don't print)
"""
self.train()
self.nets.train()
storages = np.array([
ACStorage(n_steps, self.states_shape) for _ in range(n_agents)
])
states = np.array([env.reset() for env in self.envs])
dones = np.zeros((n_agents), dtype=bool)
batch_n = np.zeros((n_agents))
episode_rewards = np.zeros((n_agents))
if self.save_returns:
self.returns_over_episodes = []
if self.save_returns or verbosity > 0:
sum_rewards_e = 0.0
if verbosity > 0:
sum_loss_actor = 0.0
sum_loss_critic = 0.0
sum_entropy = 0.0
batches = 0
timer_st = time.time()
for i_episode in range(n_episodes):
for i_agent in np.where(dones == True)[0]:
batch_n[i_agent] = 0
episode_rewards[i_agent] = 0
states[i_agent] = self.envs[i_agent].reset()
for i_agent in range(n_agents):
storages[i_agent].clear()
for i_agent in range(n_agents):
for step in range(n_steps):
act = self.act(states[i_agent])
s_new, rew, dones[i_agent], _ = self.envs[i_agent].step(act)
storages[i_agent].insert(states[i_agent], act, rew)
states[i_agent] = s_new
if dones[i_agent]:
break
dones_agents = np.where(dones == True)[0]
active_agents = np.where(dones == False)[0]
#DEBUG
if self.save_returns or verbosity > 0:
episode_rewards += self.gamma**(batch_n*n_steps) * \
np.array([s.calc_G_0(self.gamma) for s in storages])
batch_n += 1
next_values = np.zeros(n_agents)
if active_agents.shape[0] > 0:
s_enc = self.nets.encode_states(np.array([s for s in states[active_agents]]))
next_values[active_agents] = self.nets.critic_nn(
Variable(s_enc, volatile=True)
).data[0]
states_all = [s.get_states() for s in storages]
indices = np.cumsum([0] + [s.shape[0] for s in states_all])
states_var = Variable(self.nets.encode_states(
np.concatenate(states_all)), requires_grad=False
)
values_var = self.nets.critic_nn(states_var)
actions_all = [s.get_actions() for s in storages]
aprobs_var = F.log_softmax(self.nets.actor_nn(states_var), dim=1)
aprobs_var = aprobs_var[np.arange(aprobs_var.size(0)), np.concatenate(actions_all)]
gae = np.concatenate(
[storages[i].calc_gae(
values_var[indices[i]:indices[i+1]],
next_values[i], self.gamma, k=k_bootstrap)
for i in range(n_agents)]
)
gae_var = Variable(torch.FloatTensor(gae), requires_grad=False)
if self.is_cuda:
gae_var = gae_var.cuda()
advantages = gae_var - values_var
entropy = -(aprobs_var * torch.exp(aprobs_var)).sum() / n_agents
actor_gain = (advantages * aprobs_var).sum() / n_agents
critic_loss = 0.5*advantages.pow(2).sum() / n_agents
self.optimizer.zero_grad()
(-actor_gain + critic_loss - entr_C*entropy).backward()
self.optimizer.step()
if verbosity > 0:
sum_entropy += entropy.data[0] / n_agents
sum_loss_actor += actor_gain.data[0] / n_agents
sum_loss_critic += critic_loss.data[0] / n_agents
if self.save_returns or verbosity > 0:
episode_reward = 0.0
if dones_agents.shape[0] > 0:
episode_reward = episode_rewards[dones_agents].sum() / dones_agents.shape[0]
elif len(self.returns_over_episodes) > 0:
episode_reward = self.returns_over_episodes[-1]
if self.save_returns:
self.returns_over_episodes.append(episode_reward)
sum_rewards_e += episode_reward
if verbosity > 0 and (i_episode+1) % verbosity == 0:
print(('episode {:6} [act: {:.4f}, crt: {:.4f}, ent: {:.4f} ' +
'rew_e: {:.6f}], {:.1f} ms/ep').format(
i_episode+1, sum_loss_actor/verbosity, sum_loss_critic/verbosity,
sum_entropy/verbosity, sum_rewards_e/verbosity,
(time.time()-timer_st)*1000/verbosity )
)
batches = sum_rewards_e = sum_loss_actor = sum_loss_critic = sum_entropy = 0.0
timer_st = time.time()
self.nets.eval()
self.eval()
class AgentA2C(nn.Module):
"""
Implementation of A2C algorithm
(https://arxiv.org/abs/1602.01783)
"""
def __init__(self, env, gamma=0.99, optimizer=None, lr=1e-4, save_returns=True):
"""
Constructor
Arguments:
env -- game environments
gamma -- discount factor, float
optimizer -- optimizer for learning proc., torch.Optimizer (default Adam)
lr -- learning rate, float (default 1e-4)
save_returns -- whether save returns on each steps, bool
"""
super(AgentA2C, self).__init__()
self.env = env
self.gamma = gamma
self.save_returns = save_returns
self.is_cuda = False
self.n_actions = int(env.action_space.n)
# define type of environment and init AC NNs
if (type(env.observation_space) == gym.spaces.Discrete) or (
type(env.observation_space) == gym.spaces.Box and
len(env.observation_space.shape) == 1):
if type(env.observation_space) == gym.spaces.Discrete:
self.states_shape = (env.observation_space.n, )
self.nets = OneDimensionalACNets(
self.states_shape[0], self.n_actions, need_encode=True
)
else:
self.states_shape = (env.observation_space.shape[0], )
self.nets = OneDimensionalACNets(
self.states_shape[0], self.n_actions, need_encode=False
)
elif (type(env.observation_space) == gym.spaces.Box and
len(env.observation_space.shape) == 3):
self.states_shape = env.observation_space.shape
self.nets = KDimensionalACNets(self.states_shape, self.n_actions)
if optimizer is None:
self.optimizer = optim.Adam(self.parameters(), lr=lr)
else:
self.optimizer = copy.deepcopy(optimizer)
def cuda(self):
"""
Move model to GPU
"""
self.is_cuda = True
self.nets.cuda()
return super(AgentA2C, self).cuda()
def cpu(self):
"""
Move model to CPU
"""
self.is_cuda = False
self.nets.cpu()
return super(AgentA2C, self).cpu()
def get_distr(self, state):
"""
Logits as Variable -> Distribution
Arguments:
state -- state for which actions distr. need to be computed
"""
state_enc = Variable(self.nets.encode_state(state), volatile=True)
return F.softmax(self.nets.actor_nn(state_enc), dim=-1)
def act(self, state):
"""
Choose action according to probabilities
Arguments:
state -- state from which agent acts
"""
return torch.multinomial(self.get_distr(state), 1).data[0]
def get_policy(self):
"""
Returns distributions for all possible states
"""
if type(self.env.observation_space) != gym.spaces.Discrete:
raise ValueError('Avaliable only for discrete state spaces')
all_states = np.arange(self.states_shape[0])
states = Variable(self.nets.encode_states(all_states), volatile=True)
return F.softmax(self.nets.actor_nn(states), dim=-1).cpu().data.numpy()
def learn(self, n_episodes, n_steps=10, k_bootstrap=5, entr_C=1e-3, verbosity=0):
"""
Learn model via Actor-Critic interaction
Arguments:
n_episodes -- number of full interaction emulations, int
n_steps -- number of steps for each parameters update, int
k_bootstrap -- number of rewards used to compute GAE, int
entr_C -- entropy loss multiplier, float
verbosity -- numbeg of episodes to print debug inf, int (default 0, don't print)
"""
self.train()
self.nets.train()
if self.save_returns:
self.returns_over_episodes = []
if self.save_returns or verbosity > 0:
sum_rewards_e = 0.0
if verbosity > 0:
sum_loss_actor = 0.0
sum_loss_critic = 0.0
sum_entropy = 0.0
timer_st = time.time()
batches = 0
storage = ACStorage(n_steps, self.states_shape)
for i_episode in range(n_episodes):
state = self.env.reset()
done = False
batch_n = 0
episode_reward = 0.0
while not done:
storage.clear()
for i_step in range(n_steps):
action = self.act(state)
state_new, reward, done, _ = self.env.step(action)
storage.insert(state, action, reward)
state = state_new
if done:
break
if self.save_returns or verbosity > 0:
episode_reward += self.gamma**(batch_n*n_steps) * \
storage.calc_G_0(self.gamma)
batch_n += 1
state_enc = self.nets.encode_state(state)
next_value = 0.0
if not done:
next_value = self.nets.critic_nn(Variable(state_enc, volatile=True)).data[0]
states = self.nets.encode_states(storage.get_states())
states_var = Variable(states, requires_grad=False)
values_var = self.nets.critic_nn(states_var)
aprobs_var = F.log_softmax(self.nets.actor_nn(states_var), dim=1)
aprobs_var = aprobs_var[np.arange(aprobs_var.size(0)), storage.get_actions()]
gae = storage.calc_gae(values_var, next_value, self.gamma, k=k_bootstrap)
gae_var = Variable(torch.FloatTensor(gae), requires_grad=False)
if self.is_cuda:
gae_var = gae_var.cuda()
advantages = gae_var - values_var
entropy = -(aprobs_var * torch.exp(aprobs_var)).sum()
actor_gain = (gae_var * aprobs_var).sum()
critic_loss = 0.5*advantages.pow(2).sum()
self.optimizer.zero_grad()
(-actor_gain + critic_loss - entr_C*entropy).backward()
self.optimizer.step()
if verbosity > 0:
sum_entropy += entropy.data[0]
sum_loss_actor += actor_gain.data[0]
sum_loss_critic += critic_loss.data[0]
if self.save_returns or verbosity > 0:
if self.save_returns:
self.returns_over_episodes.append(episode_reward)
sum_rewards_e += episode_reward
batches += batch_n+1
if verbosity > 0 and (i_episode+1) % verbosity == 0:
print(('episode {:6} [act: {:.4f}, crt: {:.4f}, ent: {:.4f} ' +
'rew_e: {:.6f}], {:.1f} ms/ep').format(
i_episode+1, sum_loss_actor/verbosity, sum_loss_critic/verbosity,
sum_entropy/verbosity, sum_rewards_e/verbosity,
(time.time()-timer_st)*1000/verbosity )
)
batches = sum_rewards_e = sum_loss_actor = sum_loss_critic = sum_entropy = 0.0
timer_st = time.time()
self.nets.eval()
self.eval()