Skip to content

Commit 818e4cd

Browse files
authored
Merge pull request #1548 from Spaak/master
some small changes to make advi respect theano.config.floatX
2 parents df93e9d + 39c3281 commit 818e4cd

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

pymc3/blocking.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,14 @@ class DictToArrayBijection(object):
3838
def __init__(self, ordering, dpoint):
3939
self.ordering = ordering
4040
self.dpt = dpoint
41+
42+
# determine smallest float dtype that will fit all data
43+
if all([x.dtyp == 'float16' for x in ordering.vmap]):
44+
self.array_dtype = 'float16'
45+
elif all([x.dtyp == 'float32' for x in ordering.vmap]):
46+
self.array_dtype = 'float32'
47+
else:
48+
self.array_dtype = 'float64'
4149

4250
def map(self, dpt):
4351
"""
@@ -47,7 +55,7 @@ def map(self, dpt):
4755
----------
4856
dpt : dict
4957
"""
50-
apt = np.empty(self.ordering.dimensions)
58+
apt = np.empty(self.ordering.dimensions, dtype=self.array_dtype)
5159
for var, slc, _, _ in self.ordering.vmap:
5260
apt[slc] = dpt[var].ravel()
5361
return apt

pymc3/variational/advi.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ def _calc_elbo(vars, model, n_mcsamples, random_seed):
180180

181181
[logp], inarray = pm.join_nonshared_inputs([logpt], vars, shared)
182182

183-
uw = tt.dvector('uw')
183+
uw = tt.vector('uw')
184184
uw.tag.test_value = np.concatenate([inarray.tag.test_value,
185185
inarray.tag.test_value])
186186

@@ -192,9 +192,10 @@ def _calc_elbo(vars, model, n_mcsamples, random_seed):
192192
def _elbo_t(logp, uw, inarray, n_mcsamples, random_seed):
193193
"""Create Theano tensor of approximate ELBO by Monte Carlo sampling.
194194
"""
195-
l = (uw.size / 2).astype('int64')
196-
u = uw[:l]
197-
w = uw[l:]
195+
l = (uw.size / 2)
196+
l_int = l.astype('int64')
197+
u = uw[:l_int]
198+
w = uw[l_int:]
198199

199200
# Callable tensor
200201
def logp_(input):
@@ -209,14 +210,14 @@ def logp_(input):
209210
if n_mcsamples == 1:
210211
n = r.normal(size=inarray.tag.test_value.shape)
211212
q = n * tt.exp(w) + u
212-
elbo = logp_(q) + tt.sum(w) + 0.5 * l * (1 + np.log(2.0 * np.pi))
213+
elbo = logp_(q) + tt.sum(w) + 0.5 * l * (1 + tt.log(2.0 * np.pi))
213214
else:
214215
n = r.normal(size=(n_mcsamples, u.tag.test_value.shape[0]))
215216
qs = n * tt.exp(w) + u
216217
logps, _ = theano.scan(fn=lambda q: logp_(q),
217218
outputs_info=None,
218219
sequences=[qs])
219-
elbo = tt.mean(logps) + tt.sum(w) + 0.5 * l * (1 + np.log(2.0 * np.pi))
220+
elbo = tt.mean(logps) + tt.sum(w) + 0.5 * l * (1 + tt.log(2.0 * np.pi))
220221

221222
return elbo
222223

@@ -250,14 +251,15 @@ def optimizer(loss, param):
250251
param = list(param)
251252

252253
for param_ in param:
253-
i = theano.shared(np.array(0))
254+
i = theano.shared(np.array(0, dtype=theano.config.floatX))
255+
i_int = i.astype('int64')
254256
value = param_.get_value(borrow=True)
255257
accu = theano.shared(
256258
np.zeros(value.shape + (n_win,), dtype=value.dtype))
257259
grad = tt.grad(loss, param_)
258260

259261
# Append squared gradient vector to accu_new
260-
accu_new = tt.set_subtensor(accu[:, i], grad ** 2)
262+
accu_new = tt.set_subtensor(accu[:, i_int], grad ** 2)
261263
i_new = tt.switch((i + 1) < n_win, i + 1, 0)
262264

263265
updates[accu] = accu_new

0 commit comments

Comments
 (0)