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

Make one hot encoder serialisable #880

Merged
merged 2 commits into from
Oct 11, 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
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
Loading