forked from openai/random-network-distillation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstochastic_policy.py
260 lines (216 loc) · 9.93 KB
/
stochastic_policy.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
import tensorflow as tf
from baselines.common.distributions import make_pdtype
from collections import OrderedDict
from gym import spaces
def canonical_dtype(orig_dt):
if orig_dt.kind == 'f':
return tf.float32
elif orig_dt.kind in 'iu':
return tf.int32
else:
raise NotImplementedError
class StochasticPolicy(object):
def __init__(self, scope, ob_space, ac_space):
self.abs_scope = (tf.get_variable_scope().name + '/' + scope).lstrip('/')
self.ob_space = ob_space
self.ac_space = ac_space
self.pdtype = make_pdtype(ac_space)
self.ph_new = tf.placeholder(dtype=tf.float32, shape=(None, None), name='new')
self.ph_ob_keys = []
self.ph_ob_dtypes = {}
shapes = {}
if isinstance(ob_space, spaces.Dict):
assert isinstance(ob_space.spaces, OrderedDict)
for key, box in ob_space.spaces.items():
assert isinstance(box, spaces.Box)
self.ph_ob_keys.append(key)
# Keys must be ordered, because tf.concat(ph) depends on order. Here we don't keep OrderedDict
# order and sort keys instead. Rationale is to give freedom to modify environment.
self.ph_ob_keys.sort()
for k in self.ph_ob_keys:
self.ph_ob_dtypes[k] = ob_space.spaces[k].dtype
shapes[k] = ob_space.spaces[k].shape
else:
#print(ob_space)
box = ob_space
assert isinstance(box, spaces.Box)
self.ph_ob_keys = [None]
self.ph_ob_dtypes = {None: box.dtype}
shapes = {None: box.shape}
self.ph_ob = OrderedDict([(k, tf.placeholder(canonical_dtype(self.ph_ob_dtypes[k]),
(None, None,) + tuple(shapes[k]),
name=('obs/{}'.format(k) if k is not None else
'obs'))) for k in self.ph_ob_keys])
assert list(self.ph_ob.keys()) == self.ph_ob_keys, "\n{}\n{}\n".format(list(self.ph_ob.keys()), self.ph_ob_keys)
ob_shape = tf.shape(next(iter(self.ph_ob.values())))
self.sy_nenvs = ob_shape[0]
self.sy_nsteps = ob_shape[1]
self.ph_ac = self.pdtype.sample_placeholder([None, None], name='ac')
self.pd = self.vpred = self.ph_istate = None
def rel_to_abs(self, X):
"""
Code adapted from Attention Augmented Convolutional Networks:
https://arxiv.org/abs/1904.09925
Converts tensor from relative to absolute indexing
"""
# [B, Nh, L, 2L-1]
B, Nh, L, _ = X.shape
# Pad to shift from relative to absolute indexing
B = B if B.value is not None else -1
# ph_pad = tf.placeholder(tf.float32, shape=[None, Nh, L, 1])
# col_pad = tf.zeros_like(ph_pad)
# X = tf.concat([X, col_pad], axis=3)
# flat_x = tf.reshape(X, [B, Nh, L * 2 * L])
# ph_flat_pad = tf.placeholder(tf.float32, shape=[None, Nh, L-1])
# flat_pad = tf.zeros_like(ph_flat_pad)
# flat_x_padded = tf.concat([flat_x, flat_pad], axis=2)
col_pad = tf.constant([[0, 0], [0, 0], [0, 0], [0, 1]])
X = tf.pad(X, col_pad, "CONSTANT")
flat_x = tf.reshape(X, [B, Nh, L * 2 * L])
flat_pad = tf.constant([[0, 0], [0, 0], [0, L.value-1]])
flat_x_padded = tf.pad(flat_x, flat_pad, "CONSTANT")
# Reshape and slice out the padded elements.
final_x = tf.reshape(flat_x_padded, [B, Nh, L+1, (2*L) - 1])
final_x = final_x[:, :, :L, L-1:]
return final_x
def relative_logits_1d(self, q, rel_k, H, W, Nh, transpose_mask):
"""
Code adapted from Attention Augmented Convolutional Networks:
https://arxiv.org/abs/1904.09925
Compute relative logits along one dimension.
"""
# [B, Nh, H, W, 2*W-1]
rel_logits = tf.einsum('bhxyd,md->bhxym', q, rel_k)
# Collapse height and heads
rel_logits = tf.reshape(rel_logits, [-1, Nh*H, W, 2*W-1])
rel_logits = self.rel_to_abs(rel_logits)
# Shape it back and tile height times
rel_logits = tf.reshape(rel_logits, [-1, Nh, H, W, W])
rel_logits = tf.expand_dims(rel_logits, axis=3)
rel_logits = tf.tile(rel_logits, [1, 1, 1, H, 1, 1])
# Reshape for adding to the attention logits
rel_logits = tf.transpose(rel_logits, transpose_mask)
rel_logits = tf.reshape(rel_logits, [-1, Nh, H*W, H*W])
return rel_logits
def relative_logits(self, q, Nh):
"""
Code adapted from Attention Augmented Convolutional Networks:
https://arxiv.org/abs/1904.09925
Compute relative position logits.
"""
# [B, Nh, H, W, dk]
dk = q.shape[-1]
H = q.shape[2]
W = q.shape[3]
# Relative logits in width dimension
# stddev=dk.value ** -0.5 ?
# key_rel_w = tf.get_variable('key_rel_w', shape=(2*W-1, dk),
# initializer=tf.random_normal_initializer(dk.value ** -0.5))
key_rel_w = tf.get_variable('key_rel_w', shape=(2*W-1, dk),
initializer=tf.random_normal_initializer(stddev=dk.value ** -0.5))
rel_logits_w = self.relative_logits_1d(q, key_rel_w, H, W, Nh, [0, 1, 2, 4, 3, 5])
# Relative logits in height dimension.
# For ease, we transpose height and width and repeat the above steps, and transpose to
# eventually put the logits in the correct position
# key_rel_h = tf.get_variable('key_rel_h', shape=(2*H-1, dk),
# initializer=tf.random_normal_initializer(dk.value ** -0.5))
key_rel_h = tf.get_variable('key_rel_h', shape=(2*H-1, dk),
initializer=tf.random_normal_initializer(stddev=dk.value ** -0.5))
rel_logits_h = self.relative_logits_1d(tf.transpose(q, [0, 1, 3, 2, 4]),
key_rel_h, W, H, Nh, [0, 1, 4, 2, 5, 3])
return rel_logits_h, rel_logits_w
def split_heads_2d(self, inputs, Nh):
"""
Code adapted from Attention Augmented Convolutional Networks:
https://arxiv.org/abs/1904.09925
Split channels into multiple heads.
"""
s = inputs.shape[:-1].as_list()
s = [x if x is not None else -1 for x in s]
channels = inputs.shape[-1].value
ret_shape = s + [Nh, channels // Nh]
split = tf.reshape(inputs, ret_shape)
return tf.transpose(split, [0, 3, 1, 2, 4])
def combine_heads_2d(self, inputs):
"""
Code adapted from Attention Augmented Convolutional Networks:
https://arxiv.org/abs/1904.09925
Combine heads (inverse of split_heads_2d)
"""
transposed = tf.transpose(inputs, [0, 2, 3, 1, 4])
a, b = transposed.shape[-2:].as_list()
ret_shape = transposed.shape[:-2].as_list() + [a*b]
ret_shape = [x if x is not None else -1 for x in ret_shape]
# return tf.reshape(transposed, ret_shape, name='attention_output_combined')
return tf.reshape(transposed, ret_shape)
def compute_flat_qkv(self, inputs, dk, dv, Nh):
"""
Code adapted from Attention Augmented Convolutional Networks:
https://arxiv.org/abs/1904.09925
Compute flattened queries, keys and values.
"""
N, H, W, _ = inputs.shape
qkv = tf.layers.conv2d(inputs, (2*dk) + dv, 1)
q, k, v = tf.split(qkv, [dk, dk, dv], axis=3)
q = self.split_heads_2d(q, Nh)
k = self.split_heads_2d(k, Nh)
v = self.split_heads_2d(v, Nh)
# Scale query
dkh = dk // Nh
dvh = dv // Nh
q *= (dkh ** -0.5)
B = N if N.value is not None else -1
flat_q = tf.reshape(q, [B, Nh, H * W, dkh])
flat_k = tf.reshape(k, [B, Nh, H * W, dkh])
flat_v = tf.reshape(v, [B, Nh, H * W, dvh])
return flat_q, flat_k, flat_v, q
def augmented_conv2d(self, X, Fout, k=1, dk=256, dv=256, Nh=8, relative=True):
"""
Code adapted from Attention Augmented Convolutional Networks:
https://arxiv.org/abs/1904.09925
Results with:
dk=256; dv=256; Fout=512 generally best results
dk=96; Fout=352
dk=24; Fout=256
"""
if (dk % Nh != 0) or (dv % Nh != 0):
raise ValueError("dk or dv is not divisible by Nh")
if (dk // Nh < 1) or (dv // Nh < 1):
raise ValueError("(dk or dv) / Nh cannot be less then 1")
# X has shape [B, H, W, Fin]
B, H, W, _ = X.shape
conv_out = tf.layers.conv2d(X, Fout - dv, k)
# [B, Nh, HW, dvh or dkh]
flat_q, flat_k, flat_v, q = self.compute_flat_qkv(X, dk, dv, Nh)
# [B, Nh, HW, HW]
logits = tf.matmul(flat_q, flat_k, transpose_b=True)
if relative:
h_rel_logits, w_rel_logits = self.relative_logits(q, Nh)
logits += h_rel_logits
logits += w_rel_logits
weights = tf.nn.softmax(logits)
# [B, Nh, HW, dvh]
attn_out = tf.matmul(weights, flat_v)
B = B if B.value is not None else -1
attn_out = tf.reshape(attn_out, [B, Nh, H, W, dv // Nh])
attn_out = self.combine_heads_2d(attn_out)
attn_out = tf.layers.conv2d(attn_out, dv, 1, name='attention_output_combined')
return tf.concat([conv_out, attn_out], axis=3)
def finalize(self, pd, vpred, ph_istate=None): #pylint: disable=W0221
self.pd = pd
self.vpred = vpred
self.ph_istate = ph_istate
def ensure_observation_is_dict(self, ob):
if self.ph_ob_keys == [None]:
return { None: ob }
else:
return ob
def call(self, ob, new, istate):
"""
Return acs, vpred, neglogprob, nextstate
"""
raise NotImplementedError
def initial_state(self, n):
raise NotImplementedError
def update_normalization(self, ob):
pass