diff --git a/src/evaluate/evaluator/base.py b/src/evaluate/evaluator/base.py index 09de31f19..a10b6a38c 100644 --- a/src/evaluate/evaluator/base.py +++ b/src/evaluate/evaluator/base.py @@ -87,8 +87,8 @@ integer will run the model on the associated CUDA device ID. If `None` is provided it will be inferred and CUDA:0 used if available, CPU otherwise. random_state (`int`, *optional*, defaults to `None`): - The `random_state` value passed to `bootstrap` if `"bootstrap"` strategy is chosen. Useful for - debugging. + The `random_state` value passed to `bootstrap` if `"bootstrap"` strategy is chosen. If `None`, a fixed + seed of 0 is used, so the same inputs give the same confidence interval on every run. """ EVALUATOR_COMPUTE_RETURN_DOCSTRING = r""" @@ -134,6 +134,9 @@ def _compute_confidence_interval( A utility function enabling the confidence interval calculation for metrics computed by the evaluator based on `scipy`'s `bootstrap` method. """ + if random_state is None: + # Seed by default, so the same predictions give the same interval on every run. + random_state = 0 # bootstrap only works with functions that use args and no kwargs def build_args_metric(metric, key, **kwargs): diff --git a/tests/test_evaluator.py b/tests/test_evaluator.py index 259b5c7b9..2c3141b84 100644 --- a/tests/test_evaluator.py +++ b/tests/test_evaluator.py @@ -363,6 +363,23 @@ def test_bootstrap(self): self.assertAlmostEqual(results["accuracy"]["confidence_interval"][1], 1.0, 5) self.assertAlmostEqual(results["accuracy"]["standard_error"], 0.22498, 5) + def test_bootstrap_is_reproducible_without_random_state(self): + data = Dataset.from_dict({"label": [1, 0, 0], "text": ["great movie", "great movie", "horrible movie"]}) + + def run(): + return self.evaluator.compute( + model_or_pipeline=self.pipe, + data=data, + metric="accuracy", + label_mapping=self.label_mapping, + strategy="bootstrap", + n_resamples=10, + )["accuracy"] + + first, second = run(), run() + self.assertEqual(first["confidence_interval"], second["confidence_interval"]) + self.assertEqual(first["standard_error"], second["standard_error"]) + def test_perf(self): results = self.evaluator.compute( model_or_pipeline=self.perf_pipe,