# Error when using loaded approximator

**URL:** <https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170>\
**Category:** General\
**Created:** [June 10, 2025, 12:13pm UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170 "2025-06-10T12:13:23Z")\
**Posts on this page:** 10\
**Page:** 1

<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 10, 2025, 12:13pm UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/1 "2025-06-10T12:13:23Z")

</div>

When I try to use .sample when using a loaded approximator I get the following error:

ValueError: Cannot call `forward` with `strict=False` before calling `forward` with `strict=True`.

The issue does not come up when I try to use .sample straight after training the model (without first saving and loading it back in). I am using BayesFlow version 2.0.3.

---

<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 11, 2025, 6:45am UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/2 "2025-06-11T06:45:13Z")

</div>

I will investigate and get back to you. Can you show me the adapter you are using?

---

<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 11, 2025, 7:20am UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/3 "2025-06-11T07:20:56Z")

</div>

Hi Stefan, thanks for checking it out! I’m using the following adapter:

adapter = (  
bf.Adapter()  
.to\_array()  
.convert\_dtype(“float64”, “float32”)  
.concatenate([“E\_bar”, “E\_cable”, “damages”], into=“inference\_variables”)  
.concatenate([“pred\_vector”], into=“summary\_variables”)  
)

and the following approximator:

approximator = bf.BasicWorkflow(  
simulator=simulator,  
adapter=adapter,  
summary\_network=summary\_network,  
inference\_network=inference\_network,  
checkpoint\_filepath=model\_folder\_path  
)

I fit the model using:

history = approximator.fit\_offline(standardized\_training\_data, epochs=epochs, num\_batches=num\_batches, batch\_size=batch\_size, validation\_data=standardized\_validation\_data, callbacks=[StepHistory()])

I load pretrained models using:

pretrained\_approximator = keras.saving.load\_model(os.path.join(model\_folder\_path, ‘model.keras’))

---

<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:** [June 11, 2025, 7:54am UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/4 "2025-06-11T07:54:24Z")

</div>

Hi Tom,

Thanks for the context info! This reminds me of [this issue that was also using offline training](https://github.com/bayesflow-org/bayesflow/issues/255#issuecomment-2656804906) - could it be a similar problem that we have two disconnected adapters in the OfflineDataset and approximator here? I.e., I don’t know about the rest of your workflow but do you also load a saved dataset here?

If the source of the error is still unclear, it would help us a lot of you could [open an issue with a minimal reproducible code example](https://github.com/bayesflow-org/bayesflow/issues/new/choose) so that we can track down the issue. In the meantime, a quick bandaid fix so that you can continue with your project would be to manually call the adapter once (e.g., `_ = adapter(data)`) before sampling.

Best,  
Lasse

---

<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 11, 2025, 8:59am UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/5 "2025-06-11T08:59:37Z")

</div>

Hi Lasse, thanks for the swift reply!

I think indeed the issue is similar, I create an offline dataset separately and load in this dataset using pickle. For now I’ve opted for the bandaid fix, which works fine for me.

Cheers, Tom

---

<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 11, 2025, 9:30am UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/6 "2025-06-11T09:30:07Z")

</div>

Hi Tom, just to confirm. Are you using `approximator.sample()` or `workflow.sample()` after loading the model?

---

<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 11, 2025, 9:44am UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/7 "2025-06-11T09:44:40Z")

</div>

Hi Stefan,

I use the following:

approximator = bf.BasicWorkflow(  
simulator=simulator,  
adapter=adapter,  
summary\_network=summary\_network,  
inference\_network=inference\_network,  
checkpoint\_filepath=model\_folder\_path  
)

approximator = keras.saving.load\_model(os.path.join(model\_folder\_path, ‘model.keras’))

post\_draws = approximator.sample(conditions=standardized\_standard\_test\_data, num\_samples=1000)

---

<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, 10:02am UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/8 "2025-06-17T10:02:52Z")

</div>

I am not able to reproduce the problem in any of my custom training pipelines. Does the problem still persist on dev?

---

<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, 1:27pm UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/9 "2025-06-17T13:27:21Z")

</div>

Hi Stefan,

The problem is resolved on dev, thanks for checking it out!

---

<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, 3:18pm UTC](https://discuss.bayesflow.org/t/error-when-using-loaded-approximator/170/10 "2025-06-17T15:18:50Z")

</div>

Cool, thanks for confirming. `dev` is becoming the new release later today.  
S.
