-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel_autoregressive_custom.py
326 lines (274 loc) · 12.3 KB
/
model_autoregressive_custom.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
from gym.spaces import Discrete, Tuple
from ray.rllib.models.tf.misc import normc_initializer
from ray.rllib.models.tf.tf_modelv2 import TFModelV2
from ray.rllib.models.torch.misc import normc_initializer as normc_init_torch
from ray.rllib.models.torch.misc import SlimFC
from ray.rllib.models.torch.torch_modelv2 import TorchModelV2
from ray.rllib.utils.framework import try_import_tf, try_import_torch
import gym
tf1, tf, tfv = try_import_tf()
torch, nn = try_import_torch()
class AutoregressiveActionModel(TFModelV2):
"""Implements the `.action_model` branch required above."""
def __init__(self, obs_space, action_space, num_outputs, model_config, name):
super(AutoregressiveActionModel, self).__init__(
obs_space, action_space, num_outputs, model_config, name
)
if action_space != Tuple([Discrete(2), Discrete(2)]):
raise ValueError("This model only supports the [2, 2] action space")
# Inputs
obs_input = tf.keras.layers.Input(shape=obs_space.shape, name="obs_input")
a1_input = tf.keras.layers.Input(shape=(1,), name="a1_input")
ctx_input = tf.keras.layers.Input(shape=(num_outputs,), name="ctx_input")
# Output of the model (normally 'logits', but for an autoregressive
# dist this is more like a context/feature layer encoding the obs)
context = tf.keras.layers.Dense(
num_outputs,
name="hidden",
activation=tf.nn.tanh,
kernel_initializer=normc_initializer(1.0),
)(obs_input)
# V(s)
value_out = tf.keras.layers.Dense(
1,
name="value_out",
activation=None,
kernel_initializer=normc_initializer(0.01),
)(context)
# P(a1 | obs)
a1_logits = tf.keras.layers.Dense(
2,
name="a1_logits",
activation=None,
kernel_initializer=normc_initializer(0.01),
)(ctx_input)
# P(a2 | a1)
# --note: typically you'd want to implement P(a2 | a1, obs) as follows:
# a2_context = tf.keras.layers.Concatenate(axis=1)(
# [ctx_input, a1_input])
a2_context = a1_input
a2_hidden = tf.keras.layers.Dense(
16,
name="a2_hidden",
activation=tf.nn.tanh,
kernel_initializer=normc_initializer(1.0),
)(a2_context)
a2_logits = tf.keras.layers.Dense(
2,
name="a2_logits",
activation=None,
kernel_initializer=normc_initializer(0.01),
)(a2_hidden)
# Base layers
self.base_model = tf.keras.Model(obs_input, [context, value_out])
self.base_model.summary()
# Autoregressive action sampler
self.action_model = tf.keras.Model(
[ctx_input, a1_input], [a1_logits, a2_logits]
)
self.action_model.summary()
def forward(self, input_dict, state, seq_lens):
context, self._value_out = self.base_model(input_dict["obs"])
return context, state
def value_function(self):
return tf.reshape(self._value_out, [-1])
class TorchAutoregressiveActionModel(TorchModelV2, nn.Module):
"""PyTorch version of the AutoregressiveActionModel above."""
def __init__(self, obs_space, action_space, num_outputs, model_config, name):
TorchModelV2.__init__(
self, obs_space, action_space, num_outputs, model_config, name
)
nn.Module.__init__(self)
#if action_space != Tuple([Discrete(2), Discrete(2)]):
# raise ValueError("This model only supports the [2, 2] action space")
self.action_space_dim_dict = self.generate_action_space_dim_dict()
#print(f"OBS SPACE {obs_space}")
#print(f"Action SPACE {action_space}")
#HERE WE HAVE AN OUTPUT LAYER action of the size of the parametervector
self.num_outputs_action_distr = 5
# Output of the model (normally 'logits', but for an autoregressive
# dist this is more like a context/feature layer encoding the obs)
self.context_layer = SlimFC(
in_size=obs_space.shape[0],
out_size=num_outputs,
initializer=normc_init_torch(1.0),
activation_fn=nn.Tanh,
)
# V(s)
self.value_branch = SlimFC(
in_size=num_outputs,
out_size=1,
initializer=normc_init_torch(0.01),
activation_fn=None,
)
# P(a1 | obs)
self.a1_logits = SlimFC(
in_size=num_outputs,
out_size=self.action_space_dim_dict.get("a_1"), #self.num_outputs_action_distr, #this was 2 before
activation_fn=None,
initializer=normc_init_torch(0.01),
)
class _ActionModel(nn.Module):
def __init__(self, action_space_dim_dict):
nn.Module.__init__(self)
self.a2_hidden = SlimFC(
in_size=action_space_dim_dict.get("a_1"),
out_size=16,
activation_fn=nn.Tanh,
initializer=normc_init_torch(1.0),
)
self.a2_logits = SlimFC(
in_size=16,
out_size=action_space_dim_dict.get("a_2"),#self.num_outputs_action_distr, #this was 2 before
activation_fn=None,
initializer=normc_init_torch(0.01),
)
def forward(self_, ctx_input, a1_input):
#WE PASS "self_" as the instance, i.e. the _ActionModel, "self" still goes on the TorchAutoregressiveActionModel
#print("INPUT SHAPE")
#print(ctx_input.shape)
print("TEST")
print(ctx_input.device)
print(a1_input.device)
print(next(self.parameters().device))
print(next(self_.parameters().device))
print("Finished Test")
a1_logits = self.a1_logits(ctx_input)
#print("INPUT A1")
#print(a1_input.shape)
#print(a1_input)
a2_logits = self_.a2_logits(self_.a2_hidden(a1_input))
return a1_logits, a2_logits
# P(a2 | a1)
# --note: typically you'd want to implement P(a2 | a1, obs) as follows:
# a2_context = tf.keras.layers.Concatenate(axis=1)(
# [ctx_input, a1_input])
self.action_module = _ActionModel(self.action_space_dim_dict)
self._context = None
def forward(self, input_dict, state, seq_lens):
#this is passed as the "context" to the distrbution as "input" (and from the distribution it is then again passed in the _Action model)
self._context = self.context_layer(input_dict["obs"])
return self._context, state
def value_function(self):
return torch.reshape(self.value_branch(self._context), [-1])
def generate_action_space_dim_dict(self):
action_space_dim_dict = {}
if isinstance(self.action_space, gym.spaces.Dict):
for key, value in self.action_space.spaces.items():
action_space_dim_dict[key] = value.shape[0]
return action_space_dim_dict
class TorchAutoregressiveActionModelV2(TorchModelV2, nn.Module):
"""PyTorch version of the AutoregressiveActionModel above."""
def __init__(self, obs_space, action_space, num_outputs, model_config, name):
TorchModelV2.__init__(
self, obs_space, action_space, num_outputs, model_config, name
)
nn.Module.__init__(self)
#if action_space != Tuple([Discrete(2), Discrete(2)]):
# raise ValueError("This model only supports the [2, 2] action space")
self.action_space_dim_dict = self.generate_action_space_dim_dict()
#print(f"OBS SPACE {obs_space}")
#print(f"Action SPACE {action_space}")
#HERE WE HAVE AN OUTPUT LAYER action of the size of the parametervector
self.num_outputs_action_distr = 5
# Output of the model (normally 'logits', but for an autoregressive
# dist this is more like a context/feature layer encoding the obs)
self.context_layer = SlimFC(
in_size=obs_space.shape[0],
out_size=num_outputs,
initializer=normc_init_torch(1.0),
activation_fn=nn.Tanh,
)
# V(s)
self.value_branch = SlimFC(
in_size=num_outputs,
out_size=1,
initializer=normc_init_torch(0.01),
activation_fn=None,
)
# P(a1 | obs)
self.a1_logits = SlimFC(
in_size=num_outputs,
out_size=self.action_space_dim_dict.get("a_1"), #self.num_outputs_action_distr, #this was 2 before
activation_fn=None,
initializer=normc_init_torch(0.01),
)
self.a2_hidden = SlimFC(
in_size=self.action_space_dim_dict.get("a_1"),
out_size=16,
activation_fn=nn.Tanh,
initializer=normc_init_torch(1.0),
)
self.a2_logits = SlimFC(
in_size=16,
out_size=self.action_space_dim_dict.get("a_2"), # self.num_outputs_action_distr, #this was 2 before
activation_fn=None,
initializer=normc_init_torch(0.01),
)
"""
class _ActionModel(nn.Module):
def __init__(self, action_space_dim_dict):
nn.Module.__init__(self)
self.a2_hidden = SlimFC(
in_size=action_space_dim_dict.get("a_1"),
out_size=16,
activation_fn=nn.Tanh,
initializer=normc_init_torch(1.0),
)
self.a2_logits = SlimFC(
in_size=16,
out_size=action_space_dim_dict.get("a_2"),#self.num_outputs_action_distr, #this was 2 before
activation_fn=None,
initializer=normc_init_torch(0.01),
)
def forward_action_model(self_, ctx_input, a1_input):
#WE PASS "self_" as the instance, i.e. the _ActionModel, "self" still goes on the TorchAutoregressiveActionModel
#print("INPUT SHAPE")
#print(ctx_input.shape)
print("TEST")
print(ctx_input.device)
print(a1_input.device)
print(next(self.parameters().device))
print(next(self_.parameters().device))
print("Finished Test")
a1_logits = self.a1_logits(ctx_input)
#print("INPUT A1")
#print(a1_input.shape)
#print(a1_input)
a2_logits = self_.a2_logits(self_.a2_hidden(a1_input))
return a1_logits, a2_logits
""" or None
# P(a2 | a1)
# --note: typically you'd want to implement P(a2 | a1, obs) as follows:
# a2_context = tf.keras.layers.Concatenate(axis=1)(
# [ctx_input, a1_input])
#self.action_module = _ActionModel(self.action_space_dim_dict)
self._context = None
def forward(self, input_dict, state, seq_lens):
#this is passed as the "context" to the distrbution as "input" (and from the distribution it is then again passed in the _Action model)
self._context = self.context_layer(input_dict["obs"])
return self._context, state
def forward_action_model(self, ctx_input, a1_input):
# WE PASS "self_" as the instance, i.e. the _ActionModel, "self" still goes on the TorchAutoregressiveActionModel
# print("INPUT SHAPE")
# print(ctx_input.shape)
#print("TEST")
#print(ctx_input.device)
#print(a1_input.device)
#print(next(self.parameters().device))
#print(next(self_.parameters().device))
#print("Finished Test")
a1_logits = self.a1_logits(ctx_input)
# print("INPUT A1")
# print(a1_input.shape)
# print(a1_input)
a2_logits = self.a2_logits(self.a2_hidden(a1_input))
return a1_logits, a2_logits
def value_function(self):
return torch.reshape(self.value_branch(self._context), [-1])
def generate_action_space_dim_dict(self):
action_space_dim_dict = {}
if isinstance(self.action_space, gym.spaces.Dict):
for key, value in self.action_space.spaces.items():
action_space_dim_dict[key] = value.shape[0]
return action_space_dim_dict