Repository: keras-team/keras-tuner

Issue:
If no objective provided, do not use the SaveBestEpoch callback
**Describe the bug**
<!---
A clear and concise description of what the bug is.
-->

**To Reproduce**
<!---
Provide a link to a [Colab Notebook](https://research.google.com/colaboratory/), which reproduces the bug.
-->

**Expected behavior**
<!---
A clear and concise description of what you expected to happen.
-->

**Additional context**
<!---
Add any other context about the problem here.
-->
The plan is to save every epoch if no objective specified.

**Would you like to help us fix it?**


Discussion:
codecov-commenter: # [Codecov](https://codecov.io/gh/keras-team/keras-tuner/pull/657?src=pr&el=h1&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) Report
> Merging [#657](https://codecov.io/gh/keras-team/keras-tuner/pull/657?src=pr&el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) (3583648) into [master](https://codecov.io/gh/keras-team/keras-tuner/commit/be682573c6f6be1e3f3e6dcac786a34ccac19d3b?el=desc&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team) (be68257) will **decrease** coverage by `0.06%`.
> The diff coverage is `33.33%`.

> :exclamation: Current head 3583648 differs from pull request most recent head 25b56be. Consider uploading reports for the commit 25b56be to get more accurate results

[![Impacted file tree graph](https://codecov.io/gh/keras-team/keras-tuner/pull/657/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/657?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     #657      +/-   ##
==========================================
- Coverage   92.42%   92.35%   -0.07%     
==========================================
  Files          36       36              
  Lines        2876     2879       +3     
==========================================
+ Hits         2658     2659       +1     
- Misses        218      220       +2     
```


| [Impacted Files](https://codecov.io/gh/keras-team/keras-tuner/pull/657?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/tuner\_utils.py](https://codecov.io/gh/keras-team/keras-tuner/pull/657/diff?src=pr&el=tree&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team#diff-a2VyYXNfdHVuZXIvZW5naW5lL3R1bmVyX3V0aWxzLnB5) | `85.63% <33.33%> (-0.85%)` | :arrow_down: |

------

[Continue to review full report at Codecov](https://codecov.io/gh/keras-team/keras-tuner/pull/657?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/657?src=pr&el=footer&utm_medium=referral&utm_source=github&utm_content=comment&utm_campaign=pr+comments&utm_term=keras-team). Last update [be68257...25b56be](https://codecov.io/gh/keras-team/keras-tuner/pull/657?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/tuner_utils.py b/keras_tuner/engine/tuner_utils.py
index 1a3ced3..98fa0ad 100644
--- a/keras_tuner/engine/tuner_utils.py
+++ b/keras_tuner/engine/tuner_utils.py
@@ -196,6 +196,10 @@ class SaveBestEpoch(keras.callbacks.Callback):
             self.best_value = float("inf")
 
     def on_epoch_end(self, epoch, logs=None):
+        if isinstance(self.objective, obj_module.DefaultObjective):
+            # Save on every epoch if no objective is specified.
+            self.model.save_weights(self.filepath)
+            return
         current_value = self.objective.get_value(logs)
         if self.objective.better_than(current_value, self.best_value):
             self.best_value = current_value


Test patch:
diff --git a/keras_tuner/engine/tuner_utils_test.py b/keras_tuner/engine/tuner_utils_test.py
index 312ef8f..4682cea 100644
--- a/keras_tuner/engine/tuner_utils_test.py
+++ b/keras_tuner/engine/tuner_utils_test.py
@@ -68,6 +68,28 @@ def test_save_best_epoch_with_multi_objective(tmp_path):
     )
 
 
+def test_save_best_epoch_with_default_objective(tmp_path):
+    objective = obj_module.create_objective(None)
+    filepath = os.path.join(tmp_path, "saved_weights")
+    callback = tuner_utils.SaveBestEpoch(objective, filepath)
+
+    model = keras.Sequential([keras.layers.Dense(1)])
+    model.compile(loss="mse")
+    val_x = np.random.rand(10, 10)
+    val_y = np.random.rand(10, 10)
+    history = model.fit(
+        x=np.random.rand(10, 10),
+        y=np.random.rand(10, 1),
+        validation_data=(val_x, val_y),
+        epochs=10,
+        callbacks=[callback],
+    )
+
+    model.load_weights(filepath)
+
+    assert history.history["val_loss"][-1] == model.evaluate(val_x, val_y)
+
+
 def test_convert_to_metrics_with_history():
     model = keras.Sequential([keras.layers.Dense(1)])
     model.compile(loss="mse", metrics=["mae"])