Skip to content
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

Two moons test fit #175

Merged
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
2 changes: 1 addition & 1 deletion bayesflow/networks/flow_matching/flow_matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,4 @@ def compute_metrics(self, data: dict[str, Tensor], stage: str = "training") -> d

loss = keras.losses.mean_squared_error(predicted_velocity, target_velocity)

return {"loss": loss}
return {"loss": loss}
11 changes: 10 additions & 1 deletion tests/test_two_moons/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,14 @@ def validation_dataset(simulator, batch_size):
from bayesflow import OfflineDataset

num_batches = 4
data = simulator.sample((4 * batch_size,))
data = simulator.sample((num_batches * batch_size,))
return OfflineDataset(data, workers=4, max_queue_size=num_batches, batch_size=batch_size)


@pytest.fixture()
def test_dataset(simulator, batch_size):
from bayesflow import OfflineDataset

num_batches = 16
data = simulator.sample((num_batches * batch_size,))
return OfflineDataset(data, workers=4, max_queue_size=num_batches, batch_size=batch_size)
38 changes: 31 additions & 7 deletions tests/test_two_moons/test_two_moons.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import keras
import pytest

from tests.utils import assert_models_equal
from tests.utils import assert_models_equal, max_mean_discrepancy
from tests.utils import InterruptFitCallback, FitInterruptedError


Expand All @@ -11,14 +11,38 @@ def test_compile(approximator, random_samples, jit_compile):


@pytest.mark.parametrize("jit_compile", [False, True])
def test_fit(approximator, train_dataset, validation_dataset, jit_compile):
# TODO: verify the model learns something by comparing a metric before and after training
approximator.compile(jit_compile=jit_compile)
approximator.fit(
def test_fit(approximator, train_dataset, validation_dataset, test_dataset, jit_compile):
# TODO: Refactor to use approximator.sample() when implemented (instead of calling the inference network directly)

approximator.compile(jit_compile=jit_compile, loss=keras.losses.KLDivergence())
inf_vars = approximator.configurator.configure_inference_variables(test_dataset.data)
inf_conds = approximator.configurator.configure_inference_conditions(test_dataset.data)
y = test_dataset.data["x"]
Copy link
Contributor

Choose a reason for hiding this comment

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

relies on the internal structure of the dataset fixture, which is not good. Use test_batch = test_dataset[0]; observables = test_batch["x"] instead.


pre_loss = approximator.compute_metrics(train_dataset.data)["loss"]
pre_val_loss = approximator.compute_metrics(validation_dataset.data)["loss"]
x_before = approximator.inference_network(inf_vars, conditions=inf_conds)
mmd_before = max_mean_discrepancy(x_before, y)
Copy link
Contributor

Choose a reason for hiding this comment

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

inconsistent naming


history = approximator.fit(
train_dataset,
validation_data=validation_dataset,
epochs=2,
)
epochs=3,
).history
x_after = approximator.inference_network(inf_vars, conditions=inf_conds)
mmd_after = max_mean_discrepancy(x_after, y)

# Test model weights have not vanished
for layer in approximator.layers:
for weight in layer.weights:
assert not keras.ops.any(keras.ops.isnan(weight)).numpy()
Copy link
Contributor

Choose a reason for hiding this comment

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

tensor.numpy() is not backend-agnostic and also not necessary here (when necessary, use keras.ops.convert_to_numpy instead)


# Test KLD loss and validation loss decrease after training
assert history["loss"][-1] < pre_loss
assert history["val_loss"][-1] < pre_val_loss

# Test MMD improved after training
assert mmd_after < mmd_before


@pytest.mark.parametrize("jit_compile", [False, True])
Expand Down
27 changes: 27 additions & 0 deletions tests/utils/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,30 @@ def isclose(x1, x2, rtol=1e-5, atol=1e-8):

def allclose(x1, x2, rtol=1e-5, atol=1e-8):
return keras.ops.all(isclose(x1, x2, rtol, atol))


def max_mean_discrepancy(x, y):
Copy link
Contributor

Choose a reason for hiding this comment

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

In the future, let's put these in bayesflow/metrics directly. This time, I will move this and make it a bit more customizable.

# Computes the Max Mean Discrepancy between samples of two distributions
xx = keras.ops.matmul(x, keras.ops.transpose(x))
yy = keras.ops.matmul(y, keras.ops.transpose(y))
zz = keras.ops.matmul(x, keras.ops.transpose(y))

rx = keras.ops.broadcast_to(keras.ops.expand_dims(keras.ops.diag(xx), 0), xx.shape)
ry = keras.ops.broadcast_to(keras.ops.expand_dims(keras.ops.diag(yy), 0), yy.shape)

dxx = keras.ops.transpose(rx) + rx - 2.0 * xx
dyy = keras.ops.transpose(ry) + ry - 2.0 * yy
dxy = keras.ops.transpose(rx) + ry - 2.0 * zz

XX = keras.ops.zeros(xx.shape)
YY = keras.ops.zeros(yy.shape)
XY = keras.ops.zeros(zz.shape)

# RBF scaling
bandwidth = [10, 15, 20, 50]
for a in bandwidth:
XX += keras.ops.exp(-0.5 * dxx / a)
YY += keras.ops.exp(-0.5 * dyy / a)
XY += keras.ops.exp(-0.5 * dxy / a)

return keras.ops.mean(XX + YY - 2.0 * XY)
Loading