# ConfigurationError in hierarchical model

**URL:** <https://discuss.bayesflow.org/t/configurationerror-in-hierarchical-model/161>\
**Category:** General\
**Created:** [May 16, 2025, 8:49pm UTC](https://discuss.bayesflow.org/t/configurationerror-in-hierarchical-model/161 "2025-05-16T20:49:45Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jice](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jice](https://discuss.bayesflow.org/u/Jice)\
**Post date:** [May 16, 2025, 8:49pm UTC](https://discuss.bayesflow.org/t/configurationerror-in-hierarchical-model/161/1 "2025-05-16T20:49:45Z")

</div>

Hi,  
I’ve recently trying BayesFlow v1 for posterior estimation in a hierarchical model. The parameters I have are:  
8 hyperparameters: \mu\_1, \mu\_2, \mu\_3, \mu\_4, \sigma\_1, \sigma\_2, \sigma\_3, \sigma\_4  
and 4 local parameters: \theta\_1, \theta\_2, \theta\_3, \theta\_4  
with relation: \theta\_1~N(\mu\_1, \sigma\_1), also for others.  
the observations are obtained from: y=function(\theta\_1, \theta\_2, \theta\_3, \theta\_4).  
Here are my training data configuration:  
for hyperparameters: 2000 X 8  
for local parameters: 2000 X 20 X 4, which means I have 20 groups, each group has 4 local parameters  
for observation: 2000 X 20 X 12  
the data is 3D, thus I do not have to use a summary network for the first level, but still need a summary network for the second level to aggregate information from 20 groups. Below is my implementation:

```auto
summary_net = HierarchicalNetwork([ 
    DeepSet(summary_dim=128)
])

local_inference_net = InvertibleNetwork(
    num_params=4,num_coupling_layers=8,coupling_design='affine',
    coupling_settings=SETTINGS_POS,permutation="learnable", name="local_inference"
)

hyper_inference_net = InvertibleNetwork(
    num_params=8,num_coupling_layers=8,coupling_design='affine',
    coupling_settings=SETTINGS_POS,permutation="learnable", name="hyper_inference"
)

local_amortizer = AmortizedPosterior(local_inference_net, name="local_amortizer")
hyper_amortizer = AmortizedPosterior(hyper_inference_net, name="hyper_amortizer")
twolevel_amortizer = TwoLevelAmortizedPosterior(summary_net = summary_net,
                                                local_amortizer = local_amortizer,
                                                global_amortizer = hyper_amortizer)

```

I applied DeepSet to compress data (batchsize, 20, 12) into (batchsize, 128). then I use configurator and trianer as:

```auto
def configure_input_train(batch_size):
    out_dict = {}
    sim_data,hyper_draws, prior_draws = shearbuilding(128)
    # Add to keys
    out_dict["summary_conditions"] = sim_data 
    out_dict["hyper_parameters"] = hyper_draws
    out_dict["local_parameters"] = prior_draws 
    return out_dict

# configurator is used to connect data with amortizer
checkpoint_path="model_checkpoints/Four_modal_data20group"
trainer = Trainer(amortizer=twolevel_amortizer,generative_model=shearbuilding,configurator=configure_input_train,
                  checkpoint_path=checkpoint_path,max_to_keep=1)

```

sim\_data,hyper\_draws, prior\_draws have a shape respectively (128,20,12), (128,8), (128,20,4).  
However, it has error:

```auto
ConfigurationError: Could not carry out computations of generative_model ->configurator -> amortizer -> loss! Error trace:
 not enough values to unpack (expected 2, got 1)

```

I believe somewhere in configurator is wrong. It would be helpful if any comments are given.  
Thanks!

---

<div class="post-metadata">

**Author:** ![valentin](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/valentin/32/103_2.png) [@valentin](https://discuss.bayesflow.org/u/valentin)\
**Post date:** [May 19, 2025, 4:14pm UTC](https://discuss.bayesflow.org/t/configurationerror-in-hierarchical-model/161/2 "2025-05-19T16:14:16Z")

</div>

Hi Jice,  
could you please send a full reproducible example that produces the error? This would make it easier to trace what is going on and where the error might lie…

---

<div class="post-metadata">

**Author:** ![Jice](https://avatars.discourse-cdn.com/v4/letter/j/c77e96/32.png) [@Jice](https://discuss.bayesflow.org/u/Jice)\
**Post date:** [May 22, 2025, 6:29pm UTC](https://discuss.bayesflow.org/t/configurationerror-in-hierarchical-model/161/3 "2025-05-22T18:29:26Z")

</div>

Hi Valentin,

Thanks for checking in. I’ve been trying to resolve the issue over the past few days and found the solution after reviewing some discussions on the forum. It turns out that the hierarchical model requires input data in a 4D shape—once I made that adjustment, it worked.

Additionally, I decided to use a custom summary network instead of DeepSets or transformers. In my case, those architectures were difficult to train, time-consuming, and did not yield good performance. Instead, I manually computed summary statistics that are more sensitive to the parameters, such as the mean, standard deviation, and interquartile range. From the results, I observed that estimating \sigma was more challenging compared to \mu and \theta.

By the way, do you happen to have any papers related to the use of hierarchical models in BayesFlow?

Best regards,  
Jice

---

<div class="post-metadata">

**Author:** ![valentin](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/valentin/32/103_2.png) [@valentin](https://discuss.bayesflow.org/u/valentin)\
**Post date:** [May 22, 2025, 8:22pm UTC](https://discuss.bayesflow.org/t/configurationerror-in-hierarchical-model/161/4 "2025-05-22T20:22:43Z")

</div>

Nice that you got it to work. Regarding papers, you can take a look at [Amortized Bayesian Multilevel Models](https://arxiv.org/abs/2408.13230) by @Daniel and colleagues.
