|
| 1 | +import pytest |
| 2 | +import keras |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +from bayesflow.networks.coupling_flow.permutations import ( |
| 6 | + FixedPermutation, |
| 7 | + OrthogonalPermutation, |
| 8 | + RandomPermutation, |
| 9 | + Swap, |
| 10 | +) |
| 11 | + |
| 12 | + |
| 13 | +@pytest.fixture(params=[FixedPermutation, OrthogonalPermutation, RandomPermutation, Swap]) |
| 14 | +def permutation_class(request): |
| 15 | + return request.param |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture |
| 19 | +def input_tensor(): |
| 20 | + return keras.random.normal((2, 5)) |
| 21 | + |
| 22 | + |
| 23 | +def test_fixed_permutation_build_and_call(): |
| 24 | + # Since FixedPermutation is abstract, create a subclass for testing build. |
| 25 | + class TestPerm(FixedPermutation): |
| 26 | + def build(self, xz_shape, **kwargs): |
| 27 | + length = xz_shape[-1] |
| 28 | + self.forward_indices = keras.ops.arange(length - 1, -1, -1) |
| 29 | + self.inverse_indices = keras.ops.arange(length - 1, -1, -1) |
| 30 | + |
| 31 | + layer = TestPerm() |
| 32 | + input_shape = (2, 4) |
| 33 | + layer.build(input_shape) |
| 34 | + |
| 35 | + x = keras.ops.convert_to_tensor(np.arange(8).reshape(input_shape).astype("float32")) |
| 36 | + z, log_det = layer(x, inverse=False) |
| 37 | + x_inv, log_det_inv = layer(z, inverse=True) |
| 38 | + |
| 39 | + # Check shape preservation |
| 40 | + assert z.shape == x.shape |
| 41 | + assert x_inv.shape == x.shape |
| 42 | + # Forward then inverse recovers input |
| 43 | + np.testing.assert_allclose(keras.ops.convert_to_numpy(x_inv), keras.ops.convert_to_numpy(x), atol=1e-5) |
| 44 | + # log_det values should be zero tensors with the correct shape |
| 45 | + assert tuple(log_det.shape) == input_shape[:-1] |
| 46 | + assert tuple(log_det_inv.shape) == input_shape[:-1] |
| 47 | + |
| 48 | + |
| 49 | +def test_orthogonal_permutation_build_and_call(input_tensor): |
| 50 | + layer = OrthogonalPermutation() |
| 51 | + input_shape = keras.ops.shape(input_tensor) |
| 52 | + layer.build(input_shape) |
| 53 | + |
| 54 | + z, log_det = layer(input_tensor) |
| 55 | + x_inv, log_det_inv = layer(z, inverse=True) |
| 56 | + |
| 57 | + # Check output shapes |
| 58 | + assert z.shape == input_tensor.shape |
| 59 | + assert x_inv.shape == input_tensor.shape |
| 60 | + |
| 61 | + # Forward + inverse should approximately recover input (allow some numeric tolerance) |
| 62 | + np.testing.assert_allclose( |
| 63 | + keras.ops.convert_to_numpy(x_inv), keras.ops.convert_to_numpy(input_tensor), rtol=1e-5, atol=1e-5 |
| 64 | + ) |
| 65 | + |
| 66 | + # log_det should be scalar or batched scalar |
| 67 | + if len(log_det.shape) > 0: |
| 68 | + assert log_det.shape[0] == input_tensor.shape[0] # batch dim |
| 69 | + else: |
| 70 | + assert log_det.shape == () |
| 71 | + |
| 72 | + # log_det_inv should be negative of log_det (det(inv) = 1/det) |
| 73 | + log_det_np = keras.ops.convert_to_numpy(log_det) |
| 74 | + log_det_inv_np = keras.ops.convert_to_numpy(log_det_inv) |
| 75 | + np.testing.assert_allclose(log_det_inv_np, -log_det_np, rtol=1e-5, atol=1e-5) |
| 76 | + |
| 77 | + |
| 78 | +def test_random_permutation_build_and_call(input_tensor): |
| 79 | + layer = RandomPermutation() |
| 80 | + input_shape = keras.ops.shape(input_tensor) |
| 81 | + layer.build(input_shape) |
| 82 | + |
| 83 | + # Assert forward_indices and inverse_indices are set and consistent |
| 84 | + fwd = keras.ops.convert_to_numpy(layer.forward_indices) |
| 85 | + inv = keras.ops.convert_to_numpy(layer.inverse_indices) |
| 86 | + # Applying inv on fwd must yield ordered indices |
| 87 | + reordered = fwd[inv] |
| 88 | + np.testing.assert_array_equal(np.arange(len(fwd)), reordered) |
| 89 | + |
| 90 | + z, log_det = layer(input_tensor) |
| 91 | + x_inv, log_det_inv = layer(z, inverse=True) |
| 92 | + |
| 93 | + assert z.shape == input_tensor.shape |
| 94 | + assert x_inv.shape == input_tensor.shape |
| 95 | + np.testing.assert_allclose(keras.ops.convert_to_numpy(x_inv), keras.ops.convert_to_numpy(input_tensor), atol=1e-5) |
| 96 | + assert tuple(log_det.shape) == input_shape[:-1] |
| 97 | + assert tuple(log_det_inv.shape) == input_shape[:-1] |
| 98 | + |
| 99 | + |
| 100 | +def test_swap_build_and_call(input_tensor): |
| 101 | + layer = Swap() |
| 102 | + input_shape = keras.ops.shape(input_tensor) |
| 103 | + layer.build(input_shape) |
| 104 | + |
| 105 | + fwd = keras.ops.convert_to_numpy(layer.forward_indices) |
| 106 | + inv = keras.ops.convert_to_numpy(layer.inverse_indices) |
| 107 | + reordered = fwd[inv] |
| 108 | + np.testing.assert_array_equal(np.arange(len(fwd)), reordered) |
| 109 | + |
| 110 | + z, log_det = layer(input_tensor) |
| 111 | + x_inv, log_det_inv = layer(z, inverse=True) |
| 112 | + |
| 113 | + assert z.shape == input_tensor.shape |
| 114 | + assert x_inv.shape == input_tensor.shape |
| 115 | + np.testing.assert_allclose(keras.ops.convert_to_numpy(x_inv), keras.ops.convert_to_numpy(input_tensor), atol=1e-5) |
| 116 | + assert tuple(log_det.shape) == input_shape[:-1] |
| 117 | + assert tuple(log_det_inv.shape) == input_shape[:-1] |
0 commit comments