forked from Philip-Bachman/ICML-2015
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OneStageModel.py
439 lines (399 loc) · 18 KB
/
OneStageModel.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
################################################################
# Code for managing and training a generator/inferencer pair. #
################################################################
# basic python
import numpy as np
import numpy.random as npr
from collections import OrderedDict
# theano business
import theano
import theano.tensor as T
#from theano.tensor.shared_randomstreams import RandomStreams as RandStream
from theano.sandbox.cuda.rng_curand import CURAND_RandomStreams as RandStream
# phil's sweetness
from NetLayers import HiddenLayer, DiscLayer, relu_actfun, softplus_actfun, \
apply_mask
from InfNet import InfNet
from PeaNet import PeaNet
from HelperFuncs import get_param_updates
from LogPDFs import log_prob_bernoulli, log_prob_gaussian2, gaussian_kld
#
#
# Important symbolic variables:
# Xd: Xd represents input at the "data variables" of the inferencer
# Xc: Xc represents input at the "control variables" of the inferencer
# Xm: Xm represents input at the "mask variables" of the inferencer
#
#
def batch_normalize(Y):
"""
Set columns of Y to zero mean and unit variance.
"""
Y_zmuv = (Y - T.mean(Y, axis=0, keepdims=True)) / \
T.std(Y, axis=0, keepdims=True)
return Y_zmuv
class OneStageModel(object):
"""
Controller for training a basic one step VAE.
Parameters:
rng: numpy.random.RandomState (for reproducibility)
Xd: symbolic "data" input to this 2S-VAE
Xc: symbolic "control" input to this 2S-VAE
Xm: symbolic "mask" input to this 2S-VAE
p_x_given_z: InfNet for x given z
q_z_given_x: InfNet for z given x
x_dim: dimension of the "instances" variables
z_dim: dimension of the "latent prototypes" variables
params:
x_type: can be "bernoulli" or "gaussian"
xt_transform: optional transform for gaussian means
logvar_bound: optional bound on gaussian output logvar
"""
def __init__(self, rng=None, \
Xd=None, Xc=None, Xm=None, \
p_x_given_z=None, q_z_given_x=None, \
x_dim=None, z_dim=None, \
params=None):
# setup a rng for this GIPair
self.rng = RandStream(rng.randint(100000))
# grab the user-provided parameters
if params is None:
self.params = {}
else:
self.params = params
if 'xt_transform' in self.params:
assert((self.params['xt_transform'] == 'sigmoid') or \
(self.params['xt_transform'] == 'none'))
if self.params['xt_transform'] == 'sigmoid':
self.xt_transform = lambda x: T.nnet.sigmoid(x)
else:
self.xt_transform = lambda x: x
else:
self.xt_transform = lambda x: T.nnet.sigmoid(x)
if 'logvar_bound' in self.params:
self.logvar_bound = self.params['logvar_bound']
else:
self.logvar_bound = 10
#
# x_type: this tells if we're using bernoulli or gaussian model for
# the observations
#
self.x_type = self.params['x_type']
assert((self.x_type == 'bernoulli') or (self.x_type == 'gaussian'))
# record the dimensions of various spaces relevant to this model
self.z_dim = z_dim
self.x_dim = x_dim
# set parameters for the isotropic Gaussian prior over z
self.prior_mean = 0.0
self.prior_logvar = 0.0
# record the symbolic variables that will provide inputs to the
# computation graph created to describe this OneStageModel
self.Xd = Xd
self.Xc = Xc
self.Xm = Xm
self.batch_reps = T.lscalar()
self.x = apply_mask(self.Xd, self.Xc, self.Xm)
#####################################################################
# Setup the computation graph that provides values in our objective #
#####################################################################
# inferencer model for latent prototypes given instances
self.q_z_given_x = q_z_given_x.shared_param_clone(rng=rng, Xd=self.x)
self.z = self.q_z_given_x.output
self.z_mean = self.q_z_given_x.output_mean
self.z_logvar = self.q_z_given_x.output_logvar
# generator model for prototypes given latent prototypes
self.p_x_given_z = p_x_given_z.shared_param_clone(rng=rng, Xd=self.z)
self.xt = self.p_x_given_z.output_mean # use deterministic output
# construct the final output of generator, conditioned on z
if self.x_type == 'bernoulli':
self.xg = T.nnet.sigmoid(self.xt)
else:
self.xg = self.xt_transform(self.xt)
# self.output_logvar modifies the output distribution
self.output_logvar = self.p_x_given_z.sigma_layers[-1].b
self.bounded_logvar = self.logvar_bound * \
T.tanh(self.output_logvar[0] / self.logvar_bound)
######################################################################
# ALL SYMBOLIC VARS NEEDED FOR THE OBJECTIVE SHOULD NOW BE AVAILABLE #
######################################################################
# shared var learning rate for generator and inferencer
zero_ary = np.zeros((1,)).astype(theano.config.floatX)
self.lr_1 = theano.shared(value=zero_ary, name='osm_lr_1')
# shared var momentum parameters for generator and inferencer
self.mom_1 = theano.shared(value=zero_ary, name='osm_mom_1')
self.mom_2 = theano.shared(value=zero_ary, name='osm_mom_2')
self.it_count = theano.shared(value=zero_ary, name='osm_it_count')
# init parameters for controlling learning dynamics
self.set_sgd_params()
# init shared var for weighting nll of data given posterior sample
self.lam_nll = theano.shared(value=zero_ary, name='osm_lam_nll')
self.set_lam_nll(lam_nll=1.0)
# init shared var for weighting prior kld against reconstruction
self.lam_kld_1 = theano.shared(value=zero_ary, name='osm_lam_kld_1')
self.lam_kld_2 = theano.shared(value=zero_ary, name='osm_lam_kld_2')
self.set_lam_kld(lam_kld_1=1.0, lam_kld_2=0.0)
# init shared var for controlling l2 regularization on params
self.lam_l2w = theano.shared(value=zero_ary, name='osm_lam_l2w')
self.set_lam_l2w(1e-4)
# Grab all of the "optimizable" parameters in "group 1"
self.group_1_params = []
self.group_1_params.extend(self.q_z_given_x.mlp_params)
self.group_1_params.extend(self.p_x_given_z.mlp_params)
# Make a joint list of parameters
self.joint_params = self.group_1_params
###################################
# CONSTRUCT THE COSTS TO OPTIMIZE #
###################################
self.nll_costs = self.lam_nll[0] * self._construct_nll_costs()
self.nll_cost = T.mean(self.nll_costs)
self.kld_costs_1, self.kld_costs_2 = self._construct_kld_costs()
self.kld_costs = (self.lam_kld_1[0] * self.kld_costs_1) + \
(self.lam_kld_2[0] * self.kld_costs_2)
self.kld_cost = T.mean(self.kld_costs)
act_reg_cost, param_reg_cost = self._construct_reg_costs()
self.reg_cost = self.lam_l2w[0] * param_reg_cost
self.joint_cost = self.nll_cost + self.kld_cost + self.reg_cost
# Get the gradient of the joint cost for all optimizable parameters
print("Computing OneStageModel cost gradients...")
self.joint_grads = OrderedDict()
grad_list = T.grad(self.joint_cost, self.joint_params)
for i, p in enumerate(self.joint_params):
self.joint_grads[p] = grad_list[i]
# Construct the updates for the generator and inferencer networks
self.joint_updates = get_param_updates(params=self.joint_params, \
grads=self.joint_grads, alpha=self.lr_1, \
beta1=self.mom_1, beta2=self.mom_2, it_count=self.it_count, \
mom2_init=1e-3, smoothing=1e-8, max_grad_norm=10.0)
# Construct a function for jointly training the generator/inferencer
print("Compiling OneStageModel theano functions...")
self.train_joint = self._construct_train_joint()
self.compute_fe_terms = self._construct_compute_fe_terms()
self.compute_post_klds = self._construct_compute_post_klds()
self.sample_from_prior = self._construct_sample_from_prior()
self.transform_x_to_z = theano.function([self.q_z_given_x.Xd], \
outputs=self.q_z_given_x.output_mean)
self.transform_z_to_x = theano.function([self.p_x_given_z.Xd], \
outputs=self.xt_transform(self.p_x_given_z.output_mean))
self.inf_weights = self.q_z_given_x.shared_layers[0].W
self.gen_weights = self.p_x_given_z.mu_layers[-1].W
return
def set_sgd_params(self, lr_1=0.01, mom_1=0.9, mom_2=0.999):
"""
Set learning rate and momentum parameter for all updates.
"""
zero_ary = np.zeros((1,))
# set learning rates
new_lr_1 = zero_ary + lr_1
self.lr_1.set_value(new_lr_1.astype(theano.config.floatX))
# set momentums
new_mom_1 = zero_ary + mom_1
self.mom_1.set_value(new_mom_1.astype(theano.config.floatX))
new_mom_2 = zero_ary + mom_2
self.mom_2.set_value(new_mom_2.astype(theano.config.floatX))
return
def set_lam_nll(self, lam_nll=1.0):
"""
Set weight for controlling the influence of the data likelihood.
"""
zero_ary = np.zeros((1,))
new_lam = zero_ary + lam_nll
self.lam_nll.set_value(new_lam.astype(theano.config.floatX))
return
def set_lam_kld(self, lam_kld_1=1.0, lam_kld_2=0.0):
"""
Set the relative weight of prior KL-divergence vs. data likelihood.
"""
zero_ary = np.zeros((1,))
new_lam = zero_ary + lam_kld_1
self.lam_kld_1.set_value(new_lam.astype(theano.config.floatX))
new_lam = zero_ary + lam_kld_2
self.lam_kld_2.set_value(new_lam.astype(theano.config.floatX))
return
def set_lam_l2w(self, lam_l2w=1e-3):
"""
Set the relative strength of l2 regularization on network params.
"""
zero_ary = np.zeros((1,))
new_lam = zero_ary + lam_l2w
self.lam_l2w.set_value(new_lam.astype(theano.config.floatX))
return
def set_output_bias(self, new_bias=None):
"""
Set the output layer bias.
"""
new_bias = new_bias.astype(theano.config.floatX)
self.p_x_given_z.mu_layers[-1].b.set_value(new_bias)
return
def set_input_bias(self, new_bias=None):
"""
Set the input layer bias.
"""
new_bias = new_bias.astype(theano.config.floatX)
self.q_z_given_x.shared_layers[0].b_in.set_value(new_bias)
return
def _construct_nll_costs(self):
"""
Construct the negative log-likelihood part of cost to minimize.
"""
if self.x_type == 'bernoulli':
ll_cost = log_prob_bernoulli(self.x, self.xg)
else:
ll_cost = log_prob_gaussian2(self.x, self.xg, \
log_vars=self.bounded_logvar)
nll_cost = -ll_cost
return nll_cost
def _construct_kld_costs(self):
"""
Construct the posterior KL-d from prior part of cost to minimize.
"""
# compute the KLds between posteriors and priors. we compute the KLd
# independently for each input and each latent variable dimension
kld_z = gaussian_kld(self.q_z_given_x.output_mean, \
self.q_z_given_x.output_logvar, \
self.prior_mean, self.prior_logvar)
# compute the batch-wise L1 and L2 penalties on per-dim KLds
kld_l1_costs = T.sum(kld_z, axis=1, keepdims=True)
derp1 = T.mean(kld_l1_costs)
derp2 = kld_l1_costs > derp1
batch_kld_mean = theano.gradient.disconnected_grad(derp1)
mask = theano.gradient.disconnected_grad(derp2)
kld_l2_costs = T.sum(((kld_l1_costs-batch_kld_mean)**2.0 * mask), \
axis=1, keepdims=True)
return [kld_l1_costs, kld_l2_costs]
def _construct_reg_costs(self):
"""
Construct the cost for low-level basic regularization. E.g. for
applying l2 regularization to the network activations and parameters.
"""
obs_count = T.cast(self.Xd.shape[0], 'floatX')
act_reg_cost = (self.p_x_given_z.act_reg_cost + \
self.q_z_given_x.act_reg_cost) / obs_count
param_reg_cost = sum([T.sum(p**2.0) for p in self.joint_params])
other_reg_costs = [act_reg_cost, param_reg_cost]
return other_reg_costs
def _construct_train_joint(self):
"""
Construct theano function to train all networks jointly.
"""
# setup some symbolic variables for theano to deal with
Xd = T.matrix()
Xc = T.matrix()
Xm = T.matrix()
# collect the values to output with each function evaluation
outputs = [self.joint_cost, self.nll_cost, self.kld_cost, \
self.reg_cost, self.nll_costs, self.kld_costs]
func = theano.function(inputs=[ Xd, Xc, Xm, self.batch_reps ], \
outputs=outputs, \
givens={ self.Xd: Xd.repeat(self.batch_reps, axis=0), \
self.Xc: Xc.repeat(self.batch_reps, axis=0), \
self.Xm: Xm.repeat(self.batch_reps, axis=0) }, \
updates=self.joint_updates)
return func
def _construct_compute_post_klds(self):
"""
Construct theano function to compute the info about the variational
approximate posteriors for some inputs.
"""
# setup some symbolic variables for theano to deal with
Xd = T.matrix()
Xc = T.zeros_like(Xd)
Xm = T.zeros_like(Xd)
all_klds = gaussian_kld(self.q_z_given_x.output_mean, \
self.q_z_given_x.output_logvar, \
self.prior_mean, self.prior_logvar)
# compile theano function for a one-sample free-energy estimate
kld_func = theano.function(inputs=[Xd], outputs=all_klds, \
givens={self.Xd: Xd, self.Xc: Xc, self.Xm: Xm})
return kld_func
def _construct_compute_fe_terms(self):
"""
Construct theano function to compute the log-likelihood and posterior
KL-divergence terms for the variational free-energy.
"""
# setup some symbolic variables for theano to deal with
Xd = T.matrix()
Xc = T.zeros_like(Xd)
Xm = T.zeros_like(Xd)
# construct values to output
if self.x_type == 'bernoulli':
ll_term = log_prob_bernoulli(self.x, self.xg)
else:
ll_term = log_prob_gaussian2(self.x, self.xg, \
log_vars=self.bounded_logvar)
all_klds = gaussian_kld(self.q_z_given_x.output_mean, \
self.q_z_given_x.output_logvar, \
self.prior_mean, self.prior_logvar)
kld_term = T.sum(all_klds, axis=1)
# compile theano function for a one-sample free-energy estimate
fe_term_sample = theano.function(inputs=[Xd], \
outputs=[ll_term, kld_term], \
givens={self.Xd: Xd, self.Xc: Xc, self.Xm: Xm})
# construct a wrapper function for multi-sample free-energy estimate
def fe_term_estimator(X, sample_count):
ll_sum = np.zeros((X.shape[0],))
kld_sum = np.zeros((X.shape[0],))
for i in range(sample_count):
result = fe_term_sample(X)
ll_sum = ll_sum + result[0].ravel()
kld_sum = kld_sum + result[1].ravel()
mean_nll = -ll_sum / float(sample_count)
mean_kld = kld_sum / float(sample_count)
return [mean_nll, mean_kld]
return fe_term_estimator
def _construct_sample_from_prior(self):
"""
Construct a function for drawing independent samples from the
distribution generated by this OneStageModel.
"""
z_sym = T.matrix()
oputs = self.xg
sample_func = theano.function(inputs=[z_sym], outputs=oputs, \
givens={ self.z: z_sym })
def prior_sampler(samp_count):
z_samps = npr.randn(samp_count, self.z_dim)
z_samps = (np.exp(0.5 * self.prior_logvar) * z_samps) + \
self.prior_mean
z_samps = z_samps.astype(theano.config.floatX)
model_samps = sample_func(z_samps)
return model_samps
return prior_sampler
def sample_from_chain(self, X_d, X_c=None, X_m=None, loop_iters=5, \
sigma_scale=None):
"""
Sample for several rounds through the I<->G loop, initialized with the
the "data variable" samples in X_d.
"""
data_samples = []
prior_samples = []
if X_c is None:
X_c = 0.0 * X_d
if X_m is None:
X_m = 0.0 * X_d
if sigma_scale is None:
sigma_scale = 1.0
# set sigma_scale on our InfNet
old_scale = self.q_z_given_x.sigma_scale.get_value(borrow=False)
self.q_z_given_x.set_sigma_scale(sigma_scale)
for i in range(loop_iters):
# apply mask, mixing foreground and background data
X_d = apply_mask(Xd=X_d, Xc=X_c, Xm=X_m)
# record the data samples for this iteration
data_samples.append(1.0 * X_d)
# sample from their inferred posteriors
X_p = self.q_z_given_x.sample_posterior(X_d)
# record the sampled points (in the "prior space")
prior_samples.append(1.0 * X_p)
# get next data samples by transforming the prior-space points
X_d = self.transform_z_to_x(X_p)
# reset sigma_scale on our InfNet
self.q_z_given_x.set_sigma_scale(old_scale[0])
result = {"data samples": data_samples, "prior samples": prior_samples}
return result
if __name__=="__main__":
########
# DONE #
########
print("TESTING COMPLETE!")
##############
# EYE BUFFER #
##############