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

Replace integer division by floating division in Float hyperparameter #1002

Open
wants to merge 3 commits into
base: master
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
31 changes: 31 additions & 0 deletions keras_tuner/engine/hyperparameters/hp_types/float_hp_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,16 @@ def test_float_log_with_step():
assert rg.prob_to_value(0.3) == 0.1


def test_float_log_with_float_step():
rg = hp_module.Float(
"rg", min_value=0.1, max_value=0.19487171, step=1.1, sampling="log"
)
assert np.allclose(
list(rg.values),
[0.1, 0.11, 0.121, 0.1331, 0.14641, 0.161051, 0.1771561, 0.19487171],
)


def test_float_reverse_log_with_step():
rg = hp_module.Float(
"rg", min_value=0.01, max_value=100, step=10, sampling="reverse_log"
Expand All @@ -77,6 +87,17 @@ def test_float_reverse_log_with_step():
assert abs(rg.prob_to_value(0.3) - 99.91) < 1e-4


def test_float_reverse_log_with_float_step():
rg = hp_module.Float(
"rg", min_value=0.1, max_value=0.2, step=1.1, sampling="reverse_log"
)

assert np.allclose(
list(rg.values),
[0.2, 0.19, 0.179, 0.1669, 0.15359, 0.138949, 0.1228439, 0.10512829],
)


def test_sampling_zero_length_intervals():
f = hp_module.Float("f", 2, 2)
rand_sample = f.random_sample()
Expand Down Expand Up @@ -170,7 +191,17 @@ def test_float_values_property_with_step():
]


def test_float_values_property_with_float_step():
values = list(hp_module.Float("float", 0, 1, 0.1).values)
assert len(values) == 11
assert np.allclose(
values, [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]
)
assert isinstance(values[0], float)


def test_float_values_property_without_step():
assert len(list(hp_module.Float("float", 0, 1).values)) == 10
assert len(list(hp_module.Float("float", 2, 4).values)) == 10
assert len(list(hp_module.Float("float", 2, 20).values)) == 10
assert (
Expand Down
2 changes: 1 addition & 1 deletion keras_tuner/engine/hyperparameters/hp_types/numerical.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ def _get_n_values(self):
"""Get the total number of possible values using step."""
if self.sampling == "linear":
# +1 so that max_value may be sampled.
return int((self.max_value - self.min_value) // self.step + 1)
return int((self.max_value - self.min_value) / self.step + 1)
# For log and reverse_log
# +1 so that max_value may be sampled.
return (
Expand Down
Loading