Skip to content

Commit

Permalink
Use correct dtype for empty box samples (#847)
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 May 28, 2024
1 parent 391c1f0 commit fcf8835
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
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

0 comments on commit fcf8835

Please sign in to comment.