@@ -180,7 +180,7 @@ def _calc_elbo(vars, model, n_mcsamples, random_seed):
180
180
181
181
[logp ], inarray = pm .join_nonshared_inputs ([logpt ], vars , shared )
182
182
183
- uw = tt .dvector ('uw' )
183
+ uw = tt .vector ('uw' )
184
184
uw .tag .test_value = np .concatenate ([inarray .tag .test_value ,
185
185
inarray .tag .test_value ])
186
186
@@ -192,9 +192,10 @@ def _calc_elbo(vars, model, n_mcsamples, random_seed):
192
192
def _elbo_t (logp , uw , inarray , n_mcsamples , random_seed ):
193
193
"""Create Theano tensor of approximate ELBO by Monte Carlo sampling.
194
194
"""
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 :]
198
199
199
200
# Callable tensor
200
201
def logp_ (input ):
@@ -209,14 +210,14 @@ def logp_(input):
209
210
if n_mcsamples == 1 :
210
211
n = r .normal (size = inarray .tag .test_value .shape )
211
212
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 ))
213
214
else :
214
215
n = r .normal (size = (n_mcsamples , u .tag .test_value .shape [0 ]))
215
216
qs = n * tt .exp (w ) + u
216
217
logps , _ = theano .scan (fn = lambda q : logp_ (q ),
217
218
outputs_info = None ,
218
219
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 ))
220
221
221
222
return elbo
222
223
@@ -250,14 +251,15 @@ def optimizer(loss, param):
250
251
param = list (param )
251
252
252
253
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' )
254
256
value = param_ .get_value (borrow = True )
255
257
accu = theano .shared (
256
258
np .zeros (value .shape + (n_win ,), dtype = value .dtype ))
257
259
grad = tt .grad (loss , param_ )
258
260
259
261
# 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 )
261
263
i_new = tt .switch ((i + 1 ) < n_win , i + 1 , 0 )
262
264
263
265
updates [accu ] = accu_new
0 commit comments