-
Notifications
You must be signed in to change notification settings - Fork 155
/
models.py
366 lines (303 loc) · 13.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
import rl_games.algos_torch.layers
import numpy as np
import torch.nn as nn
import torch
import torch.nn.functional as F
import rl_games.common.divergence as divergence
from rl_games.common.extensions.distributions import CategoricalMasked
from torch.distributions import Categorical
from rl_games.algos_torch.sac_helper import SquashedNormal
from rl_games.algos_torch.running_mean_std import RunningMeanStd, RunningMeanStdObs
from rl_games.algos_torch.moving_mean_std import GeneralizedMovingStats
class BaseModel():
def __init__(self, model_class):
self.model_class = model_class
def is_rnn(self):
return False
def is_separate_critic(self):
return False
def get_value_layer(self):
return None
def build(self, config):
obs_shape = config['input_shape']
normalize_value = config.get('normalize_value', False)
normalize_input = config.get('normalize_input', False)
value_size = config.get('value_size', 1)
return self.Network(self.network_builder.build(self.model_class, **config), obs_shape=obs_shape,
normalize_value=normalize_value, normalize_input=normalize_input, value_size=value_size)
class BaseModelNetwork(nn.Module):
def __init__(self, obs_shape, normalize_value, normalize_input, value_size):
nn.Module.__init__(self)
self.obs_shape = obs_shape
self.normalize_value = normalize_value
self.normalize_input = normalize_input
self.value_size = value_size
if normalize_value:
self.value_mean_std = RunningMeanStd((self.value_size,)) # GeneralizedMovingStats((self.value_size,)) #
if normalize_input:
if isinstance(obs_shape, dict):
self.running_mean_std = RunningMeanStdObs(obs_shape)
else:
self.running_mean_std = RunningMeanStd(obs_shape)
def norm_obs(self, observation):
with torch.no_grad():
return self.running_mean_std(observation) if self.normalize_input else observation
def denorm_value(self, value):
with torch.no_grad():
return self.value_mean_std(value, denorm=True) if self.normalize_value else value
class ModelA2C(BaseModel):
def __init__(self, network):
BaseModel.__init__(self, 'a2c')
self.network_builder = network
class Network(BaseModelNetwork):
def __init__(self, a2c_network, **kwargs):
BaseModelNetwork.__init__(self,**kwargs)
self.a2c_network = a2c_network
def is_rnn(self):
return self.a2c_network.is_rnn()
def get_default_rnn_state(self):
return self.a2c_network.get_default_rnn_state()
def get_value_layer(self):
return self.a2c_network.get_value_layer()
def kl(self, p_dict, q_dict):
p = p_dict['logits']
q = q_dict['logits']
return divergence.d_kl_discrete(p, q)
def forward(self, input_dict):
is_train = input_dict.get('is_train', True)
action_masks = input_dict.get('action_masks', None)
prev_actions = input_dict.get('prev_actions', None)
input_dict['obs'] = self.norm_obs(input_dict['obs'])
logits, value, states = self.a2c_network(input_dict)
if is_train:
categorical = CategoricalMasked(logits=logits, masks=action_masks)
prev_neglogp = -categorical.log_prob(prev_actions)
entropy = categorical.entropy()
result = {
'prev_neglogp' : torch.squeeze(prev_neglogp),
'logits' : categorical.logits,
'values' : value,
'entropy' : entropy,
'rnn_states' : states
}
return result
else:
categorical = CategoricalMasked(logits=logits, masks=action_masks)
selected_action = categorical.sample().long()
neglogp = -categorical.log_prob(selected_action)
result = {
'neglogpacs' : torch.squeeze(neglogp),
'values' : self.denorm_value(value),
'actions' : selected_action,
'logits' : categorical.logits,
'rnn_states' : states
}
return result
class ModelA2CMultiDiscrete(BaseModel):
def __init__(self, network):
BaseModel.__init__(self, 'a2c')
self.network_builder = network
class Network(BaseModelNetwork):
def __init__(self, a2c_network, **kwargs):
BaseModelNetwork.__init__(self, **kwargs)
self.a2c_network = a2c_network
def is_rnn(self):
return self.a2c_network.is_rnn()
def get_default_rnn_state(self):
return self.a2c_network.get_default_rnn_state()
def get_value_layer(self):
return self.a2c_network.get_value_layer()
def kl(self, p_dict, q_dict):
p = p_dict['logits']
q = q_dict['logits']
return divergence.d_kl_discrete_list(p, q)
def forward(self, input_dict):
is_train = input_dict.get('is_train', True)
action_masks = input_dict.get('action_masks', None)
prev_actions = input_dict.get('prev_actions', None)
input_dict['obs'] = self.norm_obs(input_dict['obs'])
logits, value, states = self.a2c_network(input_dict)
if is_train:
if action_masks is None:
categorical = [Categorical(logits=logit) for logit in logits]
else:
action_masks = np.split(action_masks,len(logits), axis=1)
categorical = [CategoricalMasked(logits=logit, masks=mask) for logit, mask in zip(logits, action_masks)]
prev_actions = torch.split(prev_actions, 1, dim=-1)
prev_neglogp = [-c.log_prob(a.squeeze()) for c,a in zip(categorical, prev_actions)]
prev_neglogp = torch.stack(prev_neglogp, dim=-1).sum(dim=-1)
entropy = [c.entropy() for c in categorical]
entropy = torch.stack(entropy, dim=-1).sum(dim=-1)
result = {
'prev_neglogp' : torch.squeeze(prev_neglogp),
'logits' : [c.logits for c in categorical],
'values' : value,
'entropy' : torch.squeeze(entropy),
'rnn_states' : states
}
return result
else:
if action_masks is None:
categorical = [Categorical(logits=logit) for logit in logits]
else:
action_masks = np.split(action_masks, len(logits), axis=1)
categorical = [CategoricalMasked(logits=logit, masks=mask) for logit, mask in zip(logits, action_masks)]
selected_action = [c.sample().long() for c in categorical]
neglogp = [-c.log_prob(a.squeeze()) for c,a in zip(categorical, selected_action)]
selected_action = torch.stack(selected_action, dim=-1)
neglogp = torch.stack(neglogp, dim=-1).sum(dim=-1)
result = {
'neglogpacs' : torch.squeeze(neglogp),
'values' : self.denorm_value(value),
'actions' : selected_action,
'logits' : [c.logits for c in categorical],
'rnn_states' : states
}
return result
class ModelA2CContinuous(BaseModel):
def __init__(self, network):
BaseModel.__init__(self, 'a2c')
self.network_builder = network
class Network(BaseModelNetwork):
def __init__(self, a2c_network, **kwargs):
BaseModelNetwork.__init__(self, **kwargs)
self.a2c_network = a2c_network
def is_rnn(self):
return self.a2c_network.is_rnn()
def get_default_rnn_state(self):
return self.a2c_network.get_default_rnn_state()
def get_value_layer(self):
return self.a2c_network.get_value_layer()
def kl(self, p_dict, q_dict):
p = p_dict['mu'], p_dict['sigma']
q = q_dict['mu'], q_dict['sigma']
return divergence.d_kl_normal(p, q)
def forward(self, input_dict):
is_train = input_dict.get('is_train', True)
prev_actions = input_dict.get('prev_actions', None)
input_dict['obs'] = self.norm_obs(input_dict['obs'])
mu, sigma, value, states = self.a2c_network(input_dict)
distr = torch.distributions.Normal(mu, sigma, validate_args=False)
if is_train:
entropy = distr.entropy().sum(dim=-1)
prev_neglogp = -distr.log_prob(prev_actions).sum(dim=-1)
result = {
'prev_neglogp' : torch.squeeze(prev_neglogp),
'value' : value,
'entropy' : entropy,
'rnn_states' : states,
'mus' : mu,
'sigmas' : sigma
}
return result
else:
selected_action = distr.sample().squeeze()
neglogp = -distr.log_prob(selected_action).sum(dim=-1)
result = {
'neglogpacs' : torch.squeeze(neglogp),
'values' : self.denorm_value(value),
'actions' : selected_action,
'entropy' : entropy,
'rnn_states' : states,
'mus' : mu,
'sigmas' : sigma
}
return result
class ModelA2CContinuousLogStd(BaseModel):
def __init__(self, network):
BaseModel.__init__(self, 'a2c')
self.network_builder = network
class Network(BaseModelNetwork):
def __init__(self, a2c_network, **kwargs):
BaseModelNetwork.__init__(self, **kwargs)
self.a2c_network = a2c_network
def is_rnn(self):
return self.a2c_network.is_rnn()
def get_value_layer(self):
return self.a2c_network.get_value_layer()
def get_default_rnn_state(self):
return self.a2c_network.get_default_rnn_state()
def forward(self, input_dict):
is_train = input_dict.get('is_train', True)
prev_actions = input_dict.get('prev_actions', None)
input_dict['obs'] = self.norm_obs(input_dict['obs'])
mu, logstd, value, states = self.a2c_network(input_dict)
sigma = torch.exp(logstd)
distr = torch.distributions.Normal(mu, sigma, validate_args=False)
if is_train:
entropy = distr.entropy().sum(dim=-1)
prev_neglogp = self.neglogp(prev_actions, mu, sigma, logstd)
result = {
'prev_neglogp' : torch.squeeze(prev_neglogp),
'values' : value,
'entropy' : entropy,
'rnn_states' : states,
'mus' : mu,
'sigmas' : sigma
}
return result
else:
selected_action = distr.sample()
neglogp = self.neglogp(selected_action, mu, sigma, logstd)
result = {
'neglogpacs' : torch.squeeze(neglogp),
'values' : self.denorm_value(value),
'actions' : selected_action,
'rnn_states' : states,
'mus' : mu,
'sigmas' : sigma
}
return result
def neglogp(self, x, mean, std, logstd):
return 0.5 * (((x - mean) / std)**2).sum(dim=-1) \
+ 0.5 * np.log(2.0 * np.pi) * x.size()[-1] \
+ logstd.sum(dim=-1)
class ModelCentralValue(BaseModel):
def __init__(self, network):
BaseModel.__init__(self, 'a2c')
self.network_builder = network
class Network(BaseModelNetwork):
def __init__(self, a2c_network, **kwargs):
BaseModelNetwork.__init__(self, **kwargs)
self.a2c_network = a2c_network
def is_rnn(self):
return self.a2c_network.is_rnn()
def get_value_layer(self):
return self.a2c_network.get_value_layer()
def get_default_rnn_state(self):
return self.a2c_network.get_default_rnn_state()
def kl(self, p_dict, q_dict):
return None # or throw exception?
def forward(self, input_dict):
is_train = input_dict.get('is_train', True)
prev_actions = input_dict.get('prev_actions', None)
input_dict['obs'] = self.norm_obs(input_dict['obs'])
value, states = self.a2c_network(input_dict)
if not is_train:
value = self.denorm_value(value)
result = {
'values': value,
'rnn_states': states
}
return result
class ModelSACContinuous(BaseModel):
def __init__(self, network):
BaseModel.__init__(self, 'sac')
self.network_builder = network
class Network(BaseModelNetwork):
def __init__(self, sac_network,**kwargs):
BaseModelNetwork.__init__(self,**kwargs)
self.sac_network = sac_network
def critic(self, obs, action):
return self.sac_network.critic(obs, action)
def critic_target(self, obs, action):
return self.sac_network.critic_target(obs, action)
def actor(self, obs):
return self.sac_network.actor(obs)
def is_rnn(self):
return False
def forward(self, input_dict):
is_train = input_dict.pop('is_train', True)
mu, sigma = self.sac_network(input_dict)
dist = SquashedNormal(mu, sigma)
return dist