|
| 1 | +from collections.abc import Mapping |
| 2 | +from ..summary_network import SummaryNetwork |
| 3 | +from bayesflow.utils.serialization import deserialize, serializable, serialize |
| 4 | +from bayesflow.types import Tensor, Shape |
| 5 | +import keras |
| 6 | +from keras import ops |
| 7 | + |
| 8 | + |
| 9 | +@serializable("bayesflow.networks") |
| 10 | +class FusionNetwork(SummaryNetwork): |
| 11 | + def __init__( |
| 12 | + self, |
| 13 | + backbones: Mapping[str, keras.Layer], |
| 14 | + head: keras.Layer | None = None, |
| 15 | + **kwargs, |
| 16 | + ): |
| 17 | + """(SN) Wraps multiple summary networks (`backbones`) to learn summary statistics from multi-modal data. |
| 18 | +
|
| 19 | + Networks and inputs are passed as dictionaries with corresponding keys, so that each input is processed |
| 20 | + by the correct summary network. This means the "summary_variables" entry to the approximator has to be |
| 21 | + a dictionary, which can be achieved using the :py:meth:`bayesflow.adapters.Adapter.group` method. |
| 22 | +
|
| 23 | + This network implements _late_ fusion. The output of the individual summary networks is concatenated, and |
| 24 | + can be further processed by another neural network (`head`). |
| 25 | +
|
| 26 | + Parameters |
| 27 | + ---------- |
| 28 | + backbones : dict |
| 29 | + A dictionary with names of inputs as keys and corresponding summary networks as values. |
| 30 | + head : keras.Layer, optional |
| 31 | + A network to further process the concatenated outputs of the summary networks. By default, |
| 32 | + the concatenated outputs are returned without further processing. |
| 33 | + **kwargs |
| 34 | + Additional keyword arguments that are passed to the :py:class:`~bayesflow.networks.SummaryNetwork` |
| 35 | + base class. |
| 36 | + """ |
| 37 | + super().__init__(**kwargs) |
| 38 | + self.backbones = backbones |
| 39 | + self.head = head |
| 40 | + self._ordered_keys = sorted(list(self.backbones.keys())) |
| 41 | + |
| 42 | + def build(self, inputs_shape: Mapping[str, Shape]): |
| 43 | + if self.built: |
| 44 | + return |
| 45 | + output_shapes = [] |
| 46 | + for k, shape in inputs_shape.items(): |
| 47 | + if not self.backbones[k].built: |
| 48 | + self.backbones[k].build(shape) |
| 49 | + output_shapes.append(self.backbones[k].compute_output_shape(shape)) |
| 50 | + if self.head and not self.head.built: |
| 51 | + fusion_input_shape = (*output_shapes[0][:-1], sum(shape[-1] for shape in output_shapes)) |
| 52 | + self.head.build(fusion_input_shape) |
| 53 | + self.built = True |
| 54 | + |
| 55 | + def compute_output_shape(self, inputs_shape: Mapping[str, Shape]): |
| 56 | + output_shapes = [] |
| 57 | + for k, shape in inputs_shape.items(): |
| 58 | + output_shapes.append(self.backbones[k].compute_output_shape(shape)) |
| 59 | + output_shape = (*output_shapes[0][:-1], sum(shape[-1] for shape in output_shapes)) |
| 60 | + if self.head: |
| 61 | + output_shape = self.head.compute_output_shape(output_shape) |
| 62 | + return output_shape |
| 63 | + |
| 64 | + def call(self, inputs: Mapping[str, Tensor], training=False): |
| 65 | + """ |
| 66 | + Parameters |
| 67 | + ---------- |
| 68 | + inputs : dict[str, Tensor] |
| 69 | + Each value in the dictionary is the input to the summary network with the corresponding key. |
| 70 | + training : bool, optional |
| 71 | + Whether the model is in training mode, affecting layers like dropout and |
| 72 | + batch normalization. Default is False. |
| 73 | + """ |
| 74 | + outputs = [self.backbones[k](inputs[k], training=training) for k in self._ordered_keys] |
| 75 | + outputs = ops.concatenate(outputs, axis=-1) |
| 76 | + if self.head is None: |
| 77 | + return outputs |
| 78 | + return self.head(outputs, training=training) |
| 79 | + |
| 80 | + def compute_metrics(self, inputs: Mapping[str, Tensor], stage: str = "training", **kwargs) -> dict[str, Tensor]: |
| 81 | + """ |
| 82 | + Parameters |
| 83 | + ---------- |
| 84 | + inputs : dict[str, Tensor] |
| 85 | + Each value in the dictionary is the input to the summary network with the corresponding key. |
| 86 | + stage : bool, optional |
| 87 | + Whether the model is in training mode, affecting layers like dropout and |
| 88 | + batch normalization. Default is False. |
| 89 | + **kwargs |
| 90 | + Additional keyword arguments. |
| 91 | + """ |
| 92 | + metrics = {"loss": [], "outputs": []} |
| 93 | + |
| 94 | + for k in self._ordered_keys: |
| 95 | + if isinstance(self.backbones[k], SummaryNetwork): |
| 96 | + metrics_k = self.backbones[k].compute_metrics(inputs[k], stage=stage, **kwargs) |
| 97 | + metrics["outputs"].append(metrics_k["outputs"]) |
| 98 | + if "loss" in metrics_k: |
| 99 | + metrics["loss"].append(metrics_k["loss"]) |
| 100 | + else: |
| 101 | + metrics["outputs"].append(self.backbones[k](inputs[k], training=stage == "training")) |
| 102 | + if len(metrics["loss"]) == 0: |
| 103 | + del metrics["loss"] |
| 104 | + else: |
| 105 | + metrics["loss"] = ops.sum(metrics["loss"]) |
| 106 | + metrics["outputs"] = ops.concatenate(metrics["outputs"], axis=-1) |
| 107 | + if self.head is not None: |
| 108 | + metrics["outputs"] = self.head(metrics["outputs"], training=stage == "training") |
| 109 | + |
| 110 | + return metrics |
| 111 | + |
| 112 | + def get_config(self) -> dict: |
| 113 | + base_config = super().get_config() |
| 114 | + config = { |
| 115 | + "backbones": self.backbones, |
| 116 | + "head": self.head, |
| 117 | + } |
| 118 | + return base_config | serialize(config) |
| 119 | + |
| 120 | + @classmethod |
| 121 | + def from_config(cls, config: dict, custom_objects=None): |
| 122 | + config = deserialize(config, custom_objects=custom_objects) |
| 123 | + return cls(**config) |
0 commit comments