Skip to content

Commit

Permalink
fix(test): random test was failing. Fix better seed.
Browse files Browse the repository at this point in the history
The power iteration test for Dense was sometimes failing due to a random
choice of kernel and initialization vector u. Even if np.random.seed(42)
was set at the beginning of the file.

To ensure a fully deterministic test (deterministic kernel and
initialization u), a random generator np.random.default_rng(42) was created
and used to create both kernel and u.
The results for Dense test are then deterministic, either for a single test
file or for the whole unittest. Note that this is the only test modified;
other tests of the file pass, they have not been changed with deterministic
initializations.
  • Loading branch information
cofri committed Jul 26, 2023
1 parent a3e61fe commit e64d28b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions tests/test_normalizers.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
)
from deel.lip.utils import _padding_circular

np.random.seed(42)
rng = np.random.default_rng(42)


class TestSpectralNorm(unittest.TestCase):
Expand All @@ -24,11 +24,11 @@ class TestSpectralNorm(unittest.TestCase):
def test_spectral_normalization(self):
# Dense kernel
kernel_shape = (15, 32)
kernel = np.random.normal(size=kernel_shape).astype("float32")
kernel = rng.normal(size=kernel_shape).astype("float32")
self._test_kernel(kernel)
# Dense kernel projection
kernel_shape = (32, 15)
kernel = np.random.normal(size=kernel_shape).astype("float32")
kernel = rng.normal(size=kernel_shape).astype("float32")
self._test_kernel(kernel)

def _test_kernel(self, kernel):
Expand All @@ -40,7 +40,8 @@ def _test_kernel(self, kernel):
).numpy()
SVmax = np.max(sigmas_svd)

W_bar, _u, sigma = spectral_normalization(kernel, u=None, eps=1e-6)
u = rng.normal(size=(1, kernel.shape[-1]))
W_bar, _u, sigma = spectral_normalization(kernel, u=u, eps=1e-6)
# Test sigma is close to the one computed with svd first run @ 1e-1
np.testing.assert_approx_equal(
sigma, SVmax, 1, "test failed with kernel_shape " + str(kernel.shape)
Expand Down

0 comments on commit e64d28b

Please sign in to comment.