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.