forked from openai/baselines
-
Notifications
You must be signed in to change notification settings - Fork 723
/
Copy pathdistributions.py
513 lines (393 loc) · 18 KB
/
distributions.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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
import numpy as np
import tensorflow as tf
from tensorflow.python.ops import math_ops
from gym import spaces
from stable_baselines.common.tf_layers import linear
class ProbabilityDistribution(object):
"""
Base class for describing a probability distribution.
"""
def __init__(self):
super(ProbabilityDistribution, self).__init__()
def flatparam(self):
"""
Return the direct probabilities
:return: ([float]) the probabilities
"""
raise NotImplementedError
def mode(self):
"""
Returns the probability
:return: (Tensorflow Tensor) the deterministic action
"""
raise NotImplementedError
def neglogp(self, x):
"""
returns the of the negative log likelihood
:param x: (str) the labels of each index
:return: ([float]) The negative log likelihood of the distribution
"""
# Usually it's easier to define the negative logprob
raise NotImplementedError
def kl(self, other):
"""
Calculates the Kullback-Leibler divergence from the given probability distribution
:param other: ([float]) the distribution to compare with
:return: (float) the KL divergence of the two distributions
"""
raise NotImplementedError
def entropy(self):
"""
Returns Shannon's entropy of the probability
:return: (float) the entropy
"""
raise NotImplementedError
def sample(self):
"""
returns a sample from the probability distribution
:return: (Tensorflow Tensor) the stochastic action
"""
raise NotImplementedError
def logp(self, x):
"""
returns the of the log likelihood
:param x: (str) the labels of each index
:return: ([float]) The log likelihood of the distribution
"""
return - self.neglogp(x)
class ProbabilityDistributionType(object):
"""
Parametrized family of probability distributions
"""
def probability_distribution_class(self):
"""
returns the ProbabilityDistribution class of this type
:return: (Type ProbabilityDistribution) the probability distribution class associated
"""
raise NotImplementedError
def proba_distribution_from_flat(self, flat):
"""
Returns the probability distribution from flat probabilities
flat: flattened vector of parameters of probability distribution
:param flat: ([float]) the flat probabilities
:return: (ProbabilityDistribution) the instance of the ProbabilityDistribution associated
"""
return self.probability_distribution_class()(flat)
def proba_distribution_from_latent(self, pi_latent_vector, vf_latent_vector, init_scale=1.0, init_bias=0.0):
"""
returns the probability distribution from latent values
:param pi_latent_vector: ([float]) the latent pi values
:param vf_latent_vector: ([float]) the latent vf values
:param init_scale: (float) the initial scale of the distribution
:param init_bias: (float) the initial bias of the distribution
:return: (ProbabilityDistribution) the instance of the ProbabilityDistribution associated
"""
raise NotImplementedError
def param_shape(self):
"""
returns the shape of the input parameters
:return: ([int]) the shape
"""
raise NotImplementedError
def sample_shape(self):
"""
returns the shape of the sampling
:return: ([int]) the shape
"""
raise NotImplementedError
def sample_dtype(self):
"""
returns the type of the sampling
:return: (type) the type
"""
raise NotImplementedError
def param_placeholder(self, prepend_shape, name=None):
"""
returns the TensorFlow placeholder for the input parameters
:param prepend_shape: ([int]) the prepend shape
:param name: (str) the placeholder name
:return: (TensorFlow Tensor) the placeholder
"""
return tf.placeholder(dtype=tf.float32, shape=prepend_shape + self.param_shape(), name=name)
def sample_placeholder(self, prepend_shape, name=None):
"""
returns the TensorFlow placeholder for the sampling
:param prepend_shape: ([int]) the prepend shape
:param name: (str) the placeholder name
:return: (TensorFlow Tensor) the placeholder
"""
return tf.placeholder(dtype=self.sample_dtype(), shape=prepend_shape + self.sample_shape(), name=name)
class CategoricalProbabilityDistributionType(ProbabilityDistributionType):
def __init__(self, n_cat):
"""
The probability distribution type for categorical input
:param n_cat: (int) the number of categories
"""
self.n_cat = n_cat
def probability_distribution_class(self):
return CategoricalProbabilityDistribution
def proba_distribution_from_latent(self, pi_latent_vector, vf_latent_vector, init_scale=1.0, init_bias=0.0):
pdparam = linear(pi_latent_vector, 'pi', self.n_cat, init_scale=init_scale, init_bias=init_bias)
q_values = linear(vf_latent_vector, 'q', self.n_cat, init_scale=init_scale, init_bias=init_bias)
return self.proba_distribution_from_flat(pdparam), pdparam, q_values
def param_shape(self):
return [self.n_cat]
def sample_shape(self):
return []
def sample_dtype(self):
return tf.int64
class MultiCategoricalProbabilityDistributionType(ProbabilityDistributionType):
def __init__(self, n_vec):
"""
The probability distribution type for multiple categorical input
:param n_vec: ([int]) the vectors
"""
# Cast the variable because tf does not allow uint32
self.n_vec = n_vec.astype(np.int32)
# Check that the cast was valid
assert (self.n_vec > 0).all(), "Casting uint32 to int32 was invalid"
def probability_distribution_class(self):
return MultiCategoricalProbabilityDistribution
def proba_distribution_from_flat(self, flat):
return MultiCategoricalProbabilityDistribution(self.n_vec, flat)
def proba_distribution_from_latent(self, pi_latent_vector, vf_latent_vector, init_scale=1.0, init_bias=0.0):
pdparam = linear(pi_latent_vector, 'pi', sum(self.n_vec), init_scale=init_scale, init_bias=init_bias)
q_values = linear(vf_latent_vector, 'q', sum(self.n_vec), init_scale=init_scale, init_bias=init_bias)
return self.proba_distribution_from_flat(pdparam), pdparam, q_values
def param_shape(self):
return [sum(self.n_vec)]
def sample_shape(self):
return [len(self.n_vec)]
def sample_dtype(self):
return tf.int64
class DiagGaussianProbabilityDistributionType(ProbabilityDistributionType):
def __init__(self, size):
"""
The probability distribution type for multivariate Gaussian input
:param size: (int) the number of dimensions of the multivariate gaussian
"""
self.size = size
def probability_distribution_class(self):
return DiagGaussianProbabilityDistribution
def proba_distribution_from_flat(self, flat):
"""
returns the probability distribution from flat probabilities
:param flat: ([float]) the flat probabilities
:return: (ProbabilityDistribution) the instance of the ProbabilityDistribution associated
"""
return self.probability_distribution_class()(flat)
def proba_distribution_from_latent(self, pi_latent_vector, vf_latent_vector, init_scale=1.0, init_bias=0.0):
mean = linear(pi_latent_vector, 'pi', self.size, init_scale=init_scale, init_bias=init_bias)
logstd = tf.get_variable(name='pi/logstd', shape=[1, self.size], initializer=tf.zeros_initializer())
pdparam = tf.concat([mean, mean * 0.0 + logstd], axis=1)
q_values = linear(vf_latent_vector, 'q', self.size, init_scale=init_scale, init_bias=init_bias)
return self.proba_distribution_from_flat(pdparam), mean, q_values
def param_shape(self):
return [2 * self.size]
def sample_shape(self):
return [self.size]
def sample_dtype(self):
return tf.float32
class BernoulliProbabilityDistributionType(ProbabilityDistributionType):
def __init__(self, size):
"""
The probability distribution type for Bernoulli input
:param size: (int) the number of dimensions of the Bernoulli distribution
"""
self.size = size
def probability_distribution_class(self):
return BernoulliProbabilityDistribution
def proba_distribution_from_latent(self, pi_latent_vector, vf_latent_vector, init_scale=1.0, init_bias=0.0):
pdparam = linear(pi_latent_vector, 'pi', self.size, init_scale=init_scale, init_bias=init_bias)
q_values = linear(vf_latent_vector, 'q', self.size, init_scale=init_scale, init_bias=init_bias)
return self.proba_distribution_from_flat(pdparam), pdparam, q_values
def param_shape(self):
return [self.size]
def sample_shape(self):
return [self.size]
def sample_dtype(self):
return tf.int32
class CategoricalProbabilityDistribution(ProbabilityDistribution):
def __init__(self, logits):
"""
Probability distributions from categorical input
:param logits: ([float]) the categorical logits input
"""
self.logits = logits
super(CategoricalProbabilityDistribution, self).__init__()
def flatparam(self):
return self.logits
def mode(self):
return tf.argmax(self.logits, axis=-1)
def neglogp(self, x):
# Note: we can't use sparse_softmax_cross_entropy_with_logits because
# the implementation does not allow second-order derivatives...
one_hot_actions = tf.one_hot(x, self.logits.get_shape().as_list()[-1])
return tf.nn.softmax_cross_entropy_with_logits_v2(
logits=self.logits,
labels=tf.stop_gradient(one_hot_actions))
def kl(self, other):
a_0 = self.logits - tf.reduce_max(self.logits, axis=-1, keepdims=True)
a_1 = other.logits - tf.reduce_max(other.logits, axis=-1, keepdims=True)
exp_a_0 = tf.exp(a_0)
exp_a_1 = tf.exp(a_1)
z_0 = tf.reduce_sum(exp_a_0, axis=-1, keepdims=True)
z_1 = tf.reduce_sum(exp_a_1, axis=-1, keepdims=True)
p_0 = exp_a_0 / z_0
return tf.reduce_sum(p_0 * (a_0 - tf.log(z_0) - a_1 + tf.log(z_1)), axis=-1)
def entropy(self):
a_0 = self.logits - tf.reduce_max(self.logits, axis=-1, keepdims=True)
exp_a_0 = tf.exp(a_0)
z_0 = tf.reduce_sum(exp_a_0, axis=-1, keepdims=True)
p_0 = exp_a_0 / z_0
return tf.reduce_sum(p_0 * (tf.log(z_0) - a_0), axis=-1)
def sample(self):
# Gumbel-max trick to sample
# a categorical distribution (see http://amid.fish/humble-gumbel)
uniform = tf.random_uniform(tf.shape(self.logits), dtype=self.logits.dtype)
return tf.argmax(self.logits - tf.log(-tf.log(uniform)), axis=-1)
@classmethod
def fromflat(cls, flat):
"""
Create an instance of this from new logits values
:param flat: ([float]) the categorical logits input
:return: (ProbabilityDistribution) the instance from the given categorical input
"""
return cls(flat)
class MultiCategoricalProbabilityDistribution(ProbabilityDistribution):
def __init__(self, nvec, flat):
"""
Probability distributions from multicategorical input
:param nvec: ([int]) the sizes of the different categorical inputs
:param flat: ([float]) the categorical logits input
"""
self.flat = flat
self.categoricals = list(map(CategoricalProbabilityDistribution, tf.split(flat, nvec, axis=-1)))
super(MultiCategoricalProbabilityDistribution, self).__init__()
def flatparam(self):
return self.flat
def mode(self):
return tf.stack([p.mode() for p in self.categoricals], axis=-1)
def neglogp(self, x):
return tf.add_n([p.neglogp(px) for p, px in zip(self.categoricals, tf.unstack(x, axis=-1))])
def kl(self, other):
return tf.add_n([p.kl(q) for p, q in zip(self.categoricals, other.categoricals)])
def entropy(self):
return tf.add_n([p.entropy() for p in self.categoricals])
def sample(self):
return tf.stack([p.sample() for p in self.categoricals], axis=-1)
@classmethod
def fromflat(cls, flat):
"""
Create an instance of this from new logits values
:param flat: ([float]) the multi categorical logits input
:return: (ProbabilityDistribution) the instance from the given multi categorical input
"""
raise NotImplementedError
class DiagGaussianProbabilityDistribution(ProbabilityDistribution):
def __init__(self, flat):
"""
Probability distributions from multivariate Gaussian input
:param flat: ([float]) the multivariate Gaussian input data
"""
self.flat = flat
mean, logstd = tf.split(axis=len(flat.shape) - 1, num_or_size_splits=2, value=flat)
self.mean = mean
self.logstd = logstd
self.std = tf.exp(logstd)
super(DiagGaussianProbabilityDistribution, self).__init__()
def flatparam(self):
return self.flat
def mode(self):
# Bounds are taken into account outside this class (during training only)
return self.mean
def neglogp(self, x):
return 0.5 * tf.reduce_sum(tf.square((x - self.mean) / self.std), axis=-1) \
+ 0.5 * np.log(2.0 * np.pi) * tf.cast(tf.shape(x)[-1], tf.float32) \
+ tf.reduce_sum(self.logstd, axis=-1)
def kl(self, other):
assert isinstance(other, DiagGaussianProbabilityDistribution)
return tf.reduce_sum(other.logstd - self.logstd + (tf.square(self.std) + tf.square(self.mean - other.mean)) /
(2.0 * tf.square(other.std)) - 0.5, axis=-1)
def entropy(self):
return tf.reduce_sum(self.logstd + .5 * np.log(2.0 * np.pi * np.e), axis=-1)
def sample(self):
# Bounds are taken into acount outside this class (during training only)
# Otherwise, it changes the distribution and breaks PPO2 for instance
return self.mean + self.std * tf.random_normal(tf.shape(self.mean),
dtype=self.mean.dtype)
@classmethod
def fromflat(cls, flat):
"""
Create an instance of this from new multivariate Gaussian input
:param flat: ([float]) the multivariate Gaussian input data
:return: (ProbabilityDistribution) the instance from the given multivariate Gaussian input data
"""
return cls(flat)
class BernoulliProbabilityDistribution(ProbabilityDistribution):
def __init__(self, logits):
"""
Probability distributions from Bernoulli input
:param logits: ([float]) the Bernoulli input data
"""
self.logits = logits
self.probabilities = tf.sigmoid(logits)
super(BernoulliProbabilityDistribution, self).__init__()
def flatparam(self):
return self.logits
def mode(self):
return tf.round(self.probabilities)
def neglogp(self, x):
return tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits,
labels=tf.cast(x, tf.float32)),
axis=-1)
def kl(self, other):
return tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits=other.logits,
labels=self.probabilities), axis=-1) - \
tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits,
labels=self.probabilities), axis=-1)
def entropy(self):
return tf.reduce_sum(tf.nn.sigmoid_cross_entropy_with_logits(logits=self.logits,
labels=self.probabilities), axis=-1)
def sample(self):
samples_from_uniform = tf.random_uniform(tf.shape(self.probabilities))
return tf.cast(math_ops.less(samples_from_uniform, self.probabilities), tf.float32)
@classmethod
def fromflat(cls, flat):
"""
Create an instance of this from new Bernoulli input
:param flat: ([float]) the Bernoulli input data
:return: (ProbabilityDistribution) the instance from the given Bernoulli input data
"""
return cls(flat)
def make_proba_dist_type(ac_space):
"""
return an instance of ProbabilityDistributionType for the correct type of action space
:param ac_space: (Gym Space) the input action space
:return: (ProbabilityDistributionType) the appropriate instance of a ProbabilityDistributionType
"""
if isinstance(ac_space, spaces.Box):
assert len(ac_space.shape) == 1, "Error: the action space must be a vector"
return DiagGaussianProbabilityDistributionType(ac_space.shape[0])
elif isinstance(ac_space, spaces.Discrete):
return CategoricalProbabilityDistributionType(ac_space.n)
elif isinstance(ac_space, spaces.MultiDiscrete):
return MultiCategoricalProbabilityDistributionType(ac_space.nvec)
elif isinstance(ac_space, spaces.MultiBinary):
return BernoulliProbabilityDistributionType(ac_space.n)
else:
raise NotImplementedError("Error: probability distribution, not implemented for action space of type {}."
.format(type(ac_space)) +
" Must be of type Gym Spaces: Box, Discrete, MultiDiscrete or MultiBinary.")
def shape_el(tensor, index):
"""
get the shape of a TensorFlow Tensor element
:param tensor: (TensorFlow Tensor) the input tensor
:param index: (int) the element
:return: ([int]) the shape
"""
maybe = tensor.get_shape()[index]
if maybe is not None:
return maybe
else:
return tf.shape(tensor)[index]