Skip to content

Commit

Permalink
Use different seeds for subspace sampling (#885)
Browse files Browse the repository at this point in the history
Co-authored-by: Uri Granta <[email protected]>
  • Loading branch information
uri-granta and Uri Granta authored Nov 5, 2024
1 parent d6f70ca commit beedf93
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
12 changes: 12 additions & 0 deletions tests/unit/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,6 +973,18 @@ def test_collection_space_sampling_raises_for_invalid_sample_size(
collection_space.sample(num_samples)


@pytest.mark.parametrize("search_space_type", [TaggedMultiSearchSpace, TaggedProductSearchSpace])
def test_collection_space_sampling_uses_different_seeds_for_subspaces(
search_space_type: Type[CollectionSearchSpace],
) -> None:
box = Box([0], [1])
collection_space = search_space_type(spaces=[box, box])
samples = collection_space.sample(5, seed=42)
# check that all the points are unique despite the seed
flattened = samples.numpy().flatten()
assert len(flattened) == len(set(flattened))


@pytest.mark.parametrize(
"search_space_type, space_A",
[
Expand Down
6 changes: 5 additions & 1 deletion trieste/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,7 +1229,11 @@ def subspace_sample(self, num_samples: int, seed: Optional[int] = None) -> Seque
tf.debugging.assert_non_negative(num_samples)
if seed is not None: # ensure reproducibility
tf.random.set_seed(seed)
return [self._spaces[tag].sample(num_samples, seed=seed) for tag in self._tags]
return [
# ensure subspaces (which may be identical) don't all use the same seed
self._spaces[tag].sample(num_samples, seed=None if seed is None else seed + i)
for i, tag in enumerate(self._tags)
]

def __eq__(self, other: object) -> bool:
"""
Expand Down

0 comments on commit beedf93

Please sign in to comment.