In a toy example, I’m able to use the tile function to produce a working example:
import scipy
import theano
import pymc3 as pm
X = np.array([[1, 0], [1, 0], [0, 1], [1, 0], [1, 0], [0, 1], [0, 1], [0, 1], [0, 1], [1, 0], [0, 1], [0, 1], [1, 0]])
X = scipy.sparse.csr_matrix(X)
Y = np.array([[5.28708186], [4.96509597], [0.96509597], [ 5.3526662], [6.19951688], [-0.96509597], [0.49597], [-0.56509597], [0.00053345], [4.19951688], [-10], [-10], [-10]])
X = theano.sparse.as_sparse_variable(X)
Y = theano.tensor.as_tensor_variable(Y)
with pm.Model() as model:
w = pm.Dirichlet('w', a=np.array([1, 1]))
sd = pm.HalfNormal('sd', sd=1.0, shape=(1, 1))
mu = pm.Normal('mu', mu=0, sd=sd, shape=(2, 1))
mu = theano.sparse.dot(X, mu)
kappa = pm.HalfNormal('kappa', sd=1.0, shape=(1, 1))
mu2 = theano.tensor.as_tensor_variable(-10)
mu2 = theano.tensor.shape_padleft(mu2, 2)
mu2 = theano.tensor.tile(mu2, reps=[13, 1])
sd2 = theano.tensor.as_tensor_variable(1e-5)
sd2 = theano.tensor.shape_padleft(sd2, 2)
pm.NormalMixture('likelihood', w=w, mu=theano.tensor.stack([mu, mu2]),
sd=theano.tensor.stack([kappa, sd2]), observed=Y)
However, when I use this methodology in my current model factory function, I get the following traceback.
---------------------------------------------------------------------------
IndexError Traceback (most recent call last)
<ipython-input-149-56c6fce440e4> in <module>
2 preprocessors['design_matrix'].fit(train_data)
3
----> 4 model = construct_model(train_data, preprocessors, fit_intercept=False)
5
6 with model:
<ipython-input-148-0dcee7f04157> in construct_model(data, preprocessors, fit_intercept)
47
48 pm.NormalMixture('likelihood', w=w,
---> 49 mu=theano.tensor.stack([mu, degenerate_mu]),
50 sd=theano.tensor.stack([kappa, degenerate_sd]),
51 observed=success_rate)
/anaconda3/envs/audience-concentration-forecast-service-env/lib/python3.6/site-packages/theano/tensor/basic.py in stack(*tensors, **kwargs)
4726 dtype = scal.upcast(*[i.dtype for i in tensors])
4727 return theano.tensor.opt.MakeVector(dtype)(*tensors)
-> 4728 return join(axis, *[shape_padaxis(t, axis) for t in tensors])
4729
4730
/anaconda3/envs/audience-concentration-forecast-service-env/lib/python3.6/site-packages/theano/tensor/basic.py in join(axis, *tensors_list)
4500 return tensors_list[0]
4501 else:
-> 4502 return join_(axis, *tensors_list)
4503
4504
/anaconda3/envs/audience-concentration-forecast-service-env/lib/python3.6/site-packages/theano/gof/op.py in __call__(self, *inputs, **kwargs)
613 """
614 return_list = kwargs.pop('return_list', False)
--> 615 node = self.make_node(*inputs, **kwargs)
616
617 if config.compute_test_value != 'off':
/anaconda3/envs/audience-concentration-forecast-service-env/lib/python3.6/site-packages/theano/tensor/basic.py in make_node(self, *axis_and_tensors)
4233
4234 return self._make_node_internal(
-> 4235 axis, tensors, as_tensor_variable_args, output_maker)
4236
4237 def _make_node_internal(self, axis, tensors,
/anaconda3/envs/audience-concentration-forecast-service-env/lib/python3.6/site-packages/theano/tensor/basic.py in _make_node_internal(self, axis, tensors, as_tensor_variable_args, output_maker)
4285 continue
4286 if bflag:
-> 4287 bcastable[current_axis] = True
4288 try:
4289 bcastable[axis] = False
IndexError: list assignment index out of range
Here is the model factor code snippet.
with pm.Model() as model:
coefficient_vector = dfn.assemble_coefficient_vector(feature_shapes, fit_intercept=fit_intercept)
mu = theano.sparse.dot(X, coefficient_vector)
kappa = pm.HalfNormal('κ', sd=1.0, shape=(1, 1))
w = pm.Dirichlet('w', a=np.array([1, 1]))
degenerate_mu = theano.tensor.as_tensor_variable(DEGENERATE_VALUE)
degenerate_mu = theano.tensor.shape_padleft(degenerate_mu, 2)
degenerate_mu = theano.tensor.tile(degenerate_mu, reps=[M, 1])
degenerate_sd = theano.tensor.as_tensor_variable(1e-5)
degenerate_sd = theano.tensor.shape_padleft(degenerate_sd, 2)
pm.NormalMixture('likelihood', w=w,
mu=theano.tensor.stack([mu, degenerate_mu]),
sd=theano.tensor.stack([kappa, degenerate_sd]),
observed=success_rate)
Does this error make sense?