# How to correctly load a trained ModelComparisonApproximator model?

**URL:** <https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169>\
**Category:** General\
**Created:** [June 10, 2025, 7:29am UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169 "2025-06-10T07:29:55Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![dizyd](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dizyd](https://discuss.bayesflow.org/u/dizyd)\
**Post date:** [June 10, 2025, 7:29am UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/1 "2025-06-10T07:29:55Z")

</div>

Hi everyone,

I am starting to learn more about using Bayesflow, but I don’t have much experience with `keras` before, so this might be a trivial question. After training my network using a `ModelComparisonApproximator` I saved it using `approximator.save("..\..\Results\model_v1.keras")` and then later loaded it again with `keras.saving.load_model("..\Results\model_v1.keras")`.

However, whereas the confusion matrix on simulated data using this code `pred_models = approximator.predict(conditions= simulator.sample(10000))` _directly after_ training looked like this

 ![CM_after_training](https://canada1.discourse-cdn.com/flex007/uploads/bayesflow/original/1X/764f70b1e6147895a78ac69b78d64502a66828d1.png)

it looks like this when I _reload_ the model and sample from the simulator again:

 ![CM_after_loading](https://canada1.discourse-cdn.com/flex007/uploads/bayesflow/original/1X/fc48f37cb1421b3e0162d18185dd3fbb7d2aec03.png)

So what would be the best or correct way to save a model for later use?

Best,  
David

---

<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:** [June 10, 2025, 9:43am UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/2 "2025-06-10T09:43:26Z")

</div>

Hi David, which version of bayeaflow do you have installed? Does the problem still persist when you reinstall from dev?

---

<div class="post-metadata">

**Author:** ![dizyd](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dizyd](https://discuss.bayesflow.org/u/dizyd)\
**Post date:** [June 10, 2025, 9:56am UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/3 "2025-06-10T09:56:10Z")

</div>

Hey Stefan!

I am currently using 2.0.3, I will try it with the dev version and give you an update.

---

<div class="post-metadata">

**Author:** ![dizyd](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dizyd](https://discuss.bayesflow.org/u/dizyd)\
**Post date:** [June 10, 2025, 12:28pm UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/4 "2025-06-10T12:28:55Z")

</div>

I reinstalled BayesFlow from dev using `pip install git+https://github.com/bayesflow-org/bayesflow.git@dev`. I tried again, but the results where the same, but I also got this warning now: `UserWarning: Skipping variable loading for optimizer 'adam', because it has 1 variables whereas the saved optimizer has 169 variables. `

---

<div class="post-metadata">

**Author:** ![LarsKue](https://yyz1.discourse-cdn.com/flex007/user_avatar/discuss.bayesflow.org/larskue/32/100_2.png) [@LarsKue](https://discuss.bayesflow.org/u/LarsKue)\
**Post date:** [June 10, 2025, 2:18pm UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/5 "2025-06-10T14:18:32Z")

</div>

Hi David,

thank you for reporting this. Could you please [create an issue](https://github.com/bayesflow-org/bayesflow/issues/new/choose) on the repo, including a minimal reproducible code sample? We will work on fixing the issue ASAP.

Thank you.  
Lars

---

<div class="post-metadata">

**Author:** ![dizyd](https://avatars.discourse-cdn.com/v4/letter/d/90db22/32.png) [@dizyd](https://discuss.bayesflow.org/u/dizyd)\
**Post date:** [June 11, 2025, 7:50am UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/6 "2025-06-11T07:50:33Z")

</div>

Hey, I created an issue on the repo, thank you for looking into this.

---

<div class="post-metadata">

**Author:** ![tdj2000](https://avatars.discourse-cdn.com/v4/letter/t/bbe5ce/32.png) [@tdj2000](https://discuss.bayesflow.org/u/tdj2000)\
**Post date:** [June 17, 2025, 9:01am UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/7 "2025-06-17T09:01:57Z")

</div>

Hi all, I was wondering if there is any fix here? I am having similar issues.

---

<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:** [June 17, 2025, 9:26am UTC](https://discuss.bayesflow.org/t/how-to-correctly-load-a-trained-modelcomparisonapproximator-model/169/8 "2025-06-17T09:26:51Z")

</div>

yes, this has been fixed on the dev and the fix will also be available in the next release coming up today.
