-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathdistributions.py
172 lines (143 loc) · 4.92 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
from __future__ import print_function
from __future__ import absolute_import
import itertools
import tensorflow as tf
import numpy as np
TINY = 1e-8
floatX = np.float32
class Distribution(object):
@property
def dist_flat_dim(self):
"""
:rtype: int
"""
raise NotImplementedError
@property
def dim(self):
"""
:rtype: int
"""
raise NotImplementedError
@property
def effective_dim(self):
"""
The effective dimension when used for rescaling quantities. This can be different from the
actual dimension when the actual values are using redundant representations (e.g. for categorical
distributions we encode it in onehot representation)
:rtype: int
"""
raise NotImplementedError
def kl_prior(self, dist_info):
return self.kl(dist_info, self.prior_dist_info(dist_info.values()[0].get_shape()[0]))
def logli(self, x_var, dist_info):
"""
:param x_var:
:param dist_info:
:return: log likelihood of the data
"""
raise NotImplementedError
def logli_prior(self, x_var):
return self.logli(x_var, self.prior_dist_info(x_var.get_shape()[0]))
def nonreparam_logli(self, x_var, dist_info):
"""
:param x_var:
:param dist_info:
:return: the non-reparameterizable part of the log likelihood
"""
raise NotImplementedError
def activate_dist(self, flat_dist):
"""
:param flat_dist: flattened dist info without applying nonlinearity yet
:return: a dictionary of dist infos
"""
raise NotImplementedError
@property
def dist_info_keys(self):
"""
:rtype: list[str]
"""
raise NotImplementedError
def entropy(self, dist_info):
"""
:return: entropy for each minibatch entry
"""
raise NotImplementedError
def marginal_entropy(self, dist_info):
"""
:return: the entropy of the mixture distribution averaged over all minibatch entries. Will return in the same
shape as calling `:code:Distribution.entropy`
"""
raise NotImplementedError
def marginal_logli(self, x_var, dist_info):
"""
:return: the log likelihood of the given variable under the mixture distribution averaged over all minibatch
entries.
"""
raise NotImplementedError
def sample(self, dist_info):
raise NotImplementedError
def sample_prior(self, batch_size):
return self.sample(self.prior_dist_info(batch_size))
def prior_dist_info(self, batch_size):
"""
:return: a dictionary containing distribution information about the standard prior distribution, the shape
of which is jointly decided by batch_size and self.dim
"""
raise NotImplementedError
class Gaussian(Distribution):
def __init__(self, dim, fix_std=False):
self._dim = dim
self._fix_std = fix_std
@property
def dim(self):
return self._dim
@property
def dist_flat_dim(self):
return self._dim * 2
@property
def effective_dim(self):
return self._dim
def logli(self, x_var, dist_info):
mean = dist_info["mean"]
stddev = dist_info["stddev"]
epsilon = (x_var - mean) / (stddev + TINY)
return tf.reduce_sum(
- 0.5 * np.log(2 * np.pi) - tf.log(stddev + TINY) - 0.5 * tf.square(epsilon),
reduction_indices=1,
)
def prior_dist_info(self, batch_size):
mean = tf.zeros([batch_size, self.dim])
stddev = tf.ones([batch_size, self.dim])
return dict(mean=mean, stddev=stddev)
def nonreparam_logli(self, x_var, dist_info):
return tf.zeros_like(x_var[:, 0])
def kl(self, p, q):
p_mean = p["mean"]
p_stddev = p["stddev"]
q_mean = q["mean"]
q_stddev = q["stddev"]
# means: (N*D)
# std: (N*D)
# formula:
# { (\mu_1 - \mu_2)^2 + \sigma_1^2 - \sigma_2^2 } / (2\sigma_2^2) + ln(\sigma_2/\sigma_1)
numerator = tf.square(p_mean - q_mean) + tf.square(p_stddev) - tf.square(q_stddev)
denominator = 2. * tf.square(q_stddev)
return tf.reduce_sum(
numerator / (denominator + TINY) + tf.log(q_stddev + TINY) - tf.log(p_stddev + TINY),
reduction_indices=1
)
def sample(self, dist_info):
mean = dist_info["mean"]
stddev = dist_info["stddev"]
epsilon = tf.random_normal(tf.shape(mean))
return mean + epsilon * stddev
@property
def dist_info_keys(self):
return ["mean", "stddev"]
def activate_dist(self, flat_dist):
mean = flat_dist[:, :self.dim]
if self._fix_std:
stddev = tf.ones_like(mean)
else:
stddev = tf.sqrt(tf.exp(flat_dist[:, self.dim:]))
return dict(mean=mean, stddev=stddev)