Is it a shape error by BART or my lack of knowledge?

Hello, and I’m happy with the update of BART.

I currently have used the new version of BART, but I got an error.

Here is the code.

Running on arviz v0.14.0
Running on PyMC v5.0.1
Running on Numpy v1.21.6
Running on pymc_bart v0.3.0
Running on Numba v0.56.4

with pm.Model() as model_oriki:
    model_params = {
        "MCMC": {
            "random_seed": RANDOM_SEED,
            "draws": 20,
            "tune": 20,
            "target_accept": 0.99,
            # "chains": 1,
            "idata_kwargs": dict(log_likelihood=True)
        }
    }
    
    # data
    data_X = pm.MutableData("data_X", df_X[use_cols].values)
    
    σ = pm.HalfCauchy("σ", beta=0.1) #beta=0.1
    μ = pmb.BART("μ", data_X, df_y[target_cols[0]].values, m=30, alpha=0.75) 

    Ny = pm.Normal("Ny", mu=μ, sigma=σ, observed=df_y[target_cols[0]].values)

    # trace = pm.sampling_jax.sample_numpyro_nuts(**model_params["MCMC"])
    trace = pm.sample(**model_params["MCMC"])

And here is the error.

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.9/site-packages/pytensor/link/vm.py in __call__(self)
    413                 ):
--> 414                     thunk()
    415                     for old_s in old_storage:

21 frames
ValueError: non-broadcastable output operand with shape (652,1) doesn't match the broadcast shape (652,652)

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.9/site-packages/pymc_bart/utils.py in _sample_posterior(all_trees, X, rng, size, excluded)
     48     for ind, p in enumerate(pred):
     49         for tree in stacked_trees[idx[ind]]:
---> 50             p += np.array([tree.predict(x, excluded) for x in X])
     51     pred.reshape((*size, shape, -1))
     52     return pred

ValueError: non-broadcastable output operand with shape (652,1) doesn't match the broadcast shape (652,652)
Apply node that caused the error: BART_rv{1, (2, 1, 0, 0, 1), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x7FCA69DFDAC0>), TensorConstant{[]}, TensorConstant{11}, data_X, TensorConstant{[0.3255892...32962963]}, TensorConstant{30}, TensorConstant{0.75}, TensorConstant{[]})
Toposort index: 1
Inputs types: [RandomGeneratorType, TensorType(int64, (0,)), TensorType(int64, ()), TensorType(float64, (?, ?)), TensorType(float64, (652,)), TensorType(int8, ()), TensorType(float32, ()), TensorType(float64, (0,))]
Inputs shapes: ['No shapes', (0,), (), (652, 19), (652,), (), (), (0,)]
Inputs strides: ['No strides', (8,), (), (8, 5216), (8,), (), (), (8,)]
Inputs values: [Generator(PCG64) at 0x7FCA69DFDAC0, array([], dtype=int64), array(11), 'not shown', 'not shown', array(30, dtype=int8), array(0.75, dtype=float32), array([], dtype=float64)]
Outputs clients: [['output'], [Elemwise{second,no_inplace}(μ, TensorConstant{(1,) of 0...5409031397})]]

HINT: Re-running with most PyTensor optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the PyTensor flag 'optimizer=fast_compile'. If that does not work, PyTensor optimizations can be disabled with 'optimizer=None'.
HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.

Shape of my data is certainly (652, 19).

I have never met error like this before.
But it works fine and no error when I ran a sampling with “sampling_jax.“
(But maybe Bart doesn’t support sampling_jax yet.)

I guess I need to specify shape of these variables, but I have no idea for it.
And when I change the code of μ to not using bart such as pm.Normal, it works fine.

So, how could I solve the error?

Thanks and sorry for my poor English.

CC @aloctavodia

Do you get the same error if you increase the number of tuning steps and draws?

1 Like

I’m sorry. It works when I increase the number of them to 1000 :melting_face:

But another error occurs when I do sample_posterior_predictive with new data.
Could you give me some advice?

Here is the code.

with model_oriki:
  pm.set_data({"data_X": test_X[use_cols].values})
  ppc2 = pm.sample_posterior_predictive(trace, var_names=["Ny"], predictions=True)

And the error message.

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.9/site-packages/pytensor/compile/function/types.py in __call__(self, *args, **kwargs)
    971             outputs = (
--> 972                 self.vm()
    973                 if output_subset is None

11 frames
_generator.pyx in numpy.random._generator.Generator.normal()

_common.pyx in numpy.random._common.cont()

_common.pyx in numpy.random._common.cont_broadcast_2()

__init__.pxd in numpy.PyArray_MultiIterNew3()

ValueError: shape mismatch: objects cannot be broadcast to a single shape

During handling of the above exception, another exception occurred:

ValueError                                Traceback (most recent call last)
/usr/local/lib/python3.9/site-packages/pytensor/tensor/random/op.py in rng_fn(self, rng, *args, **kwargs)
    162     def rng_fn(self, rng, *args, **kwargs) -> Union[int, float, np.ndarray]:
    163         """Sample a numeric random variate."""
--> 164         return getattr(rng, self.name)(*args, **kwargs)
    165 
    166     def __str__(self):

_generator.pyx in numpy.random._generator.Generator.normal()

_common.pyx in numpy.random._common.cont()

_common.pyx in numpy.random._common.cont_broadcast_2()

__init__.pxd in numpy.PyArray_MultiIterNew3()

ValueError: shape mismatch: objects cannot be broadcast to a single shape
Apply node that caused the error: normal_rv{0, (0, 0), floatX, True}(RandomGeneratorSharedVariable(<Generator(PCG64) at 0x7FC5D49D8740>), TensorConstant{(1,) of 652}, TensorConstant{11}, μ, σ)
Toposort index: 1
Inputs types: [RandomGeneratorType, TensorType(int64, (1,)), TensorType(int64, ()), TensorType(float64, (?,)), TensorType(float64, ())]
Inputs shapes: ['No shapes', (1,), (), (104,), ()]
Inputs strides: ['No strides', (8,), (), (8,), ()]
Inputs values: [Generator(PCG64) at 0x7FC5D49D8740, array([652]), array(11), 'not shown', array(0.0015974)]
Outputs clients: [['output'], ['output']]

HINT: Re-running with most PyTensor optimizations disabled could provide a back-trace showing when this node was created. This can be done by setting the PyTensor flag 'optimizer=fast_compile'. If that does not work, PyTensor optimizations can be disabled with 'optimizer=None'.
HINT: Use the PyTensor flag `exception_verbosity=high` for a debug print-out and storage map footprint of this Apply node.

Thanks.

Try setting the shape of Ny like this shape=μ.shape

1 Like

It works!

I suspected that there were some mistakes in the shape setting, but I’ve been not possible to identify the correct shape setting because of the first error.
Thank you for your advice.

I feel like I don’t know enough about the shape settings.
I know that the default setting of the shape is set as the shape of a observed and I think the shape of Ny should be equal to the shape of a observed, so I don’t understand in the case of this code why the Ny shapes need to be set the shape as equal to the shape of the variable of BART.

Why do I need to set the shape of the Ny equal to the shape of the variable of the BART?

Thanks.

This is not just restricted to BART, is the general approach when using set_data. See for example
pymc.set_data — PyMC 5.0.2 documentation

1 Like