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

Update convergence curve flipping logic. #652

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
38 changes: 22 additions & 16 deletions vizier/_src/benchmarks/analyzers/convergence_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,24 +167,25 @@ def extrapolate_ys(cls,
class ConvergenceCurveConverter:
"""Converter for Trial sequence to ConvergenceCurve."""

def __init__(self,
metric_information: pyvizier.MetricInformation,
*,
flip_signs: bool = False,
cost_fn: Callable[[pyvizier.Trial], Union[float,
int]] = lambda _: 1,
measurements_type: str = 'final'):
def __init__(
self,
metric_information: pyvizier.MetricInformation,
*,
flip_signs_for_min: bool = False,
cost_fn: Callable[[pyvizier.Trial], Union[float, int]] = lambda _: 1,
measurements_type: str = 'final',
):
"""Init.

Args:
metric_information: Information of relevant metric.
flip_signs: If True, flips the signs of metric values to always
maximize. Useful when desiring all increasing curves.
flip_signs_for_min: If True, flips the signs of metric values to always
maximize. Useful when desiring all increasing curves.
cost_fn: Cost of each Trial (to determine xs in ConvergenceCurve).
measurements_type: ['final', 'intermediate', 'all']
"""
self.metric_information = metric_information
self.flip_signs = flip_signs
self.flip_signs_for_min = flip_signs_for_min
self.cost_fn = cost_fn
self.measurements_type = measurements_type

Expand All @@ -211,16 +212,21 @@ def convert(self, trials: Sequence[pyvizier.Trial]) -> ConvergenceCurve:
yvals.append(comparator([yvalue, yvals[-1]]))

yvals = np.asarray(yvals[1:])
trend = ConvergenceCurve.YTrend.DECREASING
if (self.metric_information.goal == pyvizier.ObjectiveMetricGoal.MAXIMIZE
) or (self.metric_information.goal
== pyvizier.ObjectiveMetricGoal.MINIMIZE and self.flip_signs):
if self.metric_information.goal == pyvizier.ObjectiveMetricGoal.MAXIMIZE:
trend = ConvergenceCurve.YTrend.INCREASING
flipped = False
elif self.flip_signs_for_min:
trend = ConvergenceCurve.YTrend.INCREASING
flipped = True
else:
trend = ConvergenceCurve.YTrend.DECREASING
flipped = False
return ConvergenceCurve(
xs=np.asarray(xvals[1:]),
ys=np.asarray(yvals).reshape([1, -1]) * (-1 if self.flip_signs else 1),
ys=np.asarray(yvals).reshape([1, -1]) * (-1 if flipped else 1),
trend=trend,
ylabel=self.metric_information.name)
ylabel=self.metric_information.name,
)

@property
def comparator(self):
Expand Down
13 changes: 13 additions & 0 deletions vizier/_src/benchmarks/analyzers/convergence_curve_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ def test_convert_basic(self, goal, expected):
np.testing.assert_array_equal(curve.xs, [1, 2, 3])
np.testing.assert_array_equal(curve.ys, expected)

@parameterized.named_parameters(
('maximize', pyvizier.ObjectiveMetricGoal.MAXIMIZE, [[2, 2, 3]]),
('minimize', pyvizier.ObjectiveMetricGoal.MINIMIZE, [[-2, -1, -1]]),
)
def test_convert_flip_signs(self, goal, expected):
trials = _gen_trials([2, 1, 3])
generator = convergence.ConvergenceCurveConverter(
pyvizier.MetricInformation(name='', goal=goal), flip_signs_for_min=True
)
curve = generator.convert(trials)
np.testing.assert_array_equal(curve.xs, [1, 2, 3])
np.testing.assert_array_equal(curve.ys, expected)


class HyperConvergenceCurveConverterTest(parameterized.TestCase):

Expand Down
8 changes: 5 additions & 3 deletions vizier/_src/benchmarks/analyzers/state_analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,16 @@ class BenchmarkStateAnalyzer:
def to_curve(
cls,
states: List[benchmark_state.BenchmarkState],
flip_signs: bool = False,
flip_signs_for_min: bool = False,
) -> convergence_curve.ConvergenceCurve:
"""Generates a ConvergenceCurve from a batch of BenchmarkStates.

Each state in batch should represent the same study (different repeat).

Args:
states: List of BenchmarkStates.
flip_signs: If true, flip signs of curve when it is MINIMIZE metric.
flip_signs_for_min: If true, flip signs of curve when it is MINIMIZE
metric.

Returns:
Convergence curve with batch size equal to length of states.
Expand All @@ -53,7 +54,8 @@ def to_curve(
raise ValueError('Multiobjective Conversion not supported yet.')

converter = convergence_curve.ConvergenceCurveConverter(
problem_statement.metric_information.item(), flip_signs=flip_signs
problem_statement.metric_information.item(),
flip_signs_for_min=flip_signs_for_min,
)
curves = []
for state in states:
Expand Down