8000 Additional shape check for the features and labels for DefaultTrialConverter. by copybara-service[bot] · Pull Request #559 · google/vizier · GitHub
[go: up one dir, main page]

Skip to content
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
8000
29 changes: 26 additions & 3 deletions vizier/pyvizier/converters/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -895,17 +895,40 @@ def to_trials(
Args:
features: A dictionary where keys correspond to parameter names in the
returned Trial and values correspond to parameter values and have shape
(num_obs, ) or (num_obs, 1).
(num_obs, 1).
labels: A dictionary of labels where each key corresponds to a metric name
and its value is an array of shape (num_obs,) or (num_obs, 1) of oberved
metric values.
and its value is an array of shape (num_obs, 1) of observed metric
values.

Returns:
A list of pyvizier trials created with parameters corresponding to
`features` and final measurements corresponding to `labels`.
NOTE: All final measurements have steps=1. If 'labels' is not passed, the
output trials include `final_measurement=None` and `measurements=[]`.
"""
invalid_features = [
(k, v.shape)
for k, v in features.items()
if len(v.shape) != 2 or (len(v.shape) == 2 and v.shape[1] != 1)
]
if invalid_features:
raise ValueError(
'Features need to contain 2d arrays with shape (num_obs, 1). Invalid'
f' feature shapes: {invalid_features}'
)

if labels:
invalid_labels = [
(k, v.shape)
for k, v in labels.items()
if len(v.shape) != 2 or (len(v.shape) == 2 and v.shape[1] != 1)
]
if invalid_labels:
raise ValueError(
'Labels need to contain 2d arrays with shape (num_obs, 1). Invalid'
f' label shapes: {invalid_labels}'
)

if labels is None:
return [
pyvizier.Trial(parameters=p) for p in self.to_parameters(features)
Expand Down
57 changes: 57 additions & 0 deletions vizier/pyvizier/converters/core_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,63 @@ def test_infeasible_trials(self):
if np.isnan(label):
self.assertEmpty(t.final_measurement.metrics)

def test_parameters_and_labels_shape(self):
study_config = pyvizier.ProblemStatement()
root = study_config.search_space.root
root.add_float_param('x1', 0.0, 10.0)

study_config.metric_information.extend([
pyvizier.MetricInformation(
name='y1', goal=pyvizier.ObjectiveMetricGoal.MAXIMIZE
),
pyvizier.MetricInformation(
name='y2', goal=pyvizier.ObjectiveMetricGoal.MINIMIZE
),
])
converter = core.DefaultTrialConverter.from_study_config(study_config)
features_1d = {
'x1': np.array([1.0, 3.0]),
'x2': np.array([2.0, 4.0]),
}

with self.assertRaisesRegex(
ValueError,
'Features need to contain 2d arrays with shape (num_obs, 1)*',
):
converter.to_trials(features_1d)

features_right_shape = {
'x1': np.array([[1.0, 3.0]]).T,
'x2': np.array([[2.0, 4.0]]).T,
}
labels_1d = {
'y1': np.array([10.0, 40.0]),
'y2': np.array([20.0, 50.0]),
}
with self.assertRaisesRegex(
ValueError, 'Labels need to contain 2d arrays with shape (num_obs, 1)*'
):
converter.to_trials(features_right_shape, labels_1d)

features_2d_reverse_shape = {
'x1': np.array([[1.0, 3.0]]),
'x2': np.array([[2.0, 4.0]]),
}
with self.assertRaisesRegex(
ValueError,
'Features need to contain 2d arrays with shape (num_obs, 1)*',
):
converter.to_trials(features_2d_reverse_shape)

labels_2d_reverse_shape = {
'y1': np.array([[10.0, 40.0]]),
'y2': np.array([[20.0, 50.0]]),
}
with self.assertRaisesRegex(
ValueError, 'Labels need to contain 2d arrays with shape (num_obs, 1)*'
):
converter.to_trials(features_right_shape, labels_2d_reverse_shape)

def test_metrics(self):
converter = core.DefaultTrialConverter.from_study_configs(
[],
Expand Down
0