Repository: keras-team/keras-tuner

Issue:
Handling division by zero
https://github.com/keras-team/keras-tuner/blob/a17321428a55975f55acb5b9270d6d73ea11dcc9/kerastuner/engine/hyperparameters.py#L1064

Hey team,
There should be the handling of division by zero here.  

Discussion:
google-cla[bot]: All (the pull request submitter and all commit authors) CLAs are signed, **but** one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project.  Please have them confirm that by leaving a comment that contains only `@googlebot I consent.` in this pull request.

*Note to project maintainer:* There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent.  In those cases, you can manually confirm consent of the commit author(s), and set the `cla` label to `yes` (if enabled on your project).

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fkeras-team%2Fkeras-tuner%2Fpull%2F586) for more info**.

<!-- need_author_consent -->
google-cla[bot]: All (the pull request submitter and all commit authors) CLAs are signed, **but** one or more commits were authored or co-authored by someone other than the pull request submitter.

We need to confirm that all authors are ok with their commits being contributed to this project.  Please have them confirm that by leaving a comment that contains only `@googlebot I consent.` in this pull request.

*Note to project maintainer:* There may be cases where the author cannot leave a comment, or the comment is not properly detected as consent.  In those cases, you can manually confirm consent of the commit author(s), and set the `cla` label to `yes` (if enabled on your project).

ℹ️ **Googlers: [Go here](https://goto.google.com/prinfo/https%3A%2F%2Fgithub.com%2Fkeras-team%2Fkeras-tuner%2Fpull%2F586) for more info**.

<!-- need_author_consent -->
codecov-commenter: # [Codecov](https://codecov.io/gh/keras-team/keras-tuner/pull/586?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) Report
> Merging [#586](https://codecov.io/gh/keras-team/keras-tuner/pull/586?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) (d20c29d) into [master](https://codecov.io/gh/keras-team/keras-tuner/commit/bb27e674d99d5ec235c257f521fde7c35e51488a?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) (bb27e67) will **decrease** coverage by `0.20%`.
> The diff coverage is `100.00%`.

> :exclamation: Current head d20c29d differs from pull request most recent head 7ec2a67. Consider uploading reports for the commit 7ec2a67 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/keras-team/keras-tuner/pull/586/graphs/tree.svg?width=650&height=150&src=pr&token=XiR9QNcEiW&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team)](https://codecov.io/gh/keras-team/keras-tuner/pull/586?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team)

```diff
@@            Coverage Diff             @@
##           master     #586      +/-   ##
==========================================
- Coverage   92.10%   91.90%   -0.21%     
==========================================
  Files          33       33              
  Lines        2875     2877       +2     
==========================================
- Hits         2648     2644       -4     
- Misses        227      233       +6     
```


| [Impacted Files](https://codecov.io/gh/keras-team/keras-tuner/pull/586?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) | Coverage Δ | |
|---|---|---|
| [keras\_tuner/engine/hyperparameters.py](https://codecov.io/gh/keras-team/keras-tuner/pull/586/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team#diff-a2VyYXNfdHVuZXIvZW5naW5lL2h5cGVycGFyYW1ldGVycy5weQ==) | `91.68% <100.00%> (+0.03%)` | :arrow_up: |
| [keras\_tuner/applications/efficientnet.py](https://codecov.io/gh/keras-team/keras-tuner/pull/586/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team#diff-a2VyYXNfdHVuZXIvYXBwbGljYXRpb25zL2VmZmljaWVudG5ldC5weQ==) | `87.30% <0.00%> (-6.35%)` | :arrow_down: |
| [keras\_tuner/applications/augment.py](https://codecov.io/gh/keras-team/keras-tuner/pull/586/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team#diff-a2VyYXNfdHVuZXIvYXBwbGljYXRpb25zL2F1Z21lbnQucHk=) | `90.90% <0.00%> (-2.28%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/keras-team/keras-tuner/pull/586?src=pr&el=continue&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/keras-team/keras-tuner/pull/586?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team). Last update [bb27e67...7ec2a67](https://codecov.io/gh/keras-team/keras-tuner/pull/586?src=pr&el=lastupdated&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments?utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team).


Patch:
diff --git a/keras_tuner/engine/hyperparameters.py b/keras_tuner/engine/hyperparameters.py
index 646a180..32fd644 100644
--- a/keras_tuner/engine/hyperparameters.py
+++ b/keras_tuner/engine/hyperparameters.py
@@ -1164,6 +1164,8 @@ def value_to_cumulative_prob(value, hp):
         # Center the value in its probability bucket.
         return (index + 0.5) * ele_prob
     elif isinstance(hp, (Int, Float)):
+        if hp.max_value == hp.min_value:
+            return 1.0
         sampling = hp.sampling or "linear"
         if sampling == "linear":
             return (value - hp.min_value) / (hp.max_value - hp.min_value)


Test patch:
diff --git a/tests/keras_tuner/engine/hyperparameters_test.py b/tests/keras_tuner/engine/hyperparameters_test.py
index b0e62e4..a255b83 100644
--- a/tests/keras_tuner/engine/hyperparameters_test.py
+++ b/tests/keras_tuner/engine/hyperparameters_test.py
@@ -304,6 +304,16 @@ def test_sampling_arg():
         hp_module.Int("k", 1, 0, sampling="linear")
 
 
+def test_sampling_zero_length_intervals():
+    f = hp_module.Float("f", 2, 2)
+    rand_sample = f.random_sample()
+    assert rand_sample == 2
+
+    val = 2
+    prob = hp_module.value_to_cumulative_prob(val, f)
+    assert prob == 1
+
+
 def test_log_sampling_random_state():
     f = hp_module.Float("f", 1e-3, 1e3, sampling="log")
     rand_sample = f.random_sample()