Hi @junpenglao @aseyboldt, (feel free to split this off to a new discussion, as needed)
I was trying to use Ordered() on slightly modified version of example here: https://docs.pymc.io/notebooks/marginalized_gaussian_mixture_model.html (really all I did was play around with how many components, the true weights, means, and precisions, and added a lines dict for having the true values in the trace plot)
I tried to use this transform in three different ways, also noting that it now seems to be available directly:
with pm.Model() as model:
w = pm.Dirichlet('w', np.ones(N))
# mu = pm.Normal('mu', 0, 10, shape=N, transform=pm.distributions.transforms.Ordered())
# tau = pm.Gamma('tau', 1, 1, shape=N, transform=pm.distributions.transforms.Ordered())
# mu = pm.Normal('mu', 0, 10, shape=N, transform=Ordered())
# tau = pm.Gamma('tau', 1, 1, shape=N, transform=Ordered())
trafo = Composed(pm.distributions.transforms.LogOdds(), Ordered())
mu = pm.Normal('mu', 0, 10, shape=N, transform=trafo)
tau = pm.Gamma('tau', 1, 1, shape=N, transform=trafo)
x = pm.NormalMixture('x', w, mu, tau=tau, observed=xi)
trace = pm.sample(init='adapt_diag',tune=3000,random_seed=123)
pm.traceplot(trace,lines=lines_dict)
(note: N was just how many components I used in the simulated mixture data)
However, on all three attempts it gives the same error, always at the first pm.Normal line:
~/venv_py36/lib/python3.6/site-packages/theano/tensor/var.py in __getitem__(self, args)
508 # Check if the number of dimensions isn't too large.
509 if self.ndim < index_dim_count:
--> 510 raise IndexError('too many indices for array')
511
512 # Convert an Ellipsis if provided into an appropriate number of
IndexError: too many indices for array
For the bottom two (where I defined the class Ordered() and Composed(), like in the old discussion), the offending line was:
<ipython-input-3-3c5cc1901fbd> in forward(self, x)
4 def forward(self, x):
5 out = tt.zeros(x.shape)
----> 6 out = tt.inc_subtensor(out[0], x[0])
7 out = tt.inc_subtensor(out[1:], tt.log(x[1:] - x[:-1]))
8 return out
For the built-in Ordered(), the offending line was:
~/venv_py36/lib/python3.6/site-packages/pymc3/distributions/transforms.py in forward(self, x)
254 def forward(self, x):
255 y = tt.zeros(x.shape)
--> 256 y = tt.inc_subtensor(y[..., 0], x[..., 0])
257 y = tt.inc_subtensor(y[..., 1:], tt.log(x[..., 1:] - x[..., :-1]))
258 return y
Do you have any suggestions?
The code works fine (depending how well separated the data is) without the ordering, just that I noticed often the respective colored lines don’t line up with the color of the trace data, so thought the ordering might be an issue.
Thank you!