-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdistributions.py
237 lines (170 loc) · 7.74 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
import torch as tc
from utils import softplus, inverse_softplus
# Choose scaling function to map positive-definite quantities onto the real line and visa-versa
# NOTE: Seems not to matter too much
scaling_function = 'softplus'
#scaling_function = 'exponential'
# Scaling functions
if scaling_function == 'softplus':
positive_function = softplus # Scales the entire real line to a positive-definite number
positive_inverse = inverse_softplus # Scales a positive-definite number to the entire real line
elif scaling_function == 'exponential':
positive_function = tc.exp
positive_inverse = tc.log
else:
raise ValueError('Scaling function not recognised')
class Normal(tc.distributions.Normal):
def __init__(self, loc, scale): # Define an optimizable scale that exists on the entire real line
self.optim_scale = positive_inverse(scale)
super().__init__(loc, scale)
def params(self): # Return a list of standard parameters of the distribution
return [self.loc, self.scale]
def optim_params(self): # Return a list of (optimizeable) parameters of the distribution
return [self.loc.requires_grad_(), self.optim_scale.requires_grad_()]
def log_prob(self, x): # This overwrites the default log_prob function and updates scale
self.scale = positive_function(self.optim_scale) # Needed to carry the gradient through
return super().log_prob(x)
class Gamma(tc.distributions.Gamma):
def __init__(self, concentration, rate):
self.optim_concentration = positive_inverse(concentration)
self.optim_rate = positive_inverse(rate)
super().__init__(concentration, rate)
def params(self):
return [self.concentration, self.rate]
def optim_params(self):
return [self.optim_concentration.requires_grad_(), self.optim_rate.requires_grad_()]
def log_prob(self, x):
self.concentration, self.rate = positive_function(self.optim_concentration), positive_function(self.optim_rate)
return super().log_prob(x)
class Exponential(tc.distributions.Exponential):
def __init__(self, rate):
self.optim_rate = positive_inverse(rate)
super().__init__(rate)
def params(self):
return [self.rate]
def optim_params(self):
return [self.optim_rate.requires_grad_()]
def log_prob(self, x):
self.rate = positive_function(self.optim_rate)
return super().log_prob(x)
class Beta(tc.distributions.Beta):
def __init__(self, concentration1, concentration0):
self.optim_concentration1 = positive_inverse(concentration1)
self.optim_concentration0 = positive_inverse(concentration0)
super().__init__(concentration1, concentration0)
def params(self):
return [self.concentration1, self.concentration0]
def optim_params(self):
return [self.optim_concentration1.requires_grad_(), self.optim_concentration0.requires_grad_()]
def log_prob(self, x):
# TODO: Error here, I think because concentration1, concentration0 are decorated properties
self.concentration1 = positive_function(self.optim_concentration1)
self.concentration0 = positive_function(self.optim_concentration0)
return super().log_prob(x)
class Dirichlet(tc.distributions.Dirichlet):
def __init__(self, concentration):
self.optim_concentration = positive_inverse(concentration)
super().__init__(concentration)
def params(self):
return [self.concentration]
def optim_params(self):
return [self.optim_concentration.requires_grad_()]
def log_prob(self, x):
self.concentration = positive_function(self.optim_concentration)
return super().log_prob(x)
class Bernoulli(tc.distributions.Bernoulli):
def __init__(self, probs=None, logits=None):
if logits is None and probs is None:
raise ValueError('Set either probs or logits')
elif logits is None:
if type(probs) is float:
probs = tc.tensor(probs)
logits = tc.log(probs/(1.-probs)) # NOTE: This will fail if probs = 0
super().__init__(logits=logits)
def params(self):
return [self.logits]
def optim_params(self):
return [self.logits.requires_grad_()]
class Categorical(tc.distributions.Categorical):
def __init__(self, probs=None, logits=None):
if (probs is None) and (logits is None):
raise ValueError('Either `probs` or `logits` must be specified, but not both')
if probs is not None:
if probs.dim() < 1:
raise ValueError('`probs` parameter must be at least one-dimensional')
probs = probs/probs.sum(-1, keepdim=True)
logits = tc.distributions.utils.probs_to_logits(probs)
else:
if logits.dim() < 1:
raise ValueError('`logits` parameter must be at least one-dimensional')
logits = logits-logits.logsumexp(dim=-1, keepdim=True) # Normalize
super().__init__(logits=logits)
self.logits = logits
self._param = self.logits
def params(self):
return [self.logits]
def optim_params(self):
return [self.logits.requires_grad_()]
if __name__ == '__main__':
# Here is a stand-alone example program that uses BBVI to learn the parameters
# of a Gaussian posterior distribution (Q1 of the homework)
### Parameters ###
# Seed
seed = 123
# Variational inference
num_samples_per_step = 100
num_steps = 300
learning_rate = 1e-1
# Prior parameters
prior_loc = tc.tensor(1.)
prior_scale = tc.sqrt(tc.tensor(5.))
# Likelihood parameters
likelihood_scale = tc.sqrt(tc.tensor(2.))
# Observations
ys = tc.tensor([8., 9.])
### ###
### Calculations ###
# Random seed
tc.manual_seed(seed)
# Stepping
print('Number of steps in calculation:', num_steps)
# Prior distribtuion
prior = tc.distributions.Normal(prior_loc, prior_scale)
print('Prior:', prior)
# Q distribution
Q_loc, Q_scale = tc.clone(prior_loc), tc.clone(prior_scale) # NOTE: Clone is necessary here
Q = Normal(Q_loc, Q_scale)
Q.loc, Q_optim_scale = Q.optim_params() # Get the optimisable parameters for the distribution
print('Q distribution:', Q)
print('Q location, scale, optim scale:', Q.loc, Q.scale, Q.optim_scale)
print()
### ###
### Variational inference ###
# Loop over steps
locs = [Q_loc.clone().detach()]; scales = [Q_scale.clone().detach()]
optimizer = tc.optim.Adam([Q_loc, Q_optim_scale], lr=learning_rate)
print('Initial Q:', Q)
for _ in range(num_steps):
# Draw samples from the Q distribution
Q_samples = Q.sample(sample_shape=(num_samples_per_step,))
# Calculate probability of the Q samples under the Q(X) distribution
log_Q = Q.log_prob(Q_samples)
# Calculate the probability of the Q_samples and observations under the joint P(X, Y) distribution
log_likelihood = 0.
for y in ys:
log_likelihood += tc.distributions.Normal(loc=Q_samples, scale=likelihood_scale).log_prob(y)
log_prior = prior.log_prob(Q_samples)
log_P = log_likelihood+log_prior
# Calculate the log importance weights of the Q samples and obervations
log_W = log_P-log_Q
# Calculate the thing related to the ELBO
# NOTE: Must detach part of the calculation here!
ELBO_loss = -(log_Q*(log_W.detach())).mean()
ELBO_loss.backward()
# Step with the optimizer
optimizer.step()
print('Q:', Q)
optimizer.zero_grad() # NOTE: Must zero the gradient after each step!
# Append results to a list
locs.append(Q_loc.clone().detach()); scales.append(Q_scale.clone().detach())
### ###