-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
backend-specific approximators template
- Loading branch information
Showing
7 changed files
with
75 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,2 @@ | ||
|
||
from .approximator import Approximator |
14 changes: 14 additions & 0 deletions
14
bayesflow/experimental/backend_approximators/approximator.py
This file contains 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,14 @@ | ||
|
||
import keras | ||
|
||
match keras.backend.backend(): | ||
case "jax": | ||
from .jax_approximator import JAXApproximator as Approximator | ||
case "numpy": | ||
from .numpy_approximator import NumpyApproximator as Approximator | ||
case "tensorflow": | ||
from .tensorflow_approximator import TensorFlowApproximator as Approximator | ||
case "torch": | ||
from .torch_approximator import TorchApproximator as Approximator | ||
case other: | ||
raise NotImplementedError(f"BayesFlow does not currently support backend '{other}'.") |
16 changes: 16 additions & 0 deletions
16
bayesflow/experimental/backend_approximators/base_approximator.py
This file contains 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,16 @@ | ||
|
||
import keras | ||
|
||
from bayesflow.experimental.types import Tensor | ||
|
||
|
||
class BaseApproximator(keras.Model): | ||
def train_step(self, data): | ||
raise NotImplementedError | ||
|
||
# noinspection PyMethodOverriding | ||
def compute_metrics(self, data: dict[str, Tensor], mode: str = "training") -> Tensor: | ||
raise NotImplementedError | ||
|
||
def compute_loss(self, *args, **kwargs): | ||
raise NotImplementedError(f"Use compute_metrics()['loss'] instead.") |
8 changes: 8 additions & 0 deletions
8
bayesflow/experimental/backend_approximators/jax_approximator.py
This file contains 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,8 @@ | ||
|
||
import jax | ||
|
||
from .base_approximator import BaseApproximator | ||
|
||
|
||
class JAXApproximator(BaseApproximator): | ||
pass |
8 changes: 8 additions & 0 deletions
8
bayesflow/experimental/backend_approximators/numpy_approximator.py
This file contains 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,8 @@ | ||
|
||
import numpy as np | ||
|
||
from .base_approximator import BaseApproximator | ||
|
||
|
||
class NumpyApproximator(BaseApproximator): | ||
pass |
8 changes: 8 additions & 0 deletions
8
bayesflow/experimental/backend_approximators/tensorflow_approximator.py
This file contains 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,8 @@ | ||
|
||
import tensorflow as tf | ||
|
||
from .base_approximator import BaseApproximator | ||
|
||
|
||
class TensorFlowApproximator(BaseApproximator): | ||
pass |
19 changes: 19 additions & 0 deletions
19
bayesflow/experimental/backend_approximators/torch_approximator.py
This file contains 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,19 @@ | ||
|
||
import torch | ||
|
||
|
||
from .base_approximator import BaseApproximator | ||
|
||
|
||
class TorchApproximator(BaseApproximator): | ||
def train_step(self, data): | ||
with torch.enable_grad(): | ||
metrics = self.compute_metrics(data, mode="training") | ||
|
||
loss = metrics.pop("loss") | ||
|
||
self.optimizer.zero_grad() | ||
loss.backward() | ||
self.optimizer.step() | ||
|
||
return metrics |