Skip to content

Commit

Permalink
Merge pull request #659 from pfnet/add-validation-to-trial-report
Browse files Browse the repository at this point in the history
Improve handling of reported values.
  • Loading branch information
hvy committed Nov 6, 2019
2 parents 0d9d592 + 48557e1 commit 6bc98cf
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 0 deletions.
8 changes: 8 additions & 0 deletions optuna/trial.py
Expand Up @@ -375,6 +375,14 @@ def report(self, value, step=None):
Step of the trial (e.g., Epoch of neural network training).
"""

try:
# For convenience, we allow users to report a value that can be cast to `float`.
value = float(value)
except (TypeError, ValueError):
message = 'The `value` argument is of type \'{}\' but supposed to be a float.'.format(
type(value).__name__)
raise TypeError(message)

self.storage.set_trial_value(self._trial_id, value)
if step is not None:
self.storage.set_trial_intermediate_value(self._trial_id, step, value)
Expand Down
26 changes: 26 additions & 0 deletions tests/test_trial.py
@@ -1,6 +1,7 @@
import math
from mock import Mock
from mock import patch
import numpy as np
import pytest

from optuna import distributions
Expand Down Expand Up @@ -399,3 +400,28 @@ def objective(trial):
study.optimize(objective, n_trials=1)

assert study.trials[0].datetime_start == trial_datetime_start[0]


def test_trial_report():
# type: () -> None

study = create_study()
trial = Trial(study, study._storage.create_new_trial(study.study_id))

# Report values that can be cast to `float` (OK).
trial.report(1.23)
trial.report(float('nan'))
trial.report('1.23') # type: ignore
trial.report('inf') # type: ignore
trial.report(1)
trial.report(np.array([1], dtype=np.float32)[0])

# Report values that cannot be cast to `float` (Error).
with pytest.raises(TypeError):
trial.report(None) # type: ignore

with pytest.raises(TypeError):
trial.report('foo') # type: ignore

with pytest.raises(TypeError):
trial.report([1, 2, 3]) # type: ignore

0 comments on commit 6bc98cf

Please sign in to comment.