Keras ValueError re-appearing in newest package version?

Hi all, I seem to have bumped into an issue previously seen as resolved:

As discussed in that post, using SetTransformer or DeepSet results in an error once workflow.fit() is called:

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 'SetAttention', Received `build()` argument `self`, which does not end in `_shape`.

I am pretty new to code discussion forums etc, but my attempt at a minimal reproducible example (it errors before initializing, not sure it would run properly if it did but gets the point across?):

    
workflow = bf.BasicWorkflow(
    inference_network=bf.networks.FlowMatching(),
    inference_variables=["parameters"],
    summary_variables=["observables"],
    summary_network=bf.networks.SetTransformer(),
    simulator=bf.simulators.SIR(),
)

history = workflow.fit_online(
    epochs=2,
    batch_size=32,
    num_batches_per_epoch=200,
)

Bayesflow version: 2.0.11

Keras 3.14.0

Python 3.11

Happy to provide more info as guided by the community! Thanks for all the help :slight_smile:

Hi, I believe the error is a mock error as the workflow can’t possible run: the default SIR settings follow the formulation from the SBI benchmark which does not foresee the use of summary networks.

If you want to test the particular example, you need to initialize your workflow like so:

import bayesflow as bf

workflow = bf.BasicWorkflow(
    inference_network=bf.networks.FlowMatching(),
    inference_variables=["parameters"],
    summary_variables=["observables"],
    summary_network=bf.networks.TimeSeriesTransformer(),
    simulator=bf.simulators.SIR(subsample=None),
)

The SetTransformer is also not intended for ordered sequences (i.e., time series). Check out the improved User Guide on summary networks or let me know if the issues persists.

Hi, yep poor choice of example… But it’s definitely a bug:

import numpy as np

def prior():
    return dict(theta=np.random.normal(size=2))  # [mu, log_sigma]

def likelihood(theta):
    mu, log_sigma = theta
    sigma = np.exp(log_sigma * 0.5)
    return dict(
        x=np.random.normal(mu, sigma, size=(30, 1)),
        y=np.random.normal(mu, sigma, size=(30, 1)),
        z=np.random.normal(mu, sigma, size=(30, 1)),
    )

simulator = bf.make_simulator([prior, likelihood])
    
workflow = bf.BasicWorkflow(
    simulator=simulator,
    inference_variables=["theta"],
    summary_variables=["x", "y", "z"],           # tells the workflow what to summarize
    inference_network=bf.networks.FlowMatching(),
    summary_network=bf.networks.TimeSeriesTransformer(summary_dim=16),
)

history = workflow.fit_online(epochs=5, batch_size=128, num_batches=100)

Using the summary timeseries example straight from the userguide retains the same error!

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 'MultiHeadAttention', Received `build()` argument `self`, which does not end in `_shape`.

Does that make sense?

Hi Dani, @jerry and I were unable to reproduce the error on any of the three backends (JAX, PyTorch, TensorFlow) using bayesflow 2.0.11 on either Linux or Windows with a Python 3.11 or 3.12 env. I am very puzzled as to why the error is that the self argument is flagged. Does it happen in a clean environment too? Can you post the full error trace?

Hi @floralynth , which backend did you choose? I was unable to reproduce this error with the latest version on a JAX backend.

1 Like

Hi, this is the keras Jax backend, tried both CUDA12 and CPU jax targets.

Weirdly, I can only seem to reproduce this on linux. My windows laptop does not run into the same issues despite identical jax, bayesflow and keras installs.

Anything I can try to isolate the issue?

Sorry, missed this response! I set up a new virtual environment and just ran pip install bayesflow, jax.

This seems about as minimal as I could get? then ran the code above. See full trace below:

(venv) me:~/Documents/repos/bf_minimal$ python3.11 example.py
INFO:jax._src.xla_bridge:Unable to initialize backend 'tpu': INTERNAL: Failed to open libtpu.so: libtpu.so: cannot open shared object file: No such file or directory
WARNING:jax._src.xla_bridge:An NVIDIA GPU may be present on this machine, but a CUDA-enabled jaxlib is not installed. Falling back to cpu.
INFO:bayesflow:Using backend 'jax'
INFO:bayesflow:Fitting on dataset instance of OnlineDataset.
INFO:bayesflow:Building on a test batch.
Traceback (most recent call last):
  File "/home/mydetails/Documents/repos/bf_minimal/example.py", line 24, in <module>
    history = workflow.fit_online(epochs=5, batch_size=128, num_batches=100)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/workflows/basic_workflow.py", line 340, in fit_online
    return self._fit(
           ^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/workflows/basic_workflow.py", line 467, in _fit
    self.history = self.approximator.fit(
                   ^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/approximators/continuous_approximator.py", line 182, in fit
    return super().fit(*args, **kwargs, adapter=self.adapter)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/approximators/approximator.py", line 534, in fit
    self.build_from_data(mock_data)
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/approximators/approximator.py", line 540, in build_from_data
    self.build(keras.tree.map_structure(keras.ops.shape, adapted_data))
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/keras/src/layers/layer.py", line 233, in build_wrapper
    original_build_method(*args, **kwargs)
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/approximators/approximator.py", line 66, in build
    summary_outputs_shape = self._build_summary_network(data_shapes)
                            ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/approximators/approximator.py", line 98, in _build_summary_network
    self.summary_network.build(data_shapes["summary_variables"])
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/keras/src/layers/layer.py", line 233, in build_wrapper
    original_build_method(*args, **kwargs)
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/utils/decorators.py", line 95, in wrapper
    return fn(*args, **kwargs)
           ^^^^^^^^^^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/networks/summary/summary_network.py", line 57, in build
    z = self.call(x)
        ^^^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/bayesflow/networks/summary/transformers/time_series_transformer.py", line 154, in call
    inp = layer(inp, inp, training=training, attention_mask=attention_mask)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/keras/src/utils/traceback_utils.py", line 122, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/home/mydetails/Documents/repos/bf_minimal/venv/lib/python3.11/site-packages/keras/src/layers/layer.py", line 2041, in update_shapes_dict_for_target_fn
    raise ValueError(
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 'MultiHeadAttention', Received `build()` argument `self`, which does not end in `_shape`.

Hi, not sure what etiquette is here but I resolved this:

Looks like ubuntu ships with python 3.11rc1 not 3.11.0, by upgrading to 3.12 this seemed to resolve the problem. Perhaps there is something specifically problematic with the default 3.11rc1 install from Ubuntu’s repositories? I used deadsnakes to get the 3.12 which was otherwise unavailable.

2 Likes

Ok, that’s great to here! There is no etiquette, but now others who encounter this specific issue have a chance at finding a solution.