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

Use correct dtype for empty box samples #847

Merged
merged 1 commit into from
May 28, 2024
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
14 changes: 14 additions & 0 deletions tests/unit/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1577,3 +1577,17 @@ def test_nonlinear_constraints_multioutput_raises() -> None:
)
with pytest.raises(TF_DEBUGGING_ERROR_TYPES):
nlc.residual(points)


@pytest.mark.parametrize("dtype", [tf.float32, tf.float64])
def test_box_empty_sobol_sampling_returns_correct_dtype(dtype: tf.DType) -> None:
box = Box(tf.zeros((3,), dtype=dtype), tf.ones((3,), dtype=dtype))
sobol_samples = box.sample_sobol(0)
assert sobol_samples.dtype == dtype


@pytest.mark.parametrize("dtype", [tf.float32, tf.float64])
def test_box_empty_halton_sampling_returns_correct_dtype(dtype: tf.DType) -> None:
box = Box(tf.zeros((3,), dtype=dtype), tf.ones((3,), dtype=dtype))
sobol_samples = box.sample_halton(0)
assert sobol_samples.dtype == dtype
4 changes: 2 additions & 2 deletions trieste/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,7 +627,7 @@ def _sample_halton(
# Internal common method to sample from the space using a Halton sequence.
tf.debugging.assert_non_negative(num_samples)
if num_samples == 0:
return tf.constant([])
return tf.constant([], dtype=self._lower.dtype)
if seed is not None: # ensure reproducibility
tf.random.set_seed(seed)
dim = tf.shape(self._lower)[-1]
Expand Down Expand Up @@ -660,7 +660,7 @@ def sample_sobol(self, num_samples: int, skip: Optional[int] = None) -> TensorTy
"""
tf.debugging.assert_non_negative(num_samples)
if num_samples == 0:
return tf.constant([])
return tf.constant([], dtype=self._lower.dtype)
if skip is None: # generate random skip
skip = tf.random.uniform([1], maxval=2**16, dtype=tf.int32)[0]
dim = tf.shape(self._lower)[-1]
Expand Down
Loading