# ValueError: Not enough dimensions on Elemwise{mul,no\_inplace}.0 to reduce on axis -1

**URL:** <https://discourse.pymc.io/t/valueerror-not-enough-dimensions-on-elemwise-mul-no-inplace-0-to-reduce-on-axis-1/1902>\
**Category:** Questions\
**Created:** [September 11, 2018, 7:10am UTC](https://discourse.pymc.io/t/valueerror-not-enough-dimensions-on-elemwise-mul-no-inplace-0-to-reduce-on-axis-1/1902 "2018-09-11T07:10:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ravinderatla](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ravinderatla/32/1105_2.png) [@ravinderatla](https://discourse.pymc.io/u/ravinderatla)\
**Post date:** [September 11, 2018, 7:10am UTC](https://discourse.pymc.io/t/valueerror-not-enough-dimensions-on-elemwise-mul-no-inplace-0-to-reduce-on-axis-1/1902/1 "2018-09-11T07:10:07Z")

</div>

I have been working on PyMc3 to build a probabilistic model that fits my data. As a part of it I’ve tried implementing updating priors on my data in which posteriors are being used as priors and will run for multiple times

```python
observed_cell_delay = list_cell_delay[:5000]
def stick_breaking(beta):
  portion_remaining = tt.concatenate([[1], tt.extra_ops.cumprod(1 - beta)[:-1]])
  return beta * portion_remaining
K=30
with pm.Model() as cell_del_model:
  alpha = pm.Gamma('alpha', 1., 1.)
  beta = pm.Beta('beta', 1., alpha, shape=K)
  w = pm.Deterministic('w', stick_breaking(beta))
  tau = pm.Gamma('tau', 1., 1., shape=K) 
  mu = pm.Normal('mu',0.,10.,shape=K) 
  est_cell_del = pm.NormalMixture('est_cell_del', w, mu, tau=tau, observed=observed_cell_delay)

with cell_del_model:
  step = pm.Metropolis()
  #db = pm.backends.Text('samples')
  trace = pm.sample(2000,step=step,tune=1000,cores=8)#,discard_tuned_samples=True,n_init=1000,trace=db)
pm.traceplot(trace)

def from_posterior(param, samples):
  smin, smax = np.min(samples), np.max(samples)
  width = smax - smin
  x = np.linspace(smin, smax, 100)
  samples = samples[~np.isnan(samples)]
  y = stats.gaussian_kde(samples)(x)
  x = np.concatenate([[x[0] - 3 * width], x, [x[-1] + 3 * width]])
  y = np.concatenate([[0], y, [0]])
  return Interpolated(param, x, y)

new_cell_delay = list_cell_delay[10000:]
with pm.Model() as cell_delay_model:
  alpha = from_posterior('alpha',trace['alpha'])
  beta = from_posterior('beta',trace['beta'])
  w = from_posterior('w',trace['w'])
  tau = from_posterior('tau',trace['tau'])
  mu = from_posterior('mu',trace['mu'])
  est_cell_del = pm.NormalMixture('est_cell_del', w, mu, tau=tau, observed=new_cell_delay)
  step = pm.Metropolis()
  trace1 = pm.sample(1000,step=step,tune=1000,cores=8)

```

I am getting the value error `"ValueError: Not enough dimensions on Elemwise{mul,no_inplace}.0 to reduce on axis -1"` at pm.NormalMixture while updating priors. Thanks in advance.

---

<div class="post-metadata">

**Author:** ![colcarroll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/colcarroll/32/9_2.png) [@colcarroll](https://discourse.pymc.io/u/colcarroll)\
**Post date:** [September 11, 2018, 8:26pm UTC](https://discourse.pymc.io/t/valueerror-not-enough-dimensions-on-elemwise-mul-no-inplace-0-to-reduce-on-axis-1/1902/2 "2018-09-11T20:26:45Z")

</div>

The [example here](https://docs.pymc.io/notebooks/updating_priors.html) uses all univariate normal distributions, so `from_posterior` is a reasonable interpolation for the data. In your model, `beta`, `tau`, and `mu` are all `K` dimensional, and I am not sure how well `Interpolated` accepts a `K` dimensional input.

The specific problem is probably because `from_posterior` is calculating a 1d interpolation for a `K` dimensional density. I will give some thought, but will be interested if you figure out a `from_posterior` for high dimensional objects!

---

<div class="post-metadata">

**Author:** ![junpenglao](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/junpenglao/32/8_2.png) [@junpenglao](https://discourse.pymc.io/u/junpenglao)\
**Post date:** [September 11, 2018, 8:32pm UTC](https://discourse.pymc.io/t/valueerror-not-enough-dimensions-on-elemwise-mul-no-inplace-0-to-reduce-on-axis-1/1902/3 "2018-09-11T20:32:04Z")

</div>

> [@colcarroll](#):
>
> and I am not sure how well `Interpolated` accepts a `K` dimensional input

It cannot work with n dimension input - it’s for 1d only.

---

<div class="post-metadata">

**Author:** ![ravinderatla](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.pymc.io/ravinderatla/32/1105_2.png) [@ravinderatla](https://discourse.pymc.io/u/ravinderatla)\
**Post date:** [September 12, 2018, 10:09am UTC](https://discourse.pymc.io/t/valueerror-not-enough-dimensions-on-elemwise-mul-no-inplace-0-to-reduce-on-axis-1/1902/4 "2018-09-12T10:09:42Z")

</div>

Thanks for answering @colcarroll. I’d want to know if there is a way to build a probability distribution for multidimensional data (replacement for Interpolated).
