Skip to content

Improve serialization and allow networks to be passed directly to most models #413

New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Merged
merged 78 commits into from
Apr 17, 2025

Conversation

LarsKue
Copy link
Contributor

@LarsKue LarsKue commented Apr 14, 2025

This PR includes:

  • wrappers for serialization which simplify the process of type and value serialization as well as decoration
  • improved from_config and get_config methods for most models which allow the following pattern
fm = bf.networks.FlowMatching(subnet=bf.networks.MLP([64, 64]))
  • improved model summary via network.summary(expand_nested=True) via subclassing from keras.Model instead of keras.Layer
  • an overhaul of our innermost network, the MLP, which should improve robustness and gradient flow
  • an experimental ResNet implementation
  • overhauls of some build and compute_output_shape methods
  • several other cleanups

Note that the subnet_kwargs pattern is now deprecated in networks that do not require it. We might aim to remove this feature in the future.

Some drawbacks:

  • The new MLP is not compatible with the old MLP, so users will not be able to load their networks after this PR is merged.
  • Since we moved to keras.Model, our serialization testing became much more strict. This means that individual layers of each model must now also conform to consistent naming, i.e. the name cannot change e.g. from dense_1 to dense_2. This usually means that models must name their layers statically. I have disabled the test for this for now, since implementing this in one go is a lot of work, but we should aim to re-enable this test in the future.
  • Some networks that previously did not require a from_config and get_config method now do, even when they only have python primitives as constructor arguments (due to the above issue).
  • PointInferenceNetwork and its constituents are currently exempt from improvements as they posed a significant hurdle in conversion. Perhaps @han-ol can port them to the new idiom, after some correspondence.

Closes #343
Closes #391

@LarsKue LarsKue added the refactoring Some code shall be redesigned label Apr 14, 2025
@LarsKue LarsKue added this to the BayesFlow 2.0 milestone Apr 14, 2025
@LarsKue LarsKue self-assigned this Apr 14, 2025
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 60 out of 60 changed files in this pull request and generated 1 comment.

Comments suppressed due to low confidence (1)

bayesflow/experimental/resnet/resnet.py:33

  • Reassigning the 'activation' parameter within the loop may cause unintended behavior in subsequent iterations. Consider using a separate variable (e.g., 'layer_activation') for each iteration to preserve the original configuration.
activation = keras.activations.get(activation)

Copy link

codecov bot commented Apr 14, 2025

Codecov Report

Attention: Patch coverage is 69.88473% with 209 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
bayesflow/experimental/resnet/double_conv.py 0.00% 37 Missing ⚠️
bayesflow/experimental/resnet/double_linear.py 0.00% 32 Missing ⚠️
bayesflow/experimental/resnet/resnet.py 0.00% 32 Missing ⚠️
bayesflow/experimental/resnet/dense_resnet.py 0.00% 30 Missing ⚠️
...w/networks/consistency_models/consistency_model.py 33.33% 18 Missing ⚠️
.../experimental/continuous_time_consistency_model.py 33.33% 14 Missing ⚠️
bayesflow/networks/mlp/mlp.py 89.83% 6 Missing ⚠️
bayesflow/networks/inference_network.py 44.44% 5 Missing ⚠️
...flow/experimental/free_form_flow/free_form_flow.py 86.66% 4 Missing ⚠️
.../networks/coupling_flow/couplings/dual_coupling.py 63.63% 4 Missing ⚠️
... and 17 more
Files with missing lines Coverage Δ
bayesflow/adapters/transforms/as_set.py 92.30% <100.00%> (-1.45%) ⬇️
bayesflow/adapters/transforms/as_time_series.py 92.30% <100.00%> (-1.45%) ⬇️
bayesflow/adapters/transforms/broadcast.py 82.97% <100.00%> (-2.48%) ⬇️
bayesflow/adapters/transforms/concatenate.py 84.31% <100.00%> (-0.60%) ⬇️
bayesflow/adapters/transforms/constrain.py 66.66% <100.00%> (-0.78%) ⬇️
bayesflow/adapters/transforms/convert_dtype.py 100.00% <100.00%> (ø)
bayesflow/adapters/transforms/drop.py 93.33% <100.00%> (-1.12%) ⬇️
...sflow/adapters/transforms/elementwise_transform.py 82.35% <100.00%> (+5.88%) ⬆️
bayesflow/adapters/transforms/expand_dims.py 100.00% <100.00%> (ø)
bayesflow/adapters/transforms/filter_transform.py 70.21% <100.00%> (-0.67%) ⬇️
... and 64 more

... and 17 files with indirect coverage changes

@LarsKue LarsKue added feature New feature or request user interface Changes to the user interface and improvements in usability labels Apr 17, 2025
@LarsKue LarsKue changed the title Improve Serialization and allow networks to be passed directly to most models Improve serialization and allow networks to be passed directly to most models Apr 17, 2025
@LarsKue LarsKue requested a review from Copilot April 17, 2025 15:07
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR improves the serialization approach for transforms and adapters by replacing the legacy Keras serialization calls with the new bayesflow.utils.serialization methods. In addition, it refactors some APIs (for example, renaming internal parameters) and updates configuration methods to use a unified serialization format.

  • Updated all serialization decorators and get_config methods to use bayesflow’s new serialization utilities.
  • Removed redundant from_config methods in many transform classes.
  • Renamed internal parameters (e.g. _indices → indices) to improve clarity.

Reviewed Changes

Copilot reviewed 93 out of 93 changed files in this pull request and generated no comments.

Show a summary per file
File Description
bayesflow/adapters/transforms/sqrt.py Updated serialization decorator and removed redundant from_config method
bayesflow/adapters/transforms/shift.py Switched to bayesflow serialization and updated get_config implementation
bayesflow/adapters/transforms/scale.py Switched to bayesflow serialization and updated get_config implementation
bayesflow/adapters/transforms/rename.py Switched to bayesflow serialization for get_config
bayesflow/adapters/transforms/one_hot.py Updated serialization usage in get_config
bayesflow/adapters/transforms/numpy_transform.py Updated get_config to serialize function names
bayesflow/adapters/transforms/map_transform.py Switched to bayesflow serialization for get_config
bayesflow/adapters/transforms/log.py Updated serialization usage in get_config
bayesflow/adapters/transforms/keep.py Updated serialization usage in get_config
bayesflow/adapters/transforms/filter_transform.py Updated get_config implementation with kwargs merge and updated serialization
bayesflow/adapters/transforms/expand_dims.py Updated serialization usage in get_config
bayesflow/adapters/transforms/elementwise_transform.py Changed from_config to use deserialize and init parameters
bayesflow/adapters/transforms/drop.py Updated serialization usage in get_config
bayesflow/adapters/transforms/convert_dtype.py Updated serialization usage and changed astype calls to use copy=False
bayesflow/adapters/transforms/constrain.py Updated serialization usage in get_config
bayesflow/adapters/transforms/concatenate.py Renamed internal parameter from _indices to indices and updated get_config accordingly
bayesflow/adapters/transforms/broadcast.py Updated serialization usage in get_config
bayesflow/adapters/transforms/as_time_series.py Updated serialization decorator
bayesflow/adapters/transforms/as_set.py Updated serialization decorator
bayesflow/adapters/adapter.py Updated serialization for adapter configuration
Comments suppressed due to low confidence (3)

bayesflow/adapters/transforms/concatenate.py:35

  • Renaming the parameter from '_indices' to 'indices' improves clarity, but please ensure that all references in this class are updated accordingly.
def __init__(self, keys: Sequence[str], *, into: str, axis: int = -1, indices: list | None = None):

bayesflow/adapters/transforms/filter_transform.py:80

  • Merging 'self.kwargs' into the configuration dictionary may unintentionally override keys such as 'include' or 'predicate' if present; consider explicitly handling potential key collisions.
config = {"include": self.include, "transform_constructor": self.transform_constructor, "predicate": self.predicate, "exclude": self.exclude, "transform_map": self.transform_map, **self.kwargs}

bayesflow/adapters/transforms/convert_dtype.py:35

  • Using 'copy=False' in astype may lead to in-place modifications; verify that this behavior is intentional and does not cause unintended side effects.
return data.astype(self.to_dtype, copy=False)

@LarsKue
Copy link
Contributor Author

LarsKue commented Apr 17, 2025

Tests are passing, I would say this is ready to merge! 🚀

@LarsKue LarsKue merged commit 147dd2d into dev Apr 17, 2025
14 of 15 checks passed
@LarsKue LarsKue deleted the allow-networks branch April 17, 2025 15:37
@github-project-automation github-project-automation bot moved this from Future to Done in bayesflow development Apr 17, 2025
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
feature New feature or request refactoring Some code shall be redesigned user interface Changes to the user interface and improvements in usability
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

2 participants