# Confused in training \`AmortizedPosteriorEstimator\`

**URL:** <https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84>\
**Category:** General\
**Created:** [March 31, 2024, 5:19pm UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84 "2024-03-31T17:19:29Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![smabbasht](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@smabbasht](https://discuss.bayesflow.org/u/smabbasht)\
**Post date:** [March 31, 2024, 5:19pm UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84/1 "2024-03-31T17:19:29Z")

</div>

I am fairly new to Bayesflow and BayesianFlowNetworks in general, I was previously developing a Bayesian Neural Network using tensorflow\_probability but moved to BFN once I saw Bayesflow.

I have simulated data related to Astrophysics that is stored on the disk which is actually a dataset with features and corresponding labels as in usual deep learning,

I have a few questions, for each of them I have gone through the docs but wasn’t able to clear myself on the matter:

1. While training, using the trainer.train\_offline(), what is the simulation\_dict supposed to be, should it only contain my X\_train, or is it supposed to contain both X\_train, y\_train, or y\_train only, consequently what is prior\_draws in this context, is it related to y\_train or is it only the draws from prior object I defined,

2. I have a 3D object as input for each example and a 1D vector (len=3) as output labes, so with batch\_size the exact shape is (batch\_size, 100, 1000, 2) for each input, how do I incorporate this in a DeepSet model, it seems to only accept 2D inputs, not 3D.

---

<div class="post-metadata">

**Author:** ![marvinschmitt](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/marvinschmitt/32/98_2.png) [@marvinschmitt](https://discuss.bayesflow.org/u/marvinschmitt)\
**Post date:** [April 2, 2024, 8:55am UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84/2 "2024-04-02T08:55:20Z")

</div>

Hi, welcome to the BayesFlow Forums!

Just to clear up any misunderstandings: BayesFlow does **not** (currently) implement Bayesian Flow Networks (BFN; [[2308.07037] Bayesian Flow Networks](https://arxiv.org/abs/2308.07037)). BFNs are quite new and the name collision is an unfortunate coincidence.

BayesFlow mainly implements normalizing flows (hence the name) and flow matching for conditional density estimation. You can use BayesFlow for (Bayesian) parameter estimation or model comparison.

You can find more information here:

- [Journal of Open Source Software: BayesFlow: Amortized Bayesian Workflows With Neural Networks](https://joss.theoj.org/papers/10.21105/joss.05702)
- [[2005.03899] Amortized Bayesian Inference for Models of Cognition](https://arxiv.org/abs/2005.03899) (in the context of cognitive models, but directly applies to astrophysics estimation tasks as well)

Hope that helps. Cheers,  
Marvin

---

<div class="post-metadata">

**Author:** ![smabbasht](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@smabbasht](https://discuss.bayesflow.org/u/smabbasht)\
**Post date:** [April 2, 2024, 11:09pm UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84/3 "2024-04-02T23:09:17Z")

</div>

Thankyou for taking out time to reply, I really appreciate that. Could you please respond to my second question too? I have figured out the first one but would appreciate a response on that too Thanks again

---

<div class="post-metadata">

**Author:** ![KLDivergence](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/kldivergence/32/15_2.png) [@KLDivergence](https://discuss.bayesflow.org/u/KLDivergence)\
**Post date:** [April 3, 2024, 1:33am UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84/4 "2024-04-03T01:33:14Z")

</div>

Since your summary network needs to reduce a 3D tensor to a 1D vector for conditioning, you can use the `HierarchicalNetwork` interface, e.g.,:

```auto
summary_net = bf.networks.HierarchicalNetwork([
    bf.networks.DeepSet(summary_dim=16), 
    bf.networks.DeepSet(summary_dim=64)
])

```

This network will successively reduce a 4D batch into a 2D batch. Note, however, that it assumes exchangeability (i.e., IID data points) for each of the axes. If that is not desirable, you can use a backbone network different than a DeepSet.

Let us know if that helps!

---

<div class="post-metadata">

**Author:** ![smabbasht](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@smabbasht](https://discuss.bayesflow.org/u/smabbasht)\
**Post date:** [April 21, 2024, 10:59pm UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84/5 "2024-04-21T22:59:41Z")

</div>

Thanks for your reply. I have found my way through the problem using a mix of Conv2D layes and SequenceNetwork. It is working pretty well, I have also saved the trained checkpoints for summary network,

However for the inference network, I am not sure if that can be saved or not. I can’t find anything on Bayesflow docs or forum about that. Does it not need to be saved? My inference network precisely is as follows:

```auto
self.inference_network = InvertibleNetwork(
            num_params=8, # parameters to estimate
            num_coupling_layers=8,
            use_act_norm=True,
            coupling_design="affine",
            permutation="learnable",
            coupling_settings={"mc_dropout" : True, "dense_args" : dict(units=128, activation="elu")}
        )

```

Other than this is there anyway we can load the saved models without starting the training phase?

(just btw I am following the quick amortized posterior estimation guide as a baseline)

Thanks in advance

---

<div class="post-metadata">

**Author:** ![elseml](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/elseml/32/19_2.png) [@elseml](https://discuss.bayesflow.org/u/elseml)\
**Post date:** [April 22, 2024, 8:58am UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84/6 "2024-04-22T08:58:57Z")

</div>

Hi,

BayesFlow currently takes care of saving/loading all networks contained in your `Amortizer` instance via the `Trainer` instance. You can find an example in [this tutorial notebook](https://github.com/stefanradev93/BayesFlow/blob/master/examples/LCA_Model_Posterior_Estimation.ipynb) in the “Defining the Trainer” section. If checkpoints are already present at the checkpoint path, initializing the trainer loads them and you can proceed with your pre-trained networks without any further training.

Best,  
Lasse

---

<div class="post-metadata">

**Author:** ![smabbasht](https://avatars.discourse-cdn.com/v4/letter/s/c6cbf5/32.png) [@smabbasht](https://discuss.bayesflow.org/u/smabbasht)\
**Post date:** [April 23, 2024, 6:27am UTC](https://discuss.bayesflow.org/t/confused-in-training-amortizedposteriorestimator/84/7 "2024-04-23T06:27:45Z")

</div>

Hey, Thankyou so much for the reply, I was missing the checkpoint\_path parameter in defining the trainer while giving this in trainer.train\_offline and was wondering why is it not working. It works.

I wholeheartedly thank you guys for the framework and to the amazing team you have.
