/
distribution.py
442 lines (320 loc) · 13.2 KB
/
distribution.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
import copy
from chainer import backend
class Distribution(object):
"""Interface of Distribution
:class:`Distribution` is a bass class for dealing with probability
distributions.
This class provides the following capabilities.
1. Sampling random points.
2. Evaluating a probability-related function at a given realization \
value. (e.g., probability density function, probability mass function)
3. Obtaining properties of distributions. (e.g., mean, variance)
Note that every method and property that computes them from
:class:`chainer.Variable` can basically be differentiated.
In this class, sampled random points and realization values given in
probability-related function is called *sample*. Sample consists of
*batches*, and each batch consists of independent *events*. Each event
consists of values, and each value in an event cannot be sampled
independently in general. Each event in a batch is independent while it is
not sampled from an identical distribution. And each batch in sample is
sampled from an identical distribution.
Each part of the sample-batch-event hierarchy has its own shape, which is
called ``sample_shape``, ``batch_shape``, and ``event_shape``,
respectively.
On initialization, it takes distribution-specific parameters as inputs.
:attr:`batch_shape` and :attr:`event_shape` is decided by the shape of
the parameter when generating an instance of a class.
.. admonition:: Example
The following code is an example of sample-batch-event hierarchy on
using :class:`~distributions.MultivariateNormal` distribution. This
makes 2d normal distributions. ``dist`` consists of 12(4 * 3)
independent 2d normal distributions. And on initialization,
:attr:`batch_shape` and :attr:`event_shape` is decided.
>>> import chainer
>>> import chainer.distributions as D
>>> import numpy as np
>>> d = 2
>>> shape = (4, 3)
>>> loc = np.random.normal(
... size=shape + (d,)).astype(np.float32)
>>> cov = np.random.normal(size=shape + (d, d)).astype(np.float32)
>>> cov = np.matmul(cov, np.rollaxis(cov, -1, -2))
>>> l = np.linalg.cholesky(cov)
>>> dist = D.MultivariateNormal(loc, scale_tril=l)
>>> dist.event_shape
(2,)
>>> dist.batch_shape
(4, 3)
>>> sample = dist.sample(sample_shape=(6, 5))
>>> sample.shape
(6, 5, 4, 3, 2)
Every probability-related function takes realization value whose shape is
the concatenation of ``sample_shape``, ``batch_shape``, and
``event_shape`` and returns an evaluated value whose shape is the
concatenation of ``sample_shape``, and ``batch_shape``.
"""
def _copy_to(self, target):
target.__dict__ = copy.copy(self.__dict__)
return target
@property
def batch_shape(self):
"""Returns the shape of a batch.
Returns:
tuple: The shape of a sample that is not identical and independent.
"""
raise NotImplementedError
def cdf(self, x):
"""Evaluates the cumulative distribution function at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Cumulative distribution function value evaluated
at `x`.
"""
raise NotImplementedError
@property
def covariance(self):
"""Returns the covariance of the distribution.
Returns:
~chainer.Variable: The covariance of the distribution.
"""
raise NotImplementedError
@property
def entropy(self):
"""Returns the entropy of the distribution.
Returns:
~chainer.Variable: The entropy of the distribution.
"""
raise NotImplementedError
@property
def event_shape(self):
"""Returns the shape of an event.
Returns:
tuple: The shape of a sample that is not identical and independent.
"""
raise NotImplementedError
def icdf(self, x):
"""Evaluates the inverse cumulative distribution function at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Inverse cumulative distribution function value
evaluated at `x`.
"""
raise NotImplementedError
def log_cdf(self, x):
"""Evaluates the log of cumulative distribution function at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Logarithm of cumulative distribution function
value evaluated at `x`.
"""
raise NotImplementedError
def log_prob(self, x):
"""Evaluates the logarithm of probability at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Logarithm of probability evaluated at `x`.
"""
raise NotImplementedError
def log_survival_function(self, x):
"""Evaluates the logarithm of survival function at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Logarithm of survival function value evaluated
at `x`.
"""
raise NotImplementedError
@property
def mean(self):
"""Returns the mean of the distribution.
Returns:
~chainer.Variable: The mean of the distribution.
"""
raise NotImplementedError
@property
def mode(self):
"""Returns the mode of the distribution.
Returns:
~chainer.Variable: The mode of the distribution.
"""
raise NotImplementedError
@property
def params(self):
"""Returns the parameters of the distribution.
Returns:
dict: The parameters of the distribution.
"""
raise NotImplementedError
def perplexity(self, x):
"""Evaluates the perplexity function at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Perplexity function value evaluated at `x`.
"""
raise NotImplementedError
def prob(self, x):
"""Evaluates probability at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Probability evaluated at `x`.
"""
raise NotImplementedError
def sample(self, sample_shape=()):
"""Samples random points from the distribution.
This function calls `sample_n` and reshapes a result of `sample_n` to
`sample_shape + batch_shape + event_shape`. On implementing sampling
code in an inherited distribution class, it is not recommended that
you override this function. Instead of doing this, it is preferable to
override `sample_n`.
Args:
sample_shape(:class:`tuple` of :class:`int`): Sampling shape.
Returns:
~chainer.Variable: Sampled random points.
"""
final_shape = self.batch_shape + self.event_shape
if sample_shape == ():
n = 1
elif isinstance(sample_shape, int):
n = sample_shape
final_shape = (n,) + final_shape
else:
n = 1
for shape_ in sample_shape:
n *= shape_
final_shape = sample_shape + final_shape
samples = self.sample_n(n)
return samples.reshape(final_shape)
def sample_n(self, n):
"""Samples n random points from the distribution.
This function returns sampled points whose shape is
`(n,) + batch_shape + event_shape`. When implementing sampling code in
a subclass, it is recommended that you override this method.
Args:
n(int): Sampling size.
Returns:
~chainer.Variable: sampled random points.
"""
raise NotImplementedError
@property
def stddev(self):
"""Returns the standard deviation of the distribution.
Returns:
~chainer.Variable: The standard deviation of the distribution.
"""
raise NotImplementedError
@property
def support(self):
"""Returns the support of the distribution.
Returns:
str: String that means support of this distribution.
"""
raise NotImplementedError
def survival_function(self, x):
"""Evaluates the survival function at the given points.
Args:
x (:class:`~chainer.Variable` or :ref:`ndarray`): Data points in
the domain of the distribution
Returns:
~chainer.Variable: Survival function value evaluated at `x`.
"""
raise NotImplementedError
@property
def variance(self):
"""Returns the variance of the distribution.
Returns:
~chainer.Variable: The variance of the distribution.
"""
raise NotImplementedError
@property
def xp(self):
"""Array module for the distribution.
Depending on which of CPU/GPU this distribution is on, this property
returns :mod:`numpy` or :mod:`cupy`.
"""
return backend.get_array_module(*self.params.values())
_KLDIVERGENCE = {}
def register_kl(Dist1, Dist2):
"""Decorator to register KL divergence function.
This decorator registers a function which computes Kullback-Leibler
divergence. This function will be called by :func:`~chainer.kl_divergence`
based on the argument types.
Args:
Dist1(`type`): type of a class inherit from
:class:`~chainer.Distribution` to calculate KL divergence.
Dist2(`type`): type of a class inherit from
:class:`~chainer.Distribution` to calculate KL divergence.
The decorated functoion takes an instance of ``Dist1`` and ``Dist2`` and
returns KL divergence value.
.. admonition:: Example
This is a simple example to register KL divergence. A function to
calculate a KL divergence value between an instance of ``Dist1`` and
an instance of ``Dist2`` is registered.
.. code-block:: python
from chainer import distributions
@distributions.register_kl(Dist1, Dist2)
def _kl_dist1_dist2(dist1, dist2):
return KL
"""
def f(kl):
_KLDIVERGENCE[Dist1, Dist2] = kl
return f
def kl_divergence(dist1, dist2):
"""Computes Kullback-Leibler divergence.
For two continuous distributions :math:`p(x), q(x)`, it is expressed as
.. math::
D_{KL}(p||q) = \\int p(x) \\log \\frac{p(x)}{q(x)} dx
For two discrete distributions :math:`p(x), q(x)`, it is expressed as
.. math::
D_{KL}(p||q) = \\sum_x p(x) \\log \\frac{p(x)}{q(x)}
Args:
dist1(:class:`~chainer.Distribution`): Distribution to calculate KL
divergence :math:`p`. This is the first (left) operand of the KL
divergence.
dist2(:class:`~chainer.Distribution`): Distribution to calculate KL
divergence :math:`q`. This is the second (right) operand of the KL
divergence.
Returns:
~chainer.Variable: Output variable representing kl divergence
:math:`D_{KL}(p||q)`.
Using :func:`~chainer.register_kl`, we can define behavior of
:func:`~chainer.kl_divergence` for any two distributions.
"""
return _KLDIVERGENCE[type(dist1), type(dist2)](dist1, dist2)
def cross_entropy(dist1, dist2):
"""Computes Cross entropy.
For two continuous distributions :math:`p(x), q(x)`, it is expressed as
.. math::
H(p,q) = - \\int p(x) \\log q(x) dx
For two discrete distributions :math:`p(x), q(x)`, it is expressed as
.. math::
H(p,q) = - \\sum_x p(x) \\log q(x)
This function call :func:`~chainer.kl_divergence` and
:meth:`~chainer.Distribution.entropy` of ``dist1``. Therefore, it is
necessary to register KL divergence function with
:func:`~chainer.register_kl` decoartor and define
:meth:`~chainer.Distribution.entropy` in ``dist1``.
Args:
dist1(:class:`~chainer.Distribution`): Distribution to calculate cross
entropy :math:`p`. This is the first (left) operand of the cross
entropy.
dist2(:class:`~chainer.Distribution`): Distribution to calculate cross
entropy :math:`q`. This is the second (right) operand of the cross
entropy.
Returns:
~chainer.Variable: Output variable representing cross entropy
:math:`H(p,q)`.
"""
return dist1.entropy() + kl_divergence(dist1, dist2)