Skip to content

Add projectors to DeepSet #453

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 5 commits into from
May 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions bayesflow/networks/deep_set/deep_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def __init__(
mlp_widths_invariant_inner: Sequence[int] = (64, 64),
mlp_widths_invariant_outer: Sequence[int] = (64, 64),
mlp_widths_invariant_last: Sequence[int] = (64, 64),
activation: str = "gelu",
activation: str = "silu",
kernel_initializer: str = "he_normal",
dropout: int | float | None = 0.05,
spectral_normalization: bool = False,
Expand Down Expand Up @@ -72,7 +72,7 @@ def __init__(
mlp_widths_invariant_last : Sequence[int], optional
Widths of the MLP layers in the final invariant transformation. Default is (64, 64).
activation : str, optional
Activation function used throughout the network, such as "gelu". Default is "gelu".
Activation function used throughout the network, such as "gelu". Default is "silu".
kernel_initializer : str, optional
Initialization strategy for kernel weights, such as "he_normal". Default is "he_normal".
dropout : int, float, or None, optional
Expand Down
6 changes: 5 additions & 1 deletion bayesflow/networks/deep_set/equivariant_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ def __init__(
kernel_initializer=kernel_initializer,
spectral_normalization=spectral_normalization,
)
self.out_fc_projector = keras.layers.Dense(mlp_widths_equivariant[-1], kernel_initializer=kernel_initializer)

self.layer_norm = layers.LayerNormalization() if layer_norm else None

Expand Down Expand Up @@ -137,7 +138,10 @@ def call(self, input_set: Tensor, training: bool = False, **kwargs) -> Tensor:
output_set = ops.concatenate([input_set, invariant_summary], axis=-1)

# Pass through final equivariant transform + residual
output_set = input_set + self.equivariant_fc(output_set, training=training)
out_fc = self.equivariant_fc(output_set, training=training)
out_projected = self.out_fc_projector(out_fc)
output_set = input_set + out_projected

if self.layer_norm is not None:
output_set = self.layer_norm(output_set, training=training)

Expand Down
4 changes: 4 additions & 0 deletions bayesflow/networks/deep_set/invariant_layer.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ def __init__(
kernel_initializer=kernel_initializer,
spectral_normalization=spectral_normalization,
)
self.inner_projector = keras.layers.Dense(mlp_widths_inner[-1], kernel_initializer=kernel_initializer)

self.outer_fc = MLP(
mlp_widths_outer,
Expand All @@ -82,6 +83,7 @@ def __init__(
kernel_initializer=kernel_initializer,
spectral_normalization=spectral_normalization,
)
self.outer_projector = keras.layers.Dense(mlp_widths_outer[-1], kernel_initializer=kernel_initializer)

# Pooling function as keras layer for sum decomposition: inner( pooling( inner(set) ) )
if pooling_kwargs is None:
Expand All @@ -106,8 +108,10 @@ def call(self, input_set: Tensor, training: bool = False, **kwargs) -> Tensor:
"""

set_summary = self.inner_fc(input_set, training=training)
set_summary = self.inner_projector(set_summary)
set_summary = self.pooling_layer(set_summary, training=training)
set_summary = self.outer_fc(set_summary, training=training)
set_summary = self.outer_projector(set_summary)
return set_summary

@sanitize_input_shape
Expand Down