Getting error running Linear Regression Example

I am trying to become familiar with BayesFlow by going through the example scripts. I installed the latest version of Bayesflow and just tried to run the Linear_Regression_Starter example right from the Juypter notebook. All of the code up to the point where I try to run the approximate.fit command. However, when I try to run “approximator.fit” command in the example, I get an error. It seems like it starts running the function okay, but then runs into an issue.
Again, I apologize if I am missing something simple.

history = approximator.fit(
    epochs=epochs,
    num_batches=num_batches,
    batch_size=batch_size,
    simulator=simulator,
)
INFO:bayesflow:Building dataset from simulator instance of SequentialSimulator.
INFO:bayesflow:Using 8 data loading workers.
INFO:bayesflow:Building on a test batch.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[76], line 1
----> 1 history = approximator.fit(
      2     epochs=epochs,
      3     num_batches=num_batches,
      4     batch_size=batch_size,
      5     simulator=simulator,
      6 )

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/bayesflow/approximators/continuous_approximator.py:202, in ContinuousApproximator.fit(self, *args, **kwargs)
    150 def fit(self, *args, **kwargs):
    151     """
    152     Trains the approximator on the provided dataset or on-demand data generated from the given simulator.
    153     If `dataset` is not provided, a dataset is built from the `simulator`.
   (...)    200         If both `dataset` and `simulator` are provided or neither is provided.
    201     """
--> 202     return super().fit(*args, **kwargs, adapter=self.adapter)

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/bayesflow/approximators/approximator.py:134, in Approximator.fit(self, dataset, simulator, **kwargs)
    132     mock_data = dataset[0]
    133     mock_data = keras.tree.map_structure(keras.ops.convert_to_tensor, mock_data)
--> 134     self.build_from_data(mock_data)
    136 return super().fit(dataset=dataset, **kwargs)
...
   1802     )
   1803 expected_call_arg = utils.removesuffix(name, "_shape")
   1804 if expected_call_arg not in call_spec.arguments_dict:

ValueError: For a `build()` method with more than one argument, all arguments should have a `_shape` suffix and match an argument from `call()`. E.g. `build(self, foo_shape, bar_shape)`  For layer 'DeepSet', Received `build()` argument `self`, which does not end in `_shape`.
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...

Thanks for reaching out and welcome to the forum! I just tried it out, but cannot reproduce the error in my local setup. I would be grateful if you would help us to track this down.
For this, it would be great if you update your installation to the latest version of the main branch, try to run the notebook for the three different backends (Jax, TensorFlow, PyTorch), and report if you see the error everywhere or only for one of them. In addition, details on your setup (operating system, CPU or GPU) might help as well.

Valentin

Thanks for your quick reply!

I went back and reinstalled Bayesflow 2.0 from Github, no issues.
I then went through and ran the notebook using the three different backends. jax, TensorFlow, and torch. For each of the three backends, I got the same error as in my first post.

As for my setup, I am running the Bayesflow using Python 3.11.0 operating in Visual Studio.
I am currently using a Mac Pro 16 GB (Graphics Intel HD Graphics 630 1536 MB), operating system BigSur.

Any suggestions you have would be greatly appreciated.

-Mike

1 Like

Thanks for the additional testing! This is really strange, as what it seems to be complaining about is this method, which (except for self) only takes one argument. The error originates from this code in the Keras Layer class, which seems to not properly exclude self from the list of arguments. I’m not sure why it is triggered here, and why it is triggered only for you and not for others. Which version of Keras do you have installed currently?
I’ll also ask around if someone has similar hardware and can reproduce this.
If you run one of the other notebooks (e.g., the Two_Moons_Starter.ipynb), do you encounter problems as well? Another thing to try is to replace the DeepSet with a SetTransformer, to see if this is specific to the DeepSet class. As the SetTransformer uses the same build pattern, I expect to see the same error there, though.

I have just run it on my Mac (Sequoia 15.3.2) with Python 3.11.0 and it seems all fine. Perhaps an issue with Keras version as @valentin suggested?

1 Like

One more question, do you currently run bayesflow on CPU or GPU with your current setup? I am currently running on CPU only on my mac. Perhaps that explains it? Could try running on CPU and see if this fixes it. At least then we could know roughly the source of the problem.

Thanks again for your feedback. From some additional testing, I might have made some progress. I was running Keras version 3.9.0.
Using Keras version 3.9.0 I ran the Two_Moons_Starter.ipynb example. I was not able to fully run the Two_Moons_Starter notebook, but I was able to run the notebook longer than the Linear_Regression example. I got a new error once I tried running the consistency_model_workflow.fit_offline function (error listed below).

history = consistency_model_workflow.fit_offline(
    training_data, 
    epochs=epochs, 
    batch_size=batch_size, 
    validation_data=validation_data
)

TypeError                                 Traceback (most recent call last)
Cell In[13], line 1
----> 1 history = consistency_model_workflow.fit_offline(
      2     training_data, 
      3     epochs=epochs, 
      4     batch_size=batch_size, 
      5     validation_data=validation_data
      6 )

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/bayesflow/workflows/basic_workflow.py:668, in BasicWorkflow.fit_offline(self, data, epochs, batch_size, keep_optimizer, validation_data, **kwargs)
    633 """
    634 Train the approximator offline using a fixed dataset. This approach will be faster than online training,
    635 since no computation time is spent in generating new data for each batch, but it assumes that simulations
   (...)    663     metric evolution over epochs.
    664 """
    666 dataset = OfflineDataset(data=data, batch_size=batch_size, adapter=self.adapter)
--> 668 return self._fit(
    669     dataset, epochs, strategy="online", keep_optimizer=keep_optimizer, validation_data=validation_data, **kwargs
    670 )

File /Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/bayesflow/workflows/basic_workflow.py:864, in BasicWorkflow._fit(self, dataset, epochs, strategy, keep_optimizer, validation_data, **kwargs)
    862     return self.history
    863 except Exception as err:
--> 864     raise err
...
    139 )
    141 loss = inference_metrics.get("loss", keras.ops.zeros(())) + summary_metrics.get("loss", keras.ops.zeros(()))
    143 inference_metrics = {f"{key}/inference_{key}": value for key, value in inference_metrics.items()}
TypeError: ConsistencyModel.compute_metrics() got an unexpected keyword argument 'sample_weight'
Output is truncated. View as a scrollable element or open in a text editor. Adjust cell output settings...


Afterwards, I updated keras to the latest version (3.9.2) and then tried to run both the linear_regression_example (using Jax, torch, and Tensofor flow) as well as the Two_Moons_Starter.ipynb notebook. Running the scripts with the updated keras I experienced the same error in the linear_regression (initial post) and with the Two_moons_Starter notebook (shown above).

In case this information is helpful, here are the versions of the other packages that I am using.
Keras: 3.9.2
tensorFlow: 2.16.2
jax:0.4.38
torch:2.2.2

Paul

I am sorry for my ignorance, but I am not sure if I am running on my GPU or CPU. Python is not my primary language, and I don’t have a strong computer science background. If you have any resources to know if I am running on my GPU vs CPU, I would be happy to run the scripts on my CPU.

Also, thanks for checking the Bayesflows examples on your computer.

Thanks
Mike

That is very strange.

About the GPU question, which deep learning backend of the 3 are you currently using? You installed all 3 apparently, but I am not sure with which you run the notebooks.

Hi, in case it if of any help to anyone; I have the same issue where I get the following error when running history = workflow.fit_online(epochs=50, batch_size=64, num_batches_per_epoch=200) in Linear_Regression_Starter.ipynb.

ValueError: For a `build()` method with more than one argument, all arguments should have a `_shape` suffix and match an argument from `call()`. E.g. `build(self, foo_shape, bar_shape)`  For layer 'SetTransformer', Received `build()` argument `self`, which does not end in `_shape`.

I am however able to run the entirety of the Two_Moons_Starter.ipynb on MacOS Sequoia 15.4. I installed bayesflow==2.0.3 via the main branch with keras==3.9.1, and Tensorflow==2.19.0 as the backend.

1 Like

Hi all, I cannot reproduce the error on either backend using Windows or Linux. What could be amiss here?

Hi luka,
thanks a lot for sharing! Would you be willing to try a few things so that we can see whether we can fix this?

The issue is a bit weird, so I can only guess. The error message does not really make sense, but is related to the parameter names used in call and build. Usually, they should be some parameter name in call, e.g. input or x, and the same name with the suffix _shape in build, so input_shape or x_shape.
If there is only one parameter, a mismatch does not pose a problem and is normally ignored. Here, it isn’t ignored (hence the error), but the error message highlights the self parameter and not the input_set parameter.
In the SetTransformer class there is a mismatch. In call we have input_set, whereas in build, we have input_shape (inherited from SummaryNetwork). I have changed this in this branch:

You can use

pip install https://github.com/bayesflow-org/bayesflow.git@fix-issue-mac-shape

to try it out. It would be great if you let us know if this changes anything :slight_smile:

@KLDivergence See the history of the thread, for now we only encounter the issue on Macs, but also not on all Macs. It is all a bit weird…

Hi valentin,

I have run the Linear_Regression_Starter.ipynb notebook again in a different environment using the fix-issue-mac-shape branch, but I get the same error as before. The Two_Moons_Starter.ipynb notebook still runs fine.
I am happy to test anything else if it is of any help!

1 Like

Thanks a lot for trying it out! Two_Moons_Starter.ipynb working and Linear_Regression_Starter.ipynb failing indicates a problem with the summary network(s), the error message also goes into this direction. To narrow this down a bit, could you please replace the SetTransformer in the notebook with a DeepSet, and report if and which error message you get?

Hi Valentin,

Replacing SetTransformer with DeepSet gives me a similar error:


history = workflow.fit_online(epochs=50, batch_size=64, num_batches_per_epoch=200)

INFO:bayesflow:Fitting on dataset instance of OnlineDataset.
INFO:bayesflow:Building on a test batch.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[19], line 1
----> 1 history = workflow.fit_online(epochs=50, batch_size=64, num_batches_per_epoch=200)

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/bayesflow/workflows/basic_workflow.py:761, in BasicWorkflow.fit_online(self, epochs, num_batches_per_epoch, batch_size, keep_optimizer, validation_data, **kwargs)
    727 """
    728 Train the approximator using an online data-generating process. The dataset is dynamically generated during
    729 training, making this approach suitable for scenarios where generating new simulations is computationally cheap.
   (...)    754     metric evolution over epochs.
    755 """
    757 dataset = OnlineDataset(
    758     simulator=self.simulator, batch_size=batch_size, num_batches=num_batches_per_epoch, adapter=self.adapter
    759 )
--> 761 return self._fit(
    762     dataset, epochs, strategy="online", keep_optimizer=keep_optimizer, validation_data=validation_data, **kwargs
    763 )

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/bayesflow/workflows/basic_workflow.py:913, in BasicWorkflow._fit(self, dataset, epochs, strategy, keep_optimizer, validation_data, **kwargs)
    910     self.approximator.compile(optimizer=self.optimizer, metrics=kwargs.pop("metrics", None))
    912 try:
--> 913     self.history = self.approximator.fit(
    914         dataset=dataset, epochs=epochs, validation_data=validation_data, **kwargs
    915     )
    916     self._on_training_finished()
    917     return self.history

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/bayesflow/approximators/continuous_approximator.py:214, in ContinuousApproximator.fit(self, *args, **kwargs)
    162 def fit(self, *args, **kwargs):
    163     """
    164     Trains the approximator on the provided dataset or on-demand data generated from the given simulator.
    165     If `dataset` is not provided, a dataset is built from the `simulator`.
   (...)    212         If both `dataset` and `simulator` are provided or neither is provided.
    213     """
--> 214     return super().fit(*args, **kwargs, adapter=self.adapter)

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/bayesflow/approximators/approximator.py:137, in Approximator.fit(self, dataset, simulator, **kwargs)
    135     mock_data = dataset[0]
    136     mock_data = keras.tree.map_structure(keras.ops.convert_to_tensor, mock_data)
--> 137     self.build_from_data(mock_data)
    139 return super().fit(dataset=dataset, **kwargs)

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/bayesflow/approximators/approximator.py:26, in Approximator.build_from_data(self, data)
     25 def build_from_data(self, data: Mapping[str, any]) -> None:
---> 26     self.compute_metrics(**filter_kwargs(data, self.compute_metrics), stage="training")
     27     self.built = True

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/bayesflow/approximators/continuous_approximator.py:138, in ContinuousApproximator.compute_metrics(self, inference_variables, inference_conditions, summary_variables, sample_weight, stage)
    135 if summary_variables is None:
    136     raise ValueError("Summary variables are required when a summary network is present.")
--> 138 summary_metrics = self.summary_network.compute_metrics(summary_variables, stage=stage)
    139 summary_outputs = summary_metrics.pop("outputs")
    141 # append summary outputs to inference conditions

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/bayesflow/networks/summary_network.py:38, in SummaryNetwork.compute_metrics(self, x, stage, **kwargs)
     37 def compute_metrics(self, x: Tensor, stage: str = "training", **kwargs) -> dict[str, Tensor]:
---> 38     outputs = self(x, training=stage == "training")
     40     metrics = {"outputs": outputs}
     42     if self.base_distribution is not None:

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py:122, in filter_traceback.<locals>.error_handler(*args, **kwargs)
    119     filtered_tb = _process_traceback_frames(e.__traceback__)
    120     # To get the full stack trace, call:
    121     # `keras.config.disable_traceback_filtering()`
--> 122     raise e.with_traceback(filtered_tb) from None
    123 finally:
    124     del filtered_tb

File /opt/anaconda3/envs/bfnnew/lib/python3.11/site-packages/keras/src/layers/layer.py:1805, in update_shapes_dict_for_target_fn(target_fn, shapes_dict, call_spec, class_name)
   1799 error_preamble = (
   1800     f"For a `{method_name}()` method with more than one argument, all "
   1801     "arguments should have a `_shape` suffix and match an argument "
   1802     f"from `call()`. E.g. `{method_name}(self, foo_shape, bar_shape)` "
   1803 )
   1804 if not name.endswith("_shape"):
-> 1805     raise ValueError(
   1806         f"{error_preamble} For layer '{class_name}', "
   1807         f"Received `{method_name}()` argument "
   1808         f"`{name}`, which does not end in `_shape`."
   1809     )
   1810 expected_call_arg = utils.removesuffix(name, "_shape")
   1811 if expected_call_arg not in call_spec.arguments_dict:

ValueError: For a `build()` method with more than one argument, all arguments should have a `_shape` suffix and match an argument from `call()`. E.g. `build(self, foo_shape, bar_shape)`  For layer 'DeepSet', Received `build()` argument `self`, which does not end in `_shape`.

Hi luka,
thanks for trying it out. I think our best way forward is to try to construct a minimal reproducible example of the behavior, if possible in Keras 3 code without using BayesFlow, so that we can potentially file an issue with Keras. If we cannot achieve this, maybe the process gives us hints where the BayesFlow code does something unusual that causes the error.
We will try to come up with something, but we are quite limited as we cannot reproduce the problem locally. If you have ideas in the meantime, feel free to experiment as well and let us know your results.

1 Like

Hi valentin,

That makes sense, let me know if I can help again at a later time!

Hi @luka and @Collins.282

Could you please provide a lockfile for a clean environment where you install BayesFlow? A pip log would also work, e.g. something like

conda create -n test python=3.11
conda activate test
pip install -U jax git+https://github.com/bayesflow-org/bayesflow.git@dev -v --log install_log.txt