Skip to content

Commit

Permalink
Make one hot encoder serialisable (#880)
Browse files Browse the repository at this point in the history
  • Loading branch information
uri-granta authored Oct 11, 2024
1 parent 1a2e0ac commit dbb7d46
Show file tree
Hide file tree
Showing 5 changed files with 9 additions and 14 deletions.
4 changes: 2 additions & 2 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ authors:
- family-names: "Picheny"
given-names: "Victor"
title: "Trieste"
version: 4.2.0
date-released: 2024-09-20
version: 4.2.1
date-released: 2024-10-11
url: "https://github.com/secondmind-labs/trieste"
4 changes: 2 additions & 2 deletions tests/unit/test_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -1830,7 +1830,7 @@ def test_categorical_search_space_one_hot_encoding(
pytest.param(
CategoricalSearchSpace(["Y", "N"]),
tf.constant([[0], [2], [1]]),
ValueError,
InvalidArgumentError,
id="Out of range binary input value",
),
pytest.param(
Expand All @@ -1842,7 +1842,7 @@ def test_categorical_search_space_one_hot_encoding(
pytest.param(
CategoricalSearchSpace([["R", "G", "B"], ["Y", "N"]]),
tf.constant([[0], [1], [1]]),
ValueError,
InvalidArgumentError,
id="Wrong input shape",
),
],
Expand Down
2 changes: 1 addition & 1 deletion trieste/VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.2.0
4.2.1
9 changes: 2 additions & 7 deletions trieste/space.py
Original file line number Diff line number Diff line change
Expand Up @@ -656,17 +656,12 @@ def one_hot_encoder(self) -> EncoderFunction:

def binary_encoder(x: TensorType) -> TensorType:
# no need to one-hot encode binary categories (but we should still validate)
if tf.reduce_any((x != 0) & (x != 1)):
raise ValueError(f"Invalid values {tf.boolean_mask(x, ((x != 0) & (x != 1)))}")
tf.debugging.Assert(tf.reduce_all((x == 0) | (x == 1)), [tf.constant([])])
return x

def encoder(x: TensorType) -> TensorType:
flat_x, unflatten = flatten_leading_dims(x)
if flat_x.shape[-1] != len(self.tags):
raise ValueError(
"Invalid input for one-hot encoding: "
f"expected {len(self.tags)} tags, got {flat_x.shape[-1]}"
)
tf.debugging.assert_equal(flat_x.shape[-1], len(self.tags))
columns = tf.split(flat_x, flat_x.shape[-1], axis=1)
encoders = [
(
Expand Down
4 changes: 2 additions & 2 deletions versions.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
"url": "https://secondmind-labs.github.io/trieste/develop/"
},
{
"version": "4.2.0",
"url": "https://secondmind-labs.github.io/trieste/4.2.0/"
"version": "4.2.1",
"url": "https://secondmind-labs.github.io/trieste/4.2.1/"
},
{
"version": "4.1.0",
Expand Down

0 comments on commit dbb7d46

Please sign in to comment.