-
Notifications
You must be signed in to change notification settings - Fork 72
Add a custom Sequential
network to avoid issues with building and serialization in keras
#493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
dd1ca0c
add custom sequential to fix #491
LarsKue d086c05
revert using Sequential in classifier_two_sample_test.py
LarsKue 114b6cc
Add docstring to custom Sequential
LarsKue 86ca493
fix copilot docstring
LarsKue 7a8e030
remove mlp override methods
LarsKue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
from .sequential import Sequential |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from collections.abc import Sequence | ||
import keras | ||
|
||
from bayesflow.utils import layer_kwargs | ||
from bayesflow.utils.serialization import deserialize, serializable, serialize | ||
|
||
|
||
@serializable("bayesflow.networks") | ||
class Sequential(keras.Layer): | ||
""" | ||
A custom sequential model for managing a sequence of Keras layers. | ||
|
||
This class extends `keras.Layer` and provides functionality for building, | ||
calling, and serializing a sequence of layers. Unlike `keras.Sequential`, | ||
this implementation does not eagerly check input shapes, meaning it is | ||
compatible with both single inputs and sets. | ||
|
||
Parameters | ||
---------- | ||
layers : keras.layer | Sequence[keras.layer] | ||
A sequence of Keras layers to be managed by this model. | ||
Can be passed by unpacking or as a single sequence. | ||
**kwargs : | ||
Additional keyword arguments passed to the base `keras.Layer` class. | ||
|
||
Notes | ||
----- | ||
- This class differs from `keras.Sequential` in that it does not eagerly check | ||
input shapes. This means that it is compatible with both single inputs | ||
and sets. | ||
""" | ||
|
||
def __init__(self, *layers: keras.Layer | Sequence[keras.Layer], **kwargs): | ||
super().__init__(**layer_kwargs(kwargs)) | ||
if len(layers) == 1 and isinstance(layers[0], Sequence): | ||
layers = layers[0] | ||
|
||
self._layers = layers | ||
|
||
def build(self, input_shape): | ||
if self.built: | ||
# building when the network is already built can cause issues with serialization | ||
# see https://github.com/keras-team/keras/issues/21147 | ||
return | ||
|
||
for layer in self._layers: | ||
layer.build(input_shape) | ||
input_shape = layer.compute_output_shape(input_shape) | ||
|
||
def call(self, inputs, training=None, mask=None): | ||
x = inputs | ||
for layer in self._layers: | ||
kwargs = self._make_kwargs_for_layer(layer, training, mask) | ||
x = layer(x, **kwargs) | ||
return x | ||
|
||
def compute_output_shape(self, input_shape): | ||
for layer in self._layers: | ||
input_shape = layer.compute_output_shape(input_shape) | ||
|
||
return input_shape | ||
|
||
def get_config(self): | ||
base_config = super().get_config() | ||
base_config = layer_kwargs(base_config) | ||
|
||
config = { | ||
"layers": [serialize(layer) for layer in self._layers], | ||
} | ||
|
||
return base_config | config | ||
|
||
@classmethod | ||
def from_config(cls, config, custom_objects=None): | ||
return cls(**deserialize(config, custom_objects=custom_objects)) | ||
|
||
@property | ||
def layers(self): | ||
return self._layers | ||
|
||
@staticmethod | ||
def _make_kwargs_for_layer(layer, training, mask): | ||
kwargs = {} | ||
if layer._call_has_mask_arg: | ||
kwargs["mask"] = mask | ||
if layer._call_has_training_arg and training is not None: | ||
kwargs["training"] = training | ||
return kwargs |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.